Wednesday, September 28, 2011

Linux File Handling Commands

1. cat : Concatenate Files

cat command is used to display the contents of a small file on terminal
Usage:
cat <file name>
Example:

$ cat sample3.txt
Unix (officially trademarked as UNIX, sometimes...


cat when supplied with more than one file will concatenate the files without any header information
Usage: 
$ cat sample3.txt sample4.txt


2. tac : concatenate files in reverse

tac command is used to display the contents of a small file in reverse order on terminal
Usage:
tac <file name>
Example:

$ tac sample3.txt
/*displays sample3.txt in reverse order*/


tac when supplied with more than one file will concatenate the reverse contents of files without any header information
Usage:

$ tac sample3.txt sample4.txt

3. more, less : paging output

more and less commands are used to view large files one page at a time
Usage:
more <file name>
less <file name>
Example:

$ more sample1.txt
/*sample1.txt will be displayed one page at a time */
$ less sample1.txt
/*sample1.txt will be displayed one page at a time */








less is the standard pager for linux and in general less is more powerful than more


4. wc : statistic of file

wc command is used to count lines, words and characters, depending on the option used.
Usage:
wc [options] [file name]
Example:

$ wc sample1.txt
65 2776 17333 sample1.txt
  • Which means sample1.txt file has 65 lines, 2776 words, and 17333 characters
  • you can just print number of lines, number of words or number of charcters by using following options:
    • -l : Number of lines
    • -w : Number of words
    • -c : Number of characters
5. cmp: comparing two files
cmp command is used to compare two files whether they are identical or not
Usage:
cmp <file1> <file2>
Example:
$ cmp sample1.txt sample2.txt
sample1.txt sample2.txt differ: byte 1, line 1
$ cmp sample1.txt sample1_copy.txt
$ /*No output prompt returns back*/
  • The two files are compared byte by byte and the location of the first mismatch is printed on the screen.
  • If two files are identical, then it doesnot print anything on the screen.
6. comm : what is common?
comm command displays what is common between both the files
Usage:
comm <file1> <file2>
Example:
$ comm sample5.txt sample6.txt
  • The input files to comm command should be sorted alphabetically.







No comments:

Post a Comment

Post a Comment