Pages

Monday, 1 September 2014

Communication cmd

write command
Send a message to another user who is logged in.

Syntax
$ write <user_name>

Example
mithun.s@mks-laptop:~$ write mukesh.kumar

write: mukesh.kumar is logged in more than once; writing to pts/0
hi mukesh ,how are you ?

in above command mithun.s sending message to mukesh.kumar as 'hi mukesh ,how are you ?'

Now mukesh.kumar will get this on his terminal as below
mukesh.kumar@mks-laptop:~$

Message from mithun.s@mks-laptop on pts/2 at 13:32 ...
hi mukesh

mesg command
allow and disallow to other user to send message to you.

Syntax
$ mesg y[or n]

Example
$ mesg  -- Displays the status as below
is y

Disallow other users to send you message.
$ mesg n
$ mesg     -- Now it displays the status as below
is n

To allow other users to send you message type
$ mesg y

Note : Even if user disables the message he/she would receive the messages from the root.

wall command
Send a message to all the logged in user who having message recieving permission as yes.

Syntax
$ wall [-n] [-t TIMEOUT] [file]

-n : no banner
-t : timeout ,default time out is 300 sec

Example
$ wall hello world

Broadcast message from root (pts/1) (Thu Feb 27 15:06:04 IST 2014):
hello world

ftp command
Ftp (File Transfer Protocol) is the user interface to the Internet standard File Transfer Protocol.
The program allows a user to transfer files to and from a remote network site.

Syntax 
$ ftp [hostname]

put command
to copy one file from the local machine to the remote machine
$ put filename

get command
To copy one file from the remote machine to the local machine
$ get filename

To copy multiple files from the local machine to the remote machine;
 you are prompted for a y/n answer before transferring each file
$ mput file1 [file2...]

copy all from the local machine to the remote machine,which are starting from 'f'.
$ mput f*

To copy multiple files from the remote machine to the local machine;
 you are prompted for a y/n answer before transferring each file
$ mget file1 [file2...]

To copy all files from remote machine to the local machine which are starting from 'f'.
$ mget f*

To exit the FTP environment (same as quit)
$ bye

To terminate a connection with another computer
System will give “Good Bye” message then type bye
$ close

To request a list of all available FTP commands
$ help or $ ?

User can also type ‘?’ symbol for to list all FTP command
![Command]
Syntax
$ ![Command [arg...]]

Used to execute any command in local machine
It shifts the control from ftp prompt to local machine

$ !

This will shift the control from ftp prompt to local machine. Now user can perform any operation in local system by using the command prompt.
To shift the control back to ftp prompt, use exit command.


Find Utility

Find is one of the powerful utility of Unix/Linux used for searching the files or group
of in a directory hierarchy.

Syntax :
     $ find [pathname] [expression]
Examples on the find utility :

1). How to find file 'abc.txt' from home directory downwards and print them ?
$ find ~ -name abc.txt -print
or
$ find  ~  –type  f  –name  abc.txt  –print
2). How to find which ends with any digits and begin with 'abc' from home
   directory downwards and print them ?
$ find ~ -name "abc[0-9]" -print
3). How to find a directory name 'dirname' from home directory downwards and 
      print them ?
$ find ~ -type d -name dirname -print
4). How to search a character special file 'abc.txt' from current directory ?
$ find . -type c -name abc.txt -print
5). How to find file in current directory , which has inode value 76538 ?
$ find . –inum 76538 –print
6). How to find all files from current directory , which have 2 links ?
$ find . –links 2 –print
7). How to find all files from current directory , which have more than 2 links ?
$ find . –links  +2 –print
8). How to find all files from current directory , which have less than 4 links ?
$ find . –links  -4 –print
9). How to find all files from home directory downwards, which have permissions 
      755 ?
$ find ~ –perm 755 –print
10). How to search a block special file ‘abc.txt’ from home directory downwards
       and print them?
$ find  ~  –type  b  –name  abc.txt  –print
11). How to search a file ‘abc.txt’ from home directory downwards whose owner
       is ‘mukesh’ ?
$ find ~ \( –user mukesh –a –name abc.txt \) –print
12). How to search all files from home directory downwards, which have size
      1 MB
$ find ~ –size 1M –print
13). How to search a file 'file.txt' from current directory downwards and execute 
       'rm' command  on the result ?
$ find . –name file.txt –exec rm {} \;
14). How to search a file 'file.txt' from current directory  and execute 'rm'
       command on the result after confirmation ?
$ find . –name file.txt –ok rm {} \;
  ( or )
$ find  .  –name file.txt –exec rm –i {} \;
15). How to search the file 'abc.txt' in the current directory and three levels down
to the current directory ?
$ find -maxdepth 4 -name "abc.txt"
16). How to print the files in the sub-directories between level 1 and 5 ?
$ find -mindepth 2 -maxdepth 6 -name "abc.txt"
17). How to find the empty files in current directory?
$ find . maxdepth 1 -empty
18). How to find all files in current directory?
find . -maxdepth 1 -type f

Compression Utilities

Compressed files use less disk space and download faster than large, uncompressed files. You can compress Linux files with the open-source compression
tool Gzip or with Zip, which is recognized by most operating systems.

Command Description
gzip -To compress and decompress the file(s)
-Compressed file(s) gets .gz extension
gunzip Used to decompress the file having extension of .gz
Works same as gzip -d file.gz
zip -Use to compress file(s) to avoid compatibility problemsand with non-Linux users.
unzip -To extract the contents of a zip file.
zcat Displays the contents of compressed files
zmore Used to view content pagewise of compressed file
tar -Used to create a tape archive file
-Extract from archive file
gzip command
This command can be use to compress and decompress the file(s), the compressed file(s) have .gz extension.
To compress the file(s) use
$ gzip file1.ext [file2.txt...]

To decompress back,compressed file(s)
$ gzip -d file1.ext.gz [ file2.txt.gz...]

gunzip command
It is same as 'gzip -d', use to decompress the file(s) having extension .gz
$ gunzip file1.ext.gz

zip commmand
If you exchange file(s) with non-Linux users,then you can use zip to avoid compatibility problems. Red Hat Linux can easily open zip or gzip files,
but non-Linux operating systems may have problems with gzip files.

To compress a file with zip, type the below command :
$ zip -r filename.zip files

In this example, filename represents the file you are creating and files represents the files you want to put in the new file:

unzip command
To extract the contents of a zip file, type:
$ unzip filename.zip

You can zip or gzip multiple files at the same time as following example
gzip filename.gz file1 file2 file3 /user/work/college
The above command will compress file1, file2, file3, and the contents of the /user/work/college directory and put them in filename.gz.

zcat command
If you try to view a compressed file using 'cat filename' , it wont show the readable content.
Hence zcat command can be use to view the content of compressed file properly.
$ zcat file1.txt.gz

zmore command
Use to view page wised of a compressed file.
$ zmore file1.txt.gz

tar command
Tar files place several files or the contents of a directory or directories in one file. This is a good way to create backups and archives.
Usually, tar files end with the .tar extension.
TO create a tape archive file
$ tar -cvf filename.tar files/directories

In this example, filename.tar represents the file you are creating and files/directories represents the files or directories you want to put in the new
file.
To extract the contents of a tar file, type:
$ tar -xvf filename.tar

Note :
If any of the files  are directories, tar  acts on the entire subtree. Options need not be preceded by -.
Function Options (choose one)
c - create a new tape
v - verbose mode
f - archive to file
x - Extract files  from tape (if files  not specified, extract all files)

Example
To backup 'abcd' dir, create an archive file 'abcd.tar' :
$ tar -cvf abcd.tar abcd

Note : c - create, v - verbose mode, f - archive to file.

To extract it back from the archive file:
$ tar -xvf abcd.tar

Note : The archive file can have any or no extension.

Process & Memory

Command Description
ps Displays the list of process submitted to kernel
pstree Displays the process in the form of tree diagram
bg Displays the status of process running in background
fg Used to bring any background job in foreground
nice Execute a command with updated scheduling priority
kill Used to send signal to perticular process
nohup Execute the command in background even after user get log out
df Print the system disk space usage and available
du Estimate file space usage in current directory

                Process related command
ps command
To see the list of processes submitted to kernel
Syntax
$ ps [option..]

To see full listing with PPID and including user and system process.
$ ps -ef

where :
-e to display all the processes.
-f to display full format listing.

To see every process with PPD and user on the system using BSD syntax:
$ ps -awx (or)  ps awx

where :
-a,a  all w/ tty, including other users
-w,w  wide output
-x,x  processes w/o controlling ttys

pstree command
To see the process in the form of tree diagram.
Syntax :
$ pstree [options] [pid or username]

To see the tree diagram with process identification numbers (PIDs)
$ pstree -p

We can use more or less command along with pstree to see pagewise view
$ pstree | less (or)  pstree | more

To see only those branches that have been initiated by a user with a username mukesh
$ pstree mukesh

bg command 
It can be used to see the status of jobs which are running in the background.
$ bg

fg command
To bring the job in foreground from background by its JobId or PID
Syntax
$ fg %JobID (or)  fg #PID

Some examples on background and foreground processes:
Below command will execute the process created by the shell in foreground and the output is redirecting in file1.
$ ls -laFrR > file1

To send the above process in the background add one symbol '&' at the end of command.
$ ls -laFrR > file1 &

[1] 3951

The value 1 written in square bracket is JobID for this job and 3951 is PID for this process.

Immediate after running above command we can check the status of this background processes.
$ bg

bash: bg: job 1 already in background

Once this background process get over , the status will be like below
$ bg

bash: bg: current: no such job

To bring this job in foreground, use the following command
$ fg %1      (or)      fg #3951

nice command
Use nice command to run a program with modified scheduling priority
Nicenesses range at least from -20 (resulting in the most favorable scheduling) through 19 (the least favorable). The default behavior is to increase the
niceness by 10.
Syntax
$ nice [option][command]

Examples on nice command
To see the current scheduling inherited priority
$ nice

Below command will execute ls command with incremented scheduling priority. Here we are not giving any adjustment to priority, so by default priority
is incremented by 10.
$ nice  ls

To increase the scheduling priority by 5
$ nice –n5 ls

kill command
Kill sends signal to the process having with process ID as pid. If no option is specified,it sends default signal 15 (software termination signal) to the process.
Syntax
$ kill [signal] PID

Where :
Option - 1   Hang-up signal.
Option - 2   Interrupt signal.
Option - 9   terminate the process.
Option -15   Software termination signal.

Example: 
$ kill  -9  3951

Rules on the kill command
1.You can kill all your own process.
2.Only root user can kill system level process.
3.Only root user can kill process started by other users.

nohup command
nohup command allows to run command/process or shell script that can continue running in the background after you log out from a shell
Syntax:
$ nohup <commandname/shells-script>
(or)
$ nohup <commandname/shells-script> &

Example: 
$ nohup txncleanup.sh  &

Memory related command
df command
Report the amount of disk space being used by file systems,If no file name is given, the space available on all currently mounted file systems is shown.
Syntax
$ df [option] [file_name]

Type the below command to see variaous option which can be used along with df
$ df --help

Example :
To display all file systems and their disk usage
$ df

output:
Filesystem           1K-blocks      Used Available Use% Mounted on
/dev/sda1            150900956  94750776  48484824  67% /
none                    504780       280    504500   1% /dev
none                    509024      2164    506860   1% /dev/shm
none                    509024       208    508816   1% /var/run
none                    509024         0    509024   0% /var/lock
none                    509024         0    509024   0% /lib/init/rw

To display above command in human readable format
$ df -h

output:
Filesystem            Size  Used Avail Use% Mounted on
/dev/sda1             144G   91G   47G  67% /
none                  493M  280K  493M   1% /dev
none                  498M  2.1M  495M   1% /dev/shm
none                  498M  208K  497M   1% /var/run
none                  498M     0  498M   0% /var/lock
none                  498M     0  498M   0% /lib/init/rw

du command
du estimates and displays the disk space used by files.
Summarize disk usage of each FILE, recursively for directories.
Syntax
$ du [option] [filename]

$ du [option] --files0-from=F

Example
To display the size of each files starting from 'f' in current directory.
$ du -sh f*

Type the below command to see variaous option which can be used along with du
$ du --help


Text Processing Commands

Command Description
paste To combine the content of file files line by line and printing the files content horizontally
wc Displays the no of lines words and characters of any data
head Displays the lines from top to bottom as per option given
tail Displays the lines from bottom to top as per option given
cut Used to retrieves specific characters or fields from data or file
tr Used on out put of some other command to translate characters
sort Used to sort data, by default sorting happens as per the English disctionary
grep Used to pattern search in the given file
uniq Removes adjacent repeated lines in given data of file
cmp Used to compare two files, Its pinpoints at only first diffrence
comm Used to compare two files but can be apply only on sorted files
paste command
Used to combine 2 or more files content line by line and display the files content horizontally
Syntax
$ paste file1.txt [file2.txt..]

wc command
Used to display the no of lines,words and characters in any file or in any data.
syntax
$ wc [-lwc] filename.txt

head command
Used to display the set of lines from start in given file or data.
Syntax
$ head [-n] filename.txt

tail command
Used to display the set of lines from end of given file or data.
Syntax
$ tail [-n] filename.txt

Examples on head and tail commands
consider file1.txt having 15 lines as showing below
$ cat file1.txt

 1    One
 2    Two
 3    Three
 4    Four
 5    Five
 6    Six
 7    Seven
 8    Eight
 9    Nine
10    Ten
11    Eleven
12    Twelve
13    Thirteen
14    Fourteen
15    Fifteen

To print first 10 lines from top
$ head file1.txt
    (or)
$ head -10 file1.txt

Note : If count is not specified in command ,then by default it will take first 10 lines

To print the first 7 lines
$ head -7 file1.txt>
     (or)
$ head file1.txt -n 7

To print 10 lines from bottom
$ tail file1.txt
    (or)
$ tail -10 file1.txt

Note : If count is not specified in command ,then by default tail command will
take 10 lines from bottom

To print the 7 lines from bottom
$ tail -7 file1.txt
     (or)
$ tail file1.txt -n 7

To print the lines from line no 3 to 8
$ head -8 file1.txt | tail -6

cut command :
Used to retreive specific fields or characters from a file or data, it is used on output
of some other command
Syntax
$ cut [option] [filename]

Examples
Consider a file name file3.txt having content as following
$ cat > file3.txt

hello how are you!
one two three four

To retrieve the 7th character
$ cat file3.txt | cut -c7

To retrieve a character set from 5th to 9th character
$ cat file3.txt | cut -c5-9    

To retrieve 5th and 9th character
$ cat file3.txt | cut -c5,9

We can pipe cut command on any command like date ie:
$ date | cut -c7

$ date | cut -c5-9

To cut the 4th field delimated by spaces
$ date | cut -d "" -f4

To cut the 4th to 6th field delimated by space
$ date | cut -d "" -f4-6

To cut the 4th and 6th field delimated by space
$ date | cut -d "" -f4,6>
   
$ cat file3.txt | cut -d "" -f4,6

Note :
-c : for character number
-d : for delimiter (either space,comma,colon etc)
-f : for field number

tr (translate) command
Used to translate characters, by pipe on some other command
Syntax
$ tr [option] SET1 SET2

Examples
To squeezing character or remove the extra spaces between words
type tr -s " " on output of command ie :
The output of command 'who am i' having extra space as
$ who am i
mukesh.kumar pts/4        2014-03-12 16:08 (:0.0)

The following command will squeeze the words or remove the extra spaces
$ who am i | tr -s " "
mukesh.kumar pts/4 2014-03-12 16:08 (:0.0)

Translating the character without squeezing
$ who am i | tr " " "$"

Squeezing and then translation
$ who am i | tr -s " " "$"

tr Command examples:
Consider file2.txt having the below content
$ cat file2.txt

Hi       Friends
Hello      World
How are    you      !
Lets        learn the    commands

To remove the extra spaces or squeezing the space character
$ cat file2.txt | tr -s " "
output:
Hi Friends
Hello World
How are you !
Lets learn the commands

Squeezing space and translating the space to '$' character
$ cat file2.txt | tr -s " " "$"
output:
Hi$Friends
Hello$World
How$are$you$!
Lets$learn$the$commands

Translating the spaces without squeezing
$ cat file2.txt | tr  " " "$"
output:
Hi$$$$$$$Friends
Hello$$$$$$World
How$are$$$$you$$$$$$!
Lets$$$$$$$$learn$the$$$$commands

To convert the lower case into upper case letter
$ cat file2.txt | tr  "[a-z]" "[A-Z]"
output:
HI       FRIENDS
HELLO      WORLD
HOW ARE    YOU      !
LETS        LEARN THE    COMMANDS

Translating all character 'H' or 'h' to '#'
$ cat file2.txt | tr  '[H,h]' '#'
output:
#i       Friends
#ello      World
#ow are    you      !
Lets        learn t#e    commands

sort command
Used to sort the data by default it sorts as per the english disctionary
It used on output of some other command or file content
Syntax
$ sort [option] [filename]

Examples
To sort as per the english disctionary
$ sort file.txt

To sort in reverse order
$ sort -r file.txt

To apply the numerical sort
$ sort -n file.txt

To remove all duplicate recards in file or data
$ sort -u file.txt

To merge the content of two or more file in sorted manner
$ sort -m file1.txt file2.txt

Note:
If using -m option with sort command, file1.txt file2.txt should be in sorted order.
If the supplied arguments files(file1.txt, file2.txt) are not in sorted order then we should
use only 'sort' instead of 'sort –m'.

Consider the following data in student.txt, in which the first column is student id ,
2nd column is marks and 3rd is subject.
$ cat student.txt

2001:90:Math
2002:60:Physics
2003:75:Chemistry
2004:84:Math
2005:94:Chemistry
2006:96:Math
2007:86:Physics
2008:91:Physics
2009:89:Physics

Sorting based on the second column, delimated by colon
$ sort -k2 -t ":" student.txt
       (or)
$ sort +1 –t ":" student.txt
output:
2002:60:Physics
2003:75:Chemistry
2004:84:Math
2007:86:Physics
2009:89:Physics
2001:90:Math
2008:91:Physics
2005:94:Chemistry
2006:96:Math

Sorting based on the 3rd column, delimated by colon
$ sort -k3 -t ":" student.txt
       (or)
$ sort +2 –t ":" student.txt
output:
2003:75:Chemistry
2005:94:Chemistry
2001:90:Math
2004:84:Math
2006:96:Math
2002:60:Physics
2007:86:Physics
2008:91:Physics
2009:89:Physics

To store the final merged data into onother one file
$ sort –m file1.txt file2.txt –o output

grep command
Used to search for PATTERN in each FILE or standard input.
Syntax
$ grep [OPTION]... PATTERN [FILE]

Examples
To search the pattern 'Linux' in file1.txt
$ grep Linux file.txt

To search the pattern 'Linux' in multiple files say file1.txt,file2.txt,file3.txt
$ grep Linux file1.txt file2.txt file3.txt

To search for sequence of strings, including phrases
$ grep 'hello world' file1.txt file2.txt file3.txt

To ignore case while searching , use option '-i'
$ grep -i linux file.txt

To print the count of lines which having pattern 'linux'
$ grep -c linux file.txt

To print the lines which are not having pattern 'linux'
$ grep -v pattern file.txt

To search pattern for all txt files in current directory
$ grep 'linux is' *

To print the line numbers with each output lines ,where given pattern 'linux' found
$ grep –n linux file.txt

To print only the name of the files where pattern 'linux' is matching
$ grep –l linux file1 file2 file3

Some more useful grep command:
To prints lines containing any character
$ grep '.' filename.txt

To prints lines containing period character
$ grep '\.' filename.txt

To prints blank lines or lines not containing any character.
$ grep –v '.' filename.txt

To print line starting with any pattern
$ grep '^pattern' filename.txt

To print line ending with any pattern
$ grep 'pattern$' filename.txt

Examples in which defining sone boundry of words
To print the lines which are containing words starting with pattern 'linux'
$ grep '\<linux' filename.txt

To print the lines which are containing words ending with pattern 'linux'
$ grep 'linux\>' filename.txt

To print the lines which are containing given pattern 'linux' as a whole words
$ grep '\<linux\>' filename.txt

uniq command
To removes adjacent repeated lines in the given data or files
Syntax
$ uniq [OPTION]... [INPUT [OUTPUT]]

Examples
consider the file as file4.txt which containing following data
cat file4.txt

math
physics
physics
chemistry
linux
math
math
window

To removes adjacent repeated lines in the given data or files
$ uniq file4.txt
output:
math
physics
chemistry
linux
math
window

To print only adjustant duplicate lines
$ uniq -d file4.txt
output:
physics
math

To print only uniq lines which is not adjustant duplicate
$ uniq -u file4.txt
output:
math
chemistry
linux
windo

To display the line count of occurrences of line adjustant duplicate
$ uniq -c file4.txt
Output:
      1 math
      2 physics
      1 chemistry
      1 linux
      2 math
      1 window

cmp command
Used to compare two files byte by byte ,its pinpoints only the first diffresnce between two files
Syntax
$ cmp file1.txt file2.txt

Consider the content of file1.txt
$ cat file1.txt

john
monty
hi
linux
bye

Consider the content of file2.txt
$ cat file2.txt

john
monty
hi
unix
bye

Now on comparing the both the file
$ cmp file1.txt file2.txt
output:
file1.txt file2.txt differ: byte 15, line 4

The above command is comparing both the files and only giving first difference at line no 4.

comm command
Used to compare sorted files FILE1 and FILE2 line by line.
Syntax
$ comm [OPTION]... FILE1 FILE2


comm command gives the output in 3 columns
 1.data uniq in first file
 2.data uniq in second file
 3.data common in both files

To use comm command both the files should be in sorted manner.
To sort the file1.txt and storing the result in file10.txt
$ sort file1.txt > file10.txt

To sort the file2.txt and storing the result in file20.txt
$ sort file2.txt > file20.txt

Now on using the 'comm' command on sorted files as following
$ comm file10.txt file20.txt
output :
bye
hi
john
  linux
monty
unix