Saturday 27 August 2016

Understanding UNIX permissions and their attributes

Three types of user

 ·        User (owner) – The Owner is the usually the creator of the files/folders. In Linux, files or folders that you created in your Home directory are usually owned by you, unless you specifically change the ownership.

·        Group – The Group contains a group of users who share the same permissions and user privilege.

·        Other – Others means the general public.

Three types of protection

·        Read – You can only view the file, but cannot modify the content of the file. When applied on Folder, you can only view the files in the folder, but you can’t delete from or add files into the folder.
·        Write – You can edit and modify the file. For Folders, you can delete and add files into the folder.
·        Execute – Execute is mainly used when you need to run the file (commonly used when you need to run a script).

Super user can override these protection

Permission
rwx
0
none
---
1
execute only
--x
2
write only
-w-
3
write and execute
-wx
4
read only
r--
5
read and execute
r-x
6
read and write
rw-
7
read, write and execute
rwx

To change permission of file file1 type 
Chmod 777 file1
Well, the first digit is assigned to the Owner, the second digit is assigned to the Group and the third digit is assigned to the Others.
So for a file1 with ‘777’ permission, everyone can read, write and execute the file.

Sunday 21 August 2016

Shell and its types

Computer understand the language of 0's and 1's called binary language.
In early days of computing, instruction are provided using binary language, which is difficult for all of us, to read and write. So in OS there is special program called Shell. Shell accepts your instruction or commands in English (mostly) and if its a valid command, it is passed to kernel.
Shell is a user program or it's a environment provided for user interaction. 

Shell is an command language interpreter that executes commands read from the standard input device (keyboard) or from a file.
Shell is not part of system kernel, but uses the system kernel to execute programs, create files etc.
Several shell available with Linux including:

To find all available shells in your system type following command:
$ cat /etc/shells
Note that each shell does the same job, but each understand a different command syntax and provides
different built-in functions.

Following are the different types of Unix shells:
B shell - /bin/sh – This is the default Unix shell for many Unix operating systems .
Bourne shell was written by S. R. Bourne and its more emphasis is to use it as a scripting language rather than an interactive shell .
C-shell /bin/csh was designed to provide the interactive features lacking in b shell such as job control and aliasing .
K shell /bin/ksh – was created by David Korn and has features of both B shell and C shell along with some additional features .
Bash – the Bourne again shell was developed by GNU project .It is based on B shell language and has features of C and K shells.
tcsh is the default shell of FreeBSD and its descendants. Essentially it is C shell with programmable command line completion, command-line editing, and a few other features.
Zsh is a shell designed for interactive use and it has many of the useful features of bash, ksh, and tcsh along with many new features.

Saturday 20 August 2016

Basic Unix commands and its use


The UNIX operating system is case sensitive. All commands must be typed in lowercase letters unless noted otherwise.

man
This command displays the manual page for a particular command. If you are unsure how to use a command or want to find out all its options, you might want to try using man to view the manual page.

Example
To learn more about the ls command, enter:
man ls

To learn more about man, enter:
man man

Example
If you are not sure of the exact command name, you can use man with the -k option to help you find the command you need.

To see one line summaries of each reference page that contains the keyword you specify, enter:
man -k keyword

Replace keyword in the above example with the keyword which you want to reference.

who or w
The w and who commands are similar programs that list all users logged into the computer. If you use w, you also get a list of what they are doing. If you use who, you also get the IP numbers or computer names of the terminals they are using.

whatis
Whatis command is helpful to get brief information about Linux commands or functions. Whatis command displays man page single line description for command that matches string passed as a command line argument to whatis command. Whatis command searches for string in its index databases which is maintained by mandb program. Whatis command picks short description of NAME section of man page of command that matches to input given to the whatis command. Whatis provides several command line options to help user in getting brief information of specific Linux commands as per their need or interest.

Syntax
$ whatis [-options]

write (1) - send a message to another user
write (2) - write to a file descriptor

whereis
Whereis command is helpful to locate binary, source and manual pages of commands in the Linux system. It is very simple utility and provides several options which are given below with examples.

Syntax
$ whereis [-options]

Example
$ whereis open
open: /bin/open /usr/share/man/man1/open.1.gz /usr/share/man/man2/open.2.gz
It locates binary, source and man pages of “open” command and here it displayed paths where binary, man pages of open command is available in the system.

ls
Lists the names of files in a particular UNIX directory. If you type the ls command with no parameters or qualifiers, the command. displays the files listed in your current working directory.

Example
ls

Output: Lists the names of files in your default directory, in alphabetical order.

mkdir
mkdir is used to create new directories. In order to do this you must have write permission in the parent directory of the new directory.

Example
mkdir newdir

Will make a new directory called newdir.

Example
mkdir -p can be used to create a new directory, together with any parent directories required.
mkdir -p dir1/dir2/newdir

Will create newdir and its parent directories dir1 and dir2, if these do not already exist.

pwd
Displays the name of your current directory
The command pwd gives the full pathname of your current directory.

cd
cd is used to change from one directory to another.
Running cd without directory name changes directory to your home directory.

Example
cd dir1

The above changes directory so that dir1 is your new current directory. dir1 may be either the full pathname of the directory, or its pathname relative to the current directory.

cd ..
moves to the parent directory of your current directory.

vi
This command starts the vi text editor. To edit a file named myfile in the current directory, enter:

Example
vi myfile

The very least you need to know to start using vi is that in order to enter text, you need to switch the program from command mode to insert mode by pressing i . To navigate around the document with the cursor keys, you must switch back to command mode by pressing Esc. To execute any of the following commands, you must switch from command mode to ex mode by pressing : (the colon key): Enter w to save; wq to save and quit; q! to quit without saving. 

less and more
Both less and more display the contents of a file one screen at a time, waiting for you to press the Space bar between screens. This lets you read text without it scrolling quickly off your screen. The less utility is generally more flexible and powerful than more, but more is available on all Unix systems while less may not be.

Example
To read the contents of a file named textfile in the current directory, enter:
less textile

The less utility is often used for reading the output of other commands. For example, to read the output of the ls command one screen at a time, enter:
ls -la | less

In both examples, you could substitute more for less with similar results. To exit either less or more, press q . To exit less after viewing the file, press q .

Note: Do not use less or more with executable (binary files), such as output files produced by compilers. Doing so will display garbage and may lock up your terminal.

cat
Displays the contents of a file on your terminal.

Example
cat newfile

Result: Displays the contents of the file “newfile” on your terminal.

Example
cat newfile oldfile

Result: Displays the contents of two files -- “newfile” and “oldfile” -- on your terminal as one continuous display.

While a file is being displayed, you can interrupt the output by pressing <CTRL/C> and return to the UNIX system prompt.

The cat command is also used to concatenate (combine) files and put them into another file. If you concatenate files to another one that already exists, the existing contents are permanently lost.

Example
cat fileone filetwo filethree > newfile

Result: Links together three files -- fileone, filetwo and filethree -- into a new file called “newfile”. The original files remain intact.

cp
Makes copies of your files. You can use it to make copies of files in your default directory, to copy files from one directory to another directory, or to copy files from other devices.

Example
cp fileone filetwo

Result: Copies the contents of fileone to a file named filetwo. Two separate files now exist.

Example
cp /usr/neighbor/testfile .

Result: Copies the file testfile from the directory /user/neighbor to your UNIX account. The period( . ) at the end of the command line indicates that the file is to be copied to your current working directory and the name will remain the same.

To copy a file from another user’s directory on UNIX, you must know the person’s username.

Example
cp ~username/file1 yourfile

Result: Copies the file "file1" from user to your UNIX account. The name of the file in your directory becomes yourfile.

mv
This command changes the identification (name) of one or more files.

Example
mv oldfile newfile

Result: Changes the name of the file “oldfile” to “newfile”. Only one file will exist.

Example
mv oldfile bin/newfile

Result: Changes the name of the file “oldfile” to “newfile” and places it in the directory /bin. Only one file will exist.

rm
Deletes specific files. You can enter more than one file specification on a command line by separating the file specifications with spaces. 

Example
rm newfile

Result: Deletes the file named “newfile”.

Example
rm newfile oldfile

Result: Deletes two files -- “newfile” and “oldfile”.

Example
rm new*

Result: Deletes all files that begin with the prefix new.

chown
The chown command stands for change owner and group. its basic usage is: chown user.group /path/to/file. You will want to replace user and group with the username you're changing the file to, plus, the group separated by a period as shown, or a colon.

chmod
chmod alters the permissions on files and directories using either symbolic or octal numeric codes.

The symbolic codes are given here:-
u user + to add a permission r read
g group - to remove a permission w write
o other = to assign a permission explicitly x execute (for files),
access (for directories)

Example
chmod u=rw file1

Sets the permissions on the file file1 to give the user read and write permission on file1. No other permissions are altered.

Example
chmod u+x,g+w,o-r file1

Alters the permissions on the file file1 to give the user execute permission on file1, to give members of the user’s group write permission on the file, and prevent any users not in this group from reading it.

Example
chmod u+w,go-x dir1

Gives the user write permission in the directory dir1, and prevents all other users having access to that directory (by using cd. They can still list its contents using ls.)

diff
Display differences between text files

Example
diff file1 file2

Result : reports line-by-line differences between the text files file1 and file2. The default output will contain lines such as n1 a n2,n3 and n4,n5 c n6,n7 , (where n1 a n2,n3 means that file2 has the extra lines n2 to n3 following the line that has the number n1 in file1, and n4,n5 c n6,n7 means that lines n4 to n5 in file1 differ from lines n6 to n7 in file2). After each such line, diff prints the relevant lines from the text files, with < in front of each line from file1 and > in front of each line from file2. 

There are several options to diff, including diff -i, which ignores the case of letters when comparing lines, and diff -b, which ignores all trailing blanks. 

diff -cn

Produces a listing of differences within n lines of context, where the default is three lines. The form of the output is different from that given by diff, with + indicating lines which have been added, - indicating lines which have been removed, and ! indicating lines which have been changed. 

diff dir1 dir2

Will sort the contents of directories dir1 and dir2 by name, and then run diff on the text files which differ.

find
The find command lists all of the files within a directory and its subdirectories that match a set of conditions. This command is most commonly used to find all of the files that have a certain name.
Example
To find all of the files named myfile.txt in your current directory and all of its subdirectories, enter:
find . -name myfile.txt -print

Example
To look in your current directory and its subdirectories for all of the files that end in the extension .txt , enter:

find . -name "*.txt" -print

In these examples, the . (period) represents your current directory. It can be replaced by the full pathname of another directory to search. For instance, to search for files named myfile.txt in the directory /home/user/myusername and its subdirectories, enter:

find /home/user/myusername/ -name myfile.txt -print

On some systems, omitting the final / (slash) after the directory name can cause find to fail to return any results.
As a shortcut for searching in your home directory, enter:

find "$HOME/" -name myfile.txt -print

grep
Searches files for a specified string or expression. Grep searches for lines containing a specified pattern and, by default, writes them to the standard output.

Example
grep science science.txt

Result : Grep will print out each line containg the word science.
grep Science science.txt

The grep command is case sensitive; it distinguishes between Science and science.

To ignore upper/lower case distinctions, use the -i option, i.e. type

Example 
grep -i science science.txt

Example
To search for a phrase or pattern, you must enclose it in single quotes (the apostrophe symbol). For example to search for spinning top, type

grep -i 'spinning top' science.txt

Some of the other options of grep are:

-v display those lines that do NOT match
-n precede each matching line with the line number
-c print only the total count of matched lines

rm
rm is used to remove files. In order to remove a file you must have write permission in its directory, but it is not necessary to have read or write permission on the file itself. 

Example
rm file1

Result : Will delete the file file1. If you use

Example
rm -i file1

Result : Instead, you will be asked if you wish to delete file1, and the file will not be deleted unless you answer y. This is a useful safety check when deleting lots of files.

Example
rm -r dir1

Result : Recursively deletes the contents of dir1, its subdirectories, and dir1 itself, and should be used with suitable caution.

rmdir
rmdir - remove a directory
rmdir removes named empty directories. If you need to delete a non-empty directory rm -r can be used instead.

Example
rmdir exdir

Result : Will remove the empty directory exdir.

head
The head command writes the first ten lines of a file to the screen.

Example
head science.txt

Example
head -5 science.txt

It will writes the first five lines of a file to the screen.

tail
The tail command writes the last ten lines of a file to the screen.

Example
tail science.txt

wc
A handy little utility is the wc command, short for word count. 

Example
To do a word count on science.txt, type

wc -w science.txt

Example
To find out how many lines the file has, type

wc -l science.txt

* wildcard
The character * is called a wildcard, and will match against none or more character(s) in a file (or directory) name. For example, in your unixstuff directory, type

Example
ls list*

Result : This will list all files in the current directory starting with list....

Example 
ls *list

Result : This will list all files in the current directory ending with ....list

? wildcard
The character ? will match exactly one character.

So ?ouse will match files like house and mouse, but not grouse.

File and directory access rights
Each file and directory has associated access rights, which may be found by typing ls -l. 

Example
Also, ls -lg gives additional information as to which group owns the file beng95 in the following example:
-rwxrw-r-- 1 ee51ab beng95 2450 Sept29 11:52 file1

In the left-hand column is a 10 symbol string consisting of the symbols d, r, w, x, -, and, occasionally, s or S. If d is present, it will be at the left hand end of the string, and indicates a directory: otherwise - will be the starting symbol of the string.
The 9 remaining symbols indicate the permissions, or access rights, and are taken as three groups of 3.

The left group of 3 gives the file permissions for the user that owns the file (or directory) (ee51ab in the above example); the middle group gives the permissions for the group of people to whom the file (or directory) belongs (eebeng95 in the above example);the rightmost group gives the permissions for all others.

The symbols r, w, etc., have slightly different meanings depending on whether they refer to a simple file or to a directory.

Access rights on files.
r (or -), indicates read permission (or otherwise), that is, the presence or absence of permission to read and copy the file

w (or -), indicates write permission (or otherwise), that is, the permission (or otherwise) to change a file

x (or -), indicates execution permission (or otherwise), that is, the permission to execute a file, where appropriate

Access rights on directories.
r allows users to list files in the directory;

w means that users may delete files from the directory or move files into it;

x means the right to access files in the directory. This implies that you may read files in the directory provided you have read permission on the individual files.

So, in order to read a file, you must have execute permission on the directory containing that file, and hence on any directory containing that directory as a subdirectory, and so on, up the tree.

Example

-rwxrwxrwx a file that everyone can read, write and execute (and delete).


-rw------- a file that only the owner can read and write - no-one else


can read or write and no-one has execution rights (e.g. your mailbox file).

date
Date returns information on the current date and time in the format shown below:-

Tue Mar 25 15:21:16 GMT 1997

It is possible to alter the format of the output from date.

Exampleusing the command line
date ’The date is d/m/y, and the time is H:M:S.’at exactly 3.10pm on 14th December 1997, would produce the output

The date is 14/12/97, and the time is 15:10:00

ps
The ps command displays information about programs (i.e., processes) that are currently running. Entered without arguments, it lists basic information about interactive processes you own. However, it also has many options for determining what processes to display, as well as the amount of information about each. Like lp and lpr, the options available differ between BSD and System V implementations. For example, to view detailed information about all running processes, in a BSD system, you would use ps with the following arguments:

ps -alxww

To display similar information in System V, use the arguments:
ps –elf

kill
Use this command as a last resort to destroy any jobs or programs that you suspended and are unable to restart. Use the jobs command to see a list of suspended jobs. To kill suspended job number three, for example, enter:

Syntax
kill pid

Now check the jobs command again. If the job has not been cancelled, harsher measures may be necessary. Enter:

kill -9 pid

ptree
As you run the command, you get a hierarchical list of all the processes running on your system, along with process IDs (PIDs). This is a very useful command, because it shows you how exactly each process relates to others in your system. 

top
top provides an ongoing look at processor activity in real time. It displays a listing of the most CPU-intensive tasks on the system, and can provide an interactive interface for manipulating processes. It can sort the tasks by CPU usage, memory usage and runtime. can be better configured than the standard top from the procps suite. Most features can either be selected by an interactive command or by specifying the feature in the personal or system-wide configuration file. 

Syntax
top -hv | -bcisS -d delay -n iterations -p pid [, pid ...]

The traditional switches '-' and white space are optional.



Example
When operating top, the two most important keys are help ('h' or '?') and quit ('q') key. Alternatively, you could use the traditional interrupt key ('^C') when you're done.

top

Running the above command would give you a display similar to the below example. While the tasks are being displayed you can use any of the startup commands listed after the below example output.

top - 20:50:55 up 167 days, 14:26, 82 users, load average: 0.13, 0.05, 0.01
Tasks: 3 total, 1 running, 2 sleeping, 0 stopped, 0 zombie
Cpu(s): 0.0% user, 2.2% system, 0.0% nice, 97.8% idle
Mem: 514964k total, 507860k used, 7104k free, 30404k buffers
Swap: 1630588k total, 158708k used, 1471880k free, 275380k cached
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND 3747
hope 11 0 2004 1608 1608 S 0.3 0.3 0:00.56 sshd 22245
hope 10 0 688 676 672 S 0.0 0.1 0:00.06 csh 14015
hope 10 0 1024 1024 848 R 0.0 0.2 0:00.00 top

df
This command reports file system disk usage (i.e., the amount of space taken up on mounted file systems). For each mounted file system, df reports the file system device, the number of blocks used, the number of blocks available, and the directory where the file system is mounted.

Syntax
df

If the df command is not configured to show blocks in kilobytes by default, you can issue the following command:

df -k

du
This command reports disk usage (i.e., the amount of space taken up by a group of files). The du command descends all subdirectories from the directory in which you enter the command, reporting the size of their contents, and finally reporting a total size for all the files it finds. To find out how much disk space your files take up, switch to your home directory with the cd command, and enter:

Syntax
du

The numbers reported are the sizes of the files; on different systems, these sizes will be in units of either 512 byte blocks or kilobytes. To learn which is the case, use the man command, described below. On most systems, du -k will give sizes in kilobytes. 

cal
This command will print a calendar for a specified month and/or year.
To show this month's calendar, enter:
cal

To show a twelve-month calendar for 2008, enter:
cal 2008

To show a calendar for just the month of June 1970, enter:
cal 6 1970

awk
It take each line of the input file; if the line contains the pattern apply the action to the line and write the resulting line to the output-file

Example
 % awk 'pattern {action}' input-file > output-file

If the pattern is omitted, the action is applied to all lines:

% awk '{action}' input-file > output-file

By default, awk works on files that have columns of numbers or strings that are separated by white space (tabs or spaces), but the -F option can be used if the columns are separated by another character. awk refers to the first column as $1, the second column as $2, etc. The whole line referred to as $0. It is used to reformat the output of other commands. 

Example
To print only the second and sixth fields of the datecommand (the month and year) with a space separating them, at the Unix prompt, you would enter:

date | awk '{print $2 " " $6}'

free
free command displays information about free and used memory on the system.

Syntax
free [options]

Options


Example
To Print the memory size information:

free

total used free shared buffers cached
Mem: 223740 219492 4248 0 3756 73212
-/+ buffers/cache: 142524 81216
Swap: 1052216 66732 985484

uname
The command uname prints the name of the current unix system.

Syntax
uname[-amnprsv]
uname[-S system_name]

uname prints the current system name of the unix system to standard output.It is mainly useful to determine which system one is using.The options cause selected information returned by uname and/or sysinfo command to be printed: 

-a : Print all information.
-m: Print the machine hardware name.
-n: Print the node name(name by which the system is known to the communication network) .This is the default setting.
-p: Print the processor type of the current host.
-r: Print the operating system release.
-s:Print the name of the operating system.
-v: Print the version of the operating system. 

ping
The ping command sends ICMP Echo Request (ECHO_REQUEST) packets to the host once per second. Each packet that is echoed back via an ICMP Echo Response packet is written to the standard output, including round-trip time.

Syntax
ping serverName

ping ServerIPAddress

sort
The sort command sorts the contents of a file, in numeric or alphabetic order, and prints the results to standard output (usually the terminal screen). The original file is unaffected.
If filename is a file containing a list of words, at the Unix prompt, you would enter:
sort filename

This will print the list to the screen in alphabetical order (numbers first, then capital words, then lowercase words). 

To eliminate any duplicate entries in the list, use:
sort -u filename

To sort case-insensitively, use:
sort -f filename

To sort case-insensitively and in reverse order, use:
sort -fr filename

As with many Unix commands, you can redirect the output to a new file:
sort filename > newfilename

The output of the sort command will then be stored in a file named newfilename in the current directory.

You can also pipe the output of the sort command into other Unix commands, for example:
sort filename | more

This sends the output through the more command for easy reading.

To print only the first word of each line, enter:
sort filename | cut -f1 -d" "

ssh
SSH client is a program for logging into a remote machine and for executing commands on a remote machine. ssh connects and logs into the specified hostname.The user must prove his/her identity to the remote machine using one of several methods depending on the protocol version used. If command is specified, command is executed on the remote host instead of a login shell.

Syntax
ssh user@hostname command

Example 
Login into remote system called bash.codes.com and find out who logged in, enter:

$ ssh admin@bash.codes.com who

passwd
The Linux passwd command is used to change the password for a user account. A user can only change the password of his/her account but the superuser can change the password of any account. Besides changing password, this command can change other information like password validity etc.

Example
$ passwd guest

Changing password for guest.
(current) UNIX password:
Enter new UNIX password:
Retype new UNIX password:
passwd: password updated successfully

shutdown
Turn off the computer immediately or at a specified time.

Syntax

shutdown [-a][-t sec][-krhnfFc][time][warning-message]


Example
shutdown 8:00









It's not at all important to get it right the first time. It's vitally important to get it right the last time.