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
- ~$ 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.~$ - ~$ cat txtfile1 //Display the content of txtfile1.hi this is a Unix command to create a text file
and insert text into that file.~$ - ~$ cat web.html //Display the content of web.html file.< html >< head > well come to Unix </head >< body >< /body >< /html >~$
- ~$ 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.~$ - ~$ 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~$ - ~$ cat txtfile1 > txtfile2 // Overwrites the content of txtfile2 by the content of txtfile1.~$
- ~$ 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
- ~$cp srcfile destfile~$
- ~$cp srcfile MyDirectory / destfile1 //The file srcfile is copied into the file destfile1 which is under the directory MyDirectory.~$
- ~$cp srcfile MyDirectory //The file srcfile is copied into the directory MyDirectory with same file name,srcfile .~$
- ~$cp txtfile1 txtfile2 txtfile3 MyDirectory //All the three files txtfile1, txtfile2 & txtfile3 are copied into the directory MyDirectory~$
- ~$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 :
Example
- ~$cp -i srcfile destfilecp : overwrites `destfile' ? // hit y or Y to overwrite or hit any other key to avoid overwrite.
The Recursive ( -r ) option :
Example
- ~$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
- ~$ cat textfile1Hi this is the content of textfile1~$ mv textfile1 textfile2 // textfile1 is renamed as textfile2$ cat textfile1cat: textfile1: No such file or directory~$ cat textfile2Hi this is the content of textfile1~$
- ~$ mv -i textfile3 textfile2 // asks for the user permission to overwrite textfile2mv: 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
- ~$ mv textfile2 Desktop/Programs~$ ls Desktop/Programstextfile2~$
- ~$mv srcfile1 srcfile2 srcfile3 Desktop/Programsls Desktop/Programssrcfile1 srcfile2 srcfile3 textfile2~$
- ~$mv MyDirectory Desktop/Programs //The directory MyDirectory is moved to Programs directory.~$ls -F Desktop/Programssrcfile1* 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
- ~$rm srcfile1 srcfile2 // It silently removes both the files srcfile1 and srcfile2.~$
- ~$rm src* // It deletes all the files having a beginning pattern of src.~$
- ~$rm Desktop/Programs/samplefile1 // It deletes a file samplefile1 which is under the Programsdirectory.~$
- ~$rm -r srcDirectory // Usually directories are removed using rmdir but, you can also do the same by using rm along with the -r option.~$
- ~$rm -r * // The rm command with -r option removes all the files and sub-directories under the current directory.~$
- ~$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
- ~$wc srcfile13 40 200 srcfile1 // It shows that file srcfile1 has 3 Lines, 40 Words & 200 Characters.$
- ~$wc srcfile1 srcfile23 40 200 srcfile14 37 180 srcfile27 77 380 total~$
- ~$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
- ~$cat testfile1 // It display the content of testfile1this is line number onethis is line number twothis is line number threethis is line number fourthis is line number fivethis is line number sixthis is line number seventhis is line number eightthis is line number nine~$
- ~$ 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 onethis is line number twothis is line number threethis is line number four~$~$ cat xab // It displays the content of split file xab.this is line number fivethis is line number sixthis is line number seventhis 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
- ~$ 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
- ~$ cmp testfile1 testfile2 // It simply returns the prompt. That means both the files testfile1 and testfile2 are identical.~$
- ~$ cmp testfile1 testfile3 // The location of the first mismatch is echoed to the screen.byte 4, line 1
- ~$ 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
- ~$ cat file1 // It display the contents of file1.aaaaabaacaadaae~$~$ cat file2 // It display the contents of file2.aaaaabaacaaeaaf~$~$ comm file1 file2 // It display the common and differing entries of file1 and file2 in different columns..aaaaabaacaadaaeaaf~$
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
- ~$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.< aad5a5 // 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 append, c 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
- ~$ wc file15 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.gz0 2 41 file1.gz // 0 lines, 2 words & 41 characters present in file1 after get compressed~$
- ~$ gzip file2 web.html //compressing multiple files in one command line~$
- ~$gzip -l file1 file2 web.html // the -l gives the details of how much compression did we actually achieved.
compressed uncompressed ratio uncompressed_name 41 20 15.0% file1 41 20 15.0% file2 78 66 25.8% web.html 160 106 -23.6% ( toatals ) ~$ - ~$ gzip -r * //This command line compresses all the files present under the current directory~$
- ~$ gzip -r MyDirectory // Recursive compression ( -r ) option for compressing all files present in a specified directory.~$
- ~$ gzip -d file1 // -d option for Decompressing the file~$
gunzip : Decompressing Files
You can decompress one or more files using this command.
Example
- ~$gunzip file1 file2 // It decompress both files file1 & file2.~$
- ~$gunzip -r * // it decompresses all the files present under current directory~$
- ~$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
- ~$tar -cf archfile1.tar file1 file2 web.html MyDirectory // file1 , file2, web.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.file1file2web.htmlMyDirectory/~$
- ~$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.tarfile1file2web.htmlMyDirectory/~$
- ~$tar -tf archfile1.tar // The -t option is used to view the archive..file1file2web.htmlMyDirectory/~$--------------------------------------------------------------------------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.htmldrwxrwxr-x mypc/mypc 20240 2014-08-31 19:24 MyDirectory/~$
The -z option: Compressing And Archiving together (and Decompressing And Extracting together )
Example
- ~$tar -czf archfile1.tar.gz file1 file2 web.html MyDirectory // It compresses and archives all the specified files to archfile1.tar.gz~$
- ~$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
- ~$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%)~$
- ~$unzip archfile2.zip //Restoring files and directories using unzip .Archive : archfile2.zipinflating : file1inflating: file2creating: MyDirectory/~$
No comments:
Post a Comment