Friday, 29 May 2015

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/
    ~$


No comments:

Post a Comment