Friday, 29 May 2015

UNIX Commands

Unix Command is a program written to perform certain specific action. All such programs have a name. For example, The program that is used to print today's date in a specific manner has the name date and the program that is used create a small file or display the contents of file has the name cat and so on.
  1. All unix commands are written using lower case letters . For example, cat ,ls,who,date ,and so on.
  2. Almost all the unix commands are cryptic . For example, cat stands for concatenation, ls stands for listing and so on .
  3. Unix commands can have zero or more number of arguments associated with them
  4. Unix commands can also have format specifiers as well as option associated with them . Format specifiers, whenever present, are indicated by the + character. Options associated with format specifiers, whenever present are indicated by hyphen(-).There could be many number of associated with a command.
  5. A current Unix command can be killed by using either delete or ctrl-u command.

Types of Unix Commands

Basically there are two types of Unix commands. they are External command and Internal command
  1. External Commands A command with an independent existence in the form of a separate file is called an external command. For example, programs for the commands such as cat and ls, exist independently in a directory called the /bin directory. When such commands are given, the shell reaches these command files with the help of system variable called PATH variable and executes them. Most of the Unix commands are external commands
  2. Internal Commands A command that does not have an independent existence is called an internal command. Actually the programs for internal commands will be a part of another program. For example, the echo command is an internal command as its program will be a part of the shell's program, sh. In other words echo command is built into the shell.

Some Basic Commands

Unix has several hundreds of commands within it. Most of them are simple and are powerful. A few such commands are introduced in this site.

The echo Command

the echo command is used to display message .it takes zero , one or more number of arguments. Arguments may be given either as a series of individual symbols or as a string within a pair of double quotes (" ").

Example


  1. $ echo
                        //A blank line displayed
    $
  2. $ echo welcome to unix      shell programming
    welcome to unix programming
    $
  3. $ echo "welcome to unix     shell programming"
    welcome to unix     shell programming
    $
  4. $ echo "The home directory is " $HOME
    The home directory is /home/xyz
    $
In first example the echo command without an argument prints a blank line. In second example, even if there extra space between the arguments, They are adjusted and output is printed in a standard form, With only one blank space between the different arguments. However when the message is given in the form of a string arguments, it is printed as it is ,as you can see in example three. And in last example the message is printed along with the output of HOME command.

The printf Command

The printf command is an alternative to echo, but unlike echo, it doesn't automatically insert a newline unless the \n is used explicitly.

Example


  1. $ printf "my current shell is \n $SHELL \n"
    my current shell is
    / bin / bash
    $

The tput Command

This command is used to control the movement of the cursor on the screen as well as to add certain features like blinking, boldface and underlining to the displayed messages on the screen.

Example

Description

tput clear
The tput command along with the clear arguments clears the screen and puts the cursor at the left top of the screen.
tput cupThe tput command along with cup argument and certain co-ordinate values is used to position the cursor at any required position on the screen.
tput linesThe $tput command along with lines argument is used to know the number of rows present on the current terminal.
tput colsThe $tput command along with cols argument is used to know the number of columns present on the current terminal.

the tty Command

This command is used to know the terminal on which the user is working .In Unix, every terminal is associated with a special file, called the device file. All the device files will be present in the /dev directory. A user can know the name of device file on which he is working by using tty command.

Example


  1. $ tty
    / dev / tty01
    $
  2. $ tty
    / dev / pts / 1             //This is the output from Linux
    $
Some time you may like to change the settings of your terminal. The stty Command is used to both display and change settings of the terminal.

Example

Description

stty -a
The -a(all) option displays the current settings.
stty echoe
Whether backspacing should erase the character is decided by the keyword echoe. Since this command set here, backspacing removes the character from display. You can use the same keyword to reverse this setting. Here you need to prefix a - to the echoe keyword(stty -echoe).
stty -echoWith this setting ,keyboard entry is not echoed. You should turn it off after the entry is completed by usingstty echo.
stty intr \^cstty also sets the functions for some of the keys. For instance, if you like to use [ Ctrl-C ] as the interrupt key instead of [Delete], you'll have to use this command
stty eof \^aYou used [Ctrl-d] to terminate the terminal or to terminate input. This is eof character is also selectable. Instead of [Ctrl-d], you can use [Ctrl-a] as eof character.

The who Command

Unix maintains an account of all users who are logged on to the system. The user can know login details of all the current users by using the who command provides a list of all the current users.

Example


  1. $ who
    mgv      pts / 0      2014-07-29 23:09 (:0)             //Login details of all current users
    dvg      pts / 1      2014-07-29 22:04 (:1)
    $
  2. $ who am i
    mgv      pts / 0      2014-07-29 23:09 (:0)             //Self-login details
    $
  3. $ who -Hu
    NAME      LINE     TIME                              IDLE     PID     COMMENTS             //Login details of all users with proper headers for the columns
    mgv      pts / 0     2014-07-30 17:19          0:48     1475     (:0)
    dvm      pts / 1     2014-07-30 17:25          0:33     2172     (:1)
    $
From first example, The who command displays the login details of all the current users. From second example, The who am i command is used to display self-login details. As seen from the last example -H option provides headers for the columns and the -u option provides more details like idle time, PID and comments.

the uname Command

When this command is used, it gives the name of Unix variants. Certain option like r,v,m and a, can be used with this command.

Example


  1. $ uname
    Linux
    $
  2. $ uname -r
    3.13.0-24-generic             //release details
    $
  3. $ uname -m
    i686                                   //machine details
    $
Some other options like -v gives the version of the system being used. The use of the -a option gives all the details of the system. The -n option display either the host name or the complete domain name.

The date Command

This command is used to display the current date along with the time. This is one of the very few commands that allow the use of format specifiers as arguments. Format specifiers are single characters using which, one can print the date in a specific manner. Each format specifier is preceded by a + symbol followed by the % operator.

Example


  1. $ date
    Wed Jul 30 19:51:17 IST 2014
    $
  2. $ date +%m             //format specifier m displays current month in the numeric form.
    07
    $
  3. $ date +%h             //format specifier h displays the name of current month
    Jul
    $
  4. $ date "Today's date is +%d"             //format specifier d display the day of current month in dd format
    Today's date is 30
    $
  5. $ date +%D             //format specifier D display the day of current month in mm/dd/yy format
    07/30/2014
    $
  6. $ date +%Y             //format specifier Y gives all the four digits of the year.
    2014
    $
Some other format specifiers such as H, M and S stand for hour, minute and second, respectively. you can also use number of options like u, r, R, f with this command.

The cal Command

The cal command is used to print the calendar of a specific month or a specific year. when this command is used without any arguments, the calendar of the current month of the current year will be printed.

Example


  1. $ cal
                                                         July 2014
    SuMoTuWeThFrSa
        1 2 3 
     4 5 6 7 8 9 10
     11 12 13 14 15 16 17
     18 19 20 21 22 23 24
     25 26 27 28 29 30 31
           
    $
When two numeric arguments are given to cal command (for example $ cal 01 2014 or $ cal Jan 2014), the first argument will be considered as the month, the second argument will be considered as the year and the calendar for that month of that year will be printed on the screen. And when single numeric argument is given, the complete calendar for the entire year represented by the numeric argument will be printed on the screen.

The calendar Command

The calendar utility checks the current directory or the directory specified by the CALENDER_DIR environment variable for a file named calendar and displays lines that begins with either today's date or tomorrow’s.

Example


  1. $ calendar
    Aug 06       Jonathan B. Postel is born in Altadena, California, 1943
    Aug 06       Caricom in Barbados
    Aug 06       Cy Young pitches first game, 1890
    Aug 06       Bank Holiday in British Columbia, Fiji, Iceland, Ireland, Ontario
    Aug 06       Emancipation Day in Bahamas
    Aug 06       Independence Day in Bolivia
    Aug 06       John-Mark Gurney born in Detroit, Michigan, United States, 1978
    Aug 06       Soleil rouge en août, C'est de la pluie partout.
    Aug 07       Battle of Boyaca in Colombia
    Aug 07       Jonathan Mini born in San Mateo, California, United States, 1979
    Aug 07       Aujourd'hui, c'est la St(e) Gaëtan.
    Aug 07       Gründung der Sozialdemokratischen Arbeiterpartei in Eisenach unter der Führung von August Bebel und Wilhelm Liebknecht, 1869
    $
  2. $ calendar -A 00
    Aug 06       Jonathan B. Postel is born in Altadena, California, 1943
    Aug 06       Caricom in Barbados
    Aug 06       Cy Young pitches first game, 1890
    Aug 06       Bank Holiday in British Columbia, Fiji, Iceland, Ireland, Ontario
    Aug 06       Emancipation Day in Bahamas
    Aug 06       Independence Day in Bolivia
    Aug 06       John-Mark Gurney born in Detroit, Michigan, United States, 1978
    Aug 06       Soleil rouge en août, C'est de la pluie partout.
    $
The options are as follows

Option

Description

-A num
Print lines from today and next num days. As shown in example2(here num is equal to zero).
-a
Process the “calendar” files of all users and mail the results to them. This requires super user privileges.
-B num
Print lines from today and previous num days (backward, past).
-b
Enforce special date calculation mode for KOI8 calendars.
-l num
Print lines from today and next num days (forward, future). Defaults to one. (same as -A)
-w num
Print lines from today and next num days, only if today is Friday (forward, future). Defaults to two, which causes calendar to print entries through the weekend on Fridays.
-f
calendarfile Use calendarfile as the default calendar file.
-t
-t [[[cc]yy]mm]dd Act like the specified value is “today” instead of using the current date. If yy is specified, but cc is not, a value for yy Between 69 and 99 results in a cc value of 19. Otherwise, a cc value of 20 is used.

The passwd Command

User can change his or her Unix System password using this command

Example


  1. $ passwd
    ( current ) UNIX password :       //Enter your current password.
    Enter new UNIX password :       //create new password.
    Retype new UNIX password :     //Retype new password.
    password changed
    $

The bc Command

This command is used to perform all the usual arithmetic operations as well as change of bases in the range of base 2-16.

Example


  1. $ bc        // simple addition using bc command.
    n = 5 + 5
    n + 5
    15
    quit        // A session with bc is terminated using quit command.
    $
  2. $ bc
    sqrt(15)
    3
    scale = 3        // The precision is set to 3 using scale function.
    sqrt(15)
    3.872
    quit
    $
  3. $ bc
    ibase = 10
    obase = 2
    7             // The base of 7 is 10.
    111        // 7 is converted to binary(base 2).
    quit
    $

The script command

This command lets you record your terminal session in a file. You’ll find it useful to store in a file all keystrokes as well as output and error messages. You can later view this file.

Example


  1. $ script
    Script started, file is typescript
    $ echo "Now it’s recording my session"
    Now its recording my session
    $ exit
    Script done, file is typescript
    $ cat typescript
    Script started on Wednesday 06 August 2014 08 : 56 : 28 PM IST
    $ echo "Now it’s recording my session"
    Now its recording my session
    $ exit
    Script done on Wednesday 06 August 2014 08 : 56 : 29 PM IST
    $
As shown in the above example. When script command is executed and once the prompt returns all keystrokes that are entered get recorded in the file typescript. Recording is terminated using exit command. typescript can be viewed using catcommand. Script overwrites any previous typescript that may exist. If you want to append to it, or want to use a different log file, following options will help you.
script -a            //appends to the existing typescript.
script logfile        //Logs activates to logfile.

The man Command

The details pertaining to a command or a utility can be seen using man command. Thus the details of the bc command can be obtained using a command line as shown below.

Example


  1. man bc
    bc(1)                           General Command Manual                                    bc(1)
    NAME
             bc - An arbitrary precision calculator language
    SYNTAX
             bc [ -hlwsqv ] [long - option ] [ file . . . ]
    DESCRIPTION
The very first line in a page begins and ends with the name of the command. A numeral appears within a pair of parenthesis that refers to the section of the manual in which this entry is found. NAME refers to the name of the command and a one-line description of the command. Following the NAME appears a SYNOPSIS or for some command SYNTAX of the different formats that can be used with the command. The explanation of what happens when the command is used is given withinDESCRIPTION. Additional explanation, if any, will be present within NOTES.
For certain command. the output is very large and it scroll off fast and the user will be able to see only the last screen full of information. In such cases the output can be piped to the more command (for example : $ man command-name | more ) .

How to give multiple commands in a single line

Normally a single command is given to shell at its prompt. You can give multiple commands in a single command line using semicolon (;) between successive commands as shown below.

Example


  1. echo " i am giving multiple commands in a single line " ; date +%D ; who am i
    i am giving multiple commands in a single line
    08/12/14
    mgv-pc pts/1       2014-08-12 10:02 (:0)
    $


Ordinary files commands

Ordinary Files: An ordinary file is randomly addressable sequence of bytes. Most of the files, like data files, source program files, files containing Unix commands or any text file, are called ordinary or regular files.

The cat Command

The basic purposes of this command is to create small Unix file or Display the content of a file or files.

Example


  1. ~$ cat > txtfile1        //creates a file named txtfile1.
    hi this is a Unix command to create an ordinary file        //Enter text and press [ control-d ] to terminate input.
    And insert some text into that file.
    ~$
  2. ~$ cat txtfile1         //Display the content of txtfile1.
    hi this is a Unix command to create a text file
    and insert text into that file.
    ~$
  3. ~$ cat web.html         //Display the content of web.html file.
    < html >
    < head > well come to Unix </head >
    < body >
    < /body >
    < /html >
    ~$
  4. ~$ cat txtfile1 -n         //The -n option numbers lines.
    1       hi this is a Unix command to create a text file
    2       and insert text into that file.
    ~$
  5. ~$ cat txtfile1 txtfile2        //Display the content of txtfile2 immediately after the content of the content oftxtfile1.
    hi this is a Unix command to create a text file
    and insert text into that file. And this is the content of txtfile2
    ~$
  6. ~$ cat txtfile1 > txtfile2        // Overwrites the content of txtfile2 by the content of txtfile1.
    ~$
  7. ~$ cat txtfile1 >> txtfile2        // Append the content of txtfile1 to txtfile2.
    ~$
In first example, after executing the $ cat > txtfile command, the $ prompt vanishes and the system is ready to accept the input from the standard input--the keyboard. At this point the user can type in anything. [Ctrl-d] is used terminate the input mode. In second example it just Display the contents of file txtfile1. You can also use -v option to display non printing characters. In third example -n option is used to number the lines. And in fourth example the contents of filetxtfile2 is displayed immediately after the contents of file txtfile1.

The cp Command: Copying a file

The cp command is used to a file or group of files. The syntax requires at least two names to be specified in the command line. As shown in the following example.

Example


  1. ~$cp srcfile destfile
    ~$
  2. ~$cp srcfile MyDirectory / destfile1     //The file srcfile is copied into the file destfile1 which is under the directory MyDirectory.
    ~$
  3. ~$cp srcfile MyDirectory     //The file srcfile is copied into the directory MyDirectory with same file name,srcfile .
    ~$
  4. ~$cp txtfile1 txtfile2 txtfile3 MyDirectory     //All the three files txtfile1, txtfile2 & txtfile3 are copied into the directory MyDirectory
    ~$
  5. ~$cp Desktop/Programs/ section1      //All the three files txtfile1, txtfile2 & txtfile3 are copied into the directory MyDirectory
    ~$
As you can see in the first example. The srcfile file is a source file and destfile is a destination file. With the execution of the above mentioned command line the following action takes place.
  • If the destination file ( destfile ) exists, it will be overwritten by the content of source file( srcfile ).
  • If the Destination file ( destfile ) does not exist, it is created and then the content of srcfile are written in to it.

the Interactive ( -i ) option :

You can avoid overwriting using -i option. When this option is used the system pauses and asks for the user permission before overwritten.if answer is y or Y then the file is overwritten. any other key will avoid overwriting.

Example


  1. ~$cp -i srcfile destfile
    cp : overwrites `destfile' ?      // hit y or Y to overwrite or hit any other key to avoid overwrite.

The Recursive ( -r ) option :

copying all files and directory under a current directory into another directory can be done using -r option.

Example


  1. ~$cp -r srcDirectory destDirectory
    ~$
In the above example the command line copies all the files and sub-directories under the srcDirectory directory into the directory destDirectory .

The mv Command: Move or Rename files

This command is used to move or rename files and directories. This command takes a minimum of two arguments. The first argument will be the name of a file or a directory to be moved or renamed.The second argument may also a file name or a directory name.

Example


  1. ~$ cat textfile1
    Hi this is the content of textfile1
    ~$ mv textfile1 textfile2     // textfile1 is renamed as textfile2
    $ cat textfile1
    cat: textfile1: No such file or directory
    ~$ cat textfile2
    Hi this is the content of textfile1
    ~$
  2. ~$ mv -i textfile3 textfile2     // asks for the user permission to overwrite textfile2
    mv: overwrites textfile2 ?      // hit y or Y to overwrite. Or any other key to avoid overwriting.
When the command mv is executed if the destination file is already exists, it will be overwritten. Normally no warning will be given.If necessary we can avoid this accidental overwriting an existing file by using -i option. As shown in second example.
    Following examples shows moving of files or directories from current directory to another directory .

Example


  1. ~$ mv textfile2 Desktop/Programs
    ~$ ls Desktop/Programs
    textfile2
    ~$
  2. ~$mv srcfile1 srcfile2 srcfile3 Desktop/Programs
    ls Desktop/Programs
    srcfile1 srcfile2 srcfile3 textfile2
    ~$
  3. ~$mv MyDirectory Desktop/Programs      //The directory MyDirectory is moved to Programs directory.
    ~$ls -F Desktop/Programs
    srcfile1* srcfile2* srcfile3* textfile2 MyDirectory /    // * indicates executable files, / indicates a Directory and unmarked file is a ordinary file.
    ~$

The rm Command : Delete Files

The command rm is used to remove or delete files. This command can delete more than one file with a single instruction.

Example


  1. ~$rm srcfile1 srcfile2     // It silently removes both the files srcfile1 and srcfile2.
    ~$
  2. ~$rm src*     // It deletes all the files having a beginning pattern of src.
    ~$
  3. ~$rm Desktop/Programs/samplefile1     // It deletes a file samplefile1 which is under the Programsdirectory.
    ~$
  4. ~$rm -r srcDirectory     // Usually directories are removed using rmdir but, you can also do the same by using rm along with the -r option.
    ~$
  5. ~$rm -r *     // The rm command with -r option removes all the files and sub-directories under the current directory.
    ~$
  6. ~$rm -i txtfile     // The interactive option ( -i ) asks user permission to remove file or files.
    ~$

wc Command : counting the number of Lines, Words, Characters

This command is used to count the number of lines, words and characters in one or more files. It takes one or more filenames as a arguments and gives output in four columns. The first column indicates the number of lines, the second column indicates number of words, third column indicates the number of characters and last column indicates the filename.

Example


  1. ~$wc srcfile1
    3 40 200 srcfile1     // It shows that file srcfile1 has 3 Lines, 40 Words & 200 Characters.
    $
  2. ~$wc srcfile1 srcfile2
    3 40 200 srcfile1
    4 37 180 srcfile2
    7 77 380 total
    ~$
  3. ~$wc -l srcfile1     // The -l option counts only the number of Lines.
    3
    ~$wc -w srcfile1     // The -w option counts only the number of Words.
    40
    ~$wc -c srcfile1     // The -c option counts only the number of Characters.
    200
    ~$

split Command: splitting files horizontally

If a file is too big, and it is difficult to edit it. In such cases, a file can be split using split command. By using this command we can split a file into many number of files .The resulting split files are created under current directory.
By default the system names the split files as xaa, xab, xac,...xzz (maximum of 676 files).The number of files generated depends upon the size of the original file and size of the split files.

Example


  1. ~$cat testfile1      // It display the content of testfile1
    this is line number one
    this is line number two
    this is line number three
    this is line number four
    this is line number five
    this is line number six
    this is line number seven
    this is line number eight
    this is line number nine
    ~$
  2. ~$ split -4 testfile1      // This command line split the testfile1 into three split files (xaa, xab & xac), each file contains at max four lines.
    ~$
    ~$ cat xaa      // It displays the content of split file xaa.
    this is line number one
    this is line number two
    this is line number three
    this is line number four
    ~$
    ~$ cat xab      // It displays the content of split file xab.
    this is line number five
    this is line number six
    this is line number seven
    this is line number eight
    ~$
    ~$xac      // It displays the content of split file xac.
    this is line number nine
    ~$
As you can see in the first example, the file testfile1 contains 9 lines of text. And in second example the command linesplit -4 testfile1 (Here -4 specifies the size of split files.) split testfile1 into three split files.
As i mentioned above, By default the system names the split files as xaa, xab, xac and others. You can include a primary tag as shown in the following example.

Example


  1. ~$ split -4 testfile1 test      // Here the third argument ( test ) specifies the primary tag. The names of the split files now will be testaa, testab & testac
    ~$

cmp Command: Comparing Two Files

Two files can be can be compared using the cmp command.

Example


  1. ~$ cmp testfile1 testfile2      // It simply returns the prompt. That means both the files testfile1 and testfile2 are identical.
    ~$
  2. ~$ cmp testfile1 testfile3      // The location of the first mismatch is echoed to the screen.
    byte 4, line 1
  3. ~$ cmp -l testfile1 testfile3      // Displays a detailed list of mismatch.
    4 145 141
    ~$

comm Command: Common in Two Files

The comm command compare two sorted files line by line. And it display the common entries in one column and differing entries in another column.

Example


  1. ~$ cat file1      // It display the contents of file1.
    aaa
    aab
    aac
    aad
    aae
    ~$
    ~$ cat file2       // It display the contents of file2.
    aaa
    aab
    aac
    aae
    aaf
    ~$
    ~$ comm file1 file2      // It display the common and differing entries of file1 and file2 in different columns..
                            aaa
                            aab
                            aac
    aad
                            aae
               aaf
    ~$
As you can see in the above example, the command line comm file1 file2 displays three column output. The first column shows one line unique to the first file(file1), and the second column shows the one line unique to the second file ( file2 ). The third column displays three lines common to both files.

diff Command: Differentiating And Converting

This command compares two files, line by line, and displays the difference, if any. For each block of text that differs between the two files it tells the user how the text from the first file has to be changed to match the text from the second or vice versa.

Example


  1. ~$diff file1 file2      // It prints out differences between file1 & file2. And also display some instruction code for the user.
    4d3                      // This code indicates, delete the fourth line in file1 so that it matches the line number three in file2.
    < aad
    5a5                      // This code indicates, append the line 5 from file2 after line 5 in file1.
    > aaf
    ~$
Here indicates lines of text belongs to first file. Lines of test belonging to second file are indicated by . Each block of text that differs is indicated by a short line ( --- ). Some codes like a, c & d helps you to modify the file so that it matches to other file. Here a stands for appendc stands for change & d stands for delete

gzip : Compressing files

You can compress one or more files using gzip command. When you run gzip on one or more filenames as arguments. It provides the extension .gz to the compressed filename and removes the original file.
The gzip command along with some options like -d & -dr can be used for decompressing the file or files.

Example


  1. ~$ wc file1
    5 5 20 file1             // 5 Lines, 5 words and 20 characters present in file1 before compressing.
    ~$ gizip file1           // It compress the file file1, provides the compressed file file.gz and removes the original file( file1)
    ~$
    ~$ wc file1.gz
    0 2 41 file1.gz            // 0 lines, 2 words & 41 characters present in file1 after get compressed
    ~$
  2. ~$ gzip file2 web.html           //compressing multiple files in one command line
    ~$
  3. ~$gzip -l file1 file2 web.html           // the -l gives the details of how much compression did we actually achieved.
    compresseduncompressedratiouncompressed_name
    412015.0%file1
    412015.0%file2
    786625.8%web.html
    160106-23.6%( toatals )
    ~$
  4. ~$ gzip -r *           //This command line compresses all the files present under the current directory
    ~$
  5. ~$ gzip -r MyDirectory        // Recursive compression ( -r ) option for compressing all files present in a specified directory.
    ~$
  6. ~$ gzip -d file1        // -d option for Decompressing the file
    ~$

gunzip : Decompressing Files

You can decompress one or more files using this command.

Example


  1. ~$gunzip file1 file2      // It decompress both files file1 & file2.
    ~$
  2. ~$gunzip -r *      // it decompresses all the files present under current directory
    ~$
  3. ~$gunzip -r MiDirectory      // it decompresses all the files present under a directory named MyDirectory.
    ~$

tar : Tape Archive

This command is used to archive group of files or an entire directory structure. To use tar you must need to know these following key options.
-c Create new archive
-x Extract files from archive
-t Display files in archive
-f(arch) Specify the archive arch

Example


  1. ~$tar -cf archfile1.tar file1 file2 web.html MyDirectory      // file1 , file2web.html & MyDirectory are archived to the file archfile1.tar.
    ~$
    --------------------------------------------------------------------------OR-----------------------------------------------------------------------------------
    ~$tar -cf archfile1.tar file1 file2 web.html MyDirectory      // Additionally we can use the -v(verbose) option to display the progress while tar works.
    file1
    file2
    web.html
    MyDirectory/
    ~$
  2. ~$tar -xf archfile1.tar      // It extracts all files from the archived file archfile1.tar
    ~$
    ~$tar -xf archfile1.tar file1      // It extracts only the specified file( file1 ) from the archived file archfile1.tar
    ~$
    --------------------------------------------------------------------------OR-----------------------------------------------------------------------------------
    ~$tar -xvf archfile1.tar
    file1
    file2
    web.html
    MyDirectory/
    ~$
  3. ~$tar -tf archfile1.tar      // The -t option is used to view the archive..
    file1
    file2
    web.html
    MyDirectory/
    ~$
    --------------------------------------------------------------------------OR-----------------------------------------------------------------------------------
    ~$tar -tvf archfile1.tar
    -rw-rw-r--       mypc/mypc       20       2014-08-31     12:21     file1
    -rw-rw-r--       mypc/mypc       20       2014-08-31     12:19     file2
    -rw-rw-r--       mypc/mypc       25       2014-08-31     13:21     web.html
    drwxrwxr-x       mypc/mypc  20240    2014-08-31     19:24     MyDirectory/
    ~$

The -z option: Compressing And Archiving together (and Decompressing And Extracting together )

Example


  1. ~$tar -czf archfile1.tar.gz file1 file2 web.html MyDirectory // It compresses and archives all the specified files to archfile1.tar.gz
    ~$
  2. ~$tar -xzf archfile1.tar.gz       //It Decompresses and Extracts files & Directories from archfile1.tar
    ~$

zip and unzip:

The zip Command combines the compressing function of gzip with archival function of tar. It performs both compressing and archiving functions. And Files can be restore using unzip command

Example


  1. ~$zip archfile2.zip file1 file2 MyDirectory      // Compressing and archiving files & directories using zip .
    adding:   file1(deflated 60%)
    adding:   file2(deflated 60%)
    adding:   MyDirectory/(stored 20%)
    ~$
  2. ~$unzip archfile2.zip      //Restoring files and directories using unzip .
    Archive :   archfile2.zip
    inflating :   file1
    inflating:   file2
    creating:   MyDirectory/
    ~$