List File Contents

 List File Contents 


  • head-It displays the beginning of a file.
  • tail-It displays the last last part of a file.
  • cat-This command is versatile and multi worker.
  • tac-Opposite of cat.
  • more-Command line diaplays contents in pager form that is either in more format.
  • less-Command line diaplays contents in pager form that is either in less format.

--------------------------------------------------------------------------------------------------------------------------------------------------------------------------

HEAD


  • The 'head' command displays the starting content of a file. 
  • By default, it displays starting 10 lines of any file.

[haritaraka12078@cxln4 ~]$ head jtp.txt
hellllllllllllllllllooooooooooooo
allllllllllllllll
goodmorning
thank you
welcome
software engineer
hari taraka prabhu manyam
you are welcome
linux weindows operating systems
quality engineer


-------------------------------------------------------------------------------
Head command for multiple files
-------------------------------------------------------------------------------
If we'll write two file names then it will display first ten lines (in this case file has five lines only) of each file separated by a heading.

[haritaraka12078@cxln4 ~]$ head jtp.txt marks.txt
==> jtp.txt <==
hellllllllllllllllllooooooooooooo
allllllllllllllll
goodmorning
thank you
welcome
software engineer
hari taraka prabhu manyam
you are welcome
linux weindows operating systems
quality engineer

==> marks.txt <==
hellllllllllllllllllooooooooooooo
allllllllllllllll
goodmorning
thank you
welcome
software engineer
hari taraka prabhu manyam
you are welcome
linux weindows operating systems
quality engineer


-------------------------------------------------------------------------------
head -n
-------------------------------------------------------------------------------
The 'head -n' option displays specified number of lines.


[haritaraka12078@cxln4 ~]$ head -5 jtp.txt
hellllllllllllllllllooooooooooooo
allllllllllllllll
goodmorning
thank you
welcome



-------------------------------------------------------------------------------
head -c
The 'head -c' command counts the number of bytes of a file
-------------------------------------------------------------------------------.
[haritaraka12078@cxln4 ~]$ head -c 4 jtp.txt




--------------------------------------------------------------------------------------------------------------------------------------------------------------------------

TAIL


  • Linux tail command is used to display the last ten lines of one or more files. Its main purpose is to read the error message.

[haritaraka12078@cxln4 ~]$ tail jtp.txt
thank you
welcome
software engineer
hari taraka prabhu manyam
you are welcome
linux weindows operating systems
quality engineer
big data hadoop tester
goiod afternoon
happy new year


[haritaraka12078@cxln4 ~]$ tail -n 5 jtp.txt
linux weindows operating systems
quality engineer
big data hadoop tester
goiod afternoon
happy new year



[haritaraka12078@cxln4 ~]$ tail -n 5 jtp.txt marks.txt
==> jtp.txt <==
linux weindows operating systems
quality engineer
big data hadoop tester
goiod afternoon
happy new year

==> marks.txt <==
linux weindows operating systems
quality engineer
big data hadoop tester
goiod afternoon
happy new year



[haritaraka12078@cxln4 ~]$ tail -c 6 marks.txt
 year


[haritaraka12078@cxln4 ~]$ tail -f jtp.txt
thank you
welcome
software engineer
hari taraka prabhu manyam
you are welcome
linux weindows operating systems
quality engineer
big data hadoop tester
goiod afternoon
happy new year


-----------------------------------------------------------------------------------------------------------
Tail with other commands
-----------------------------------------------------------------------------------------------------------
The tail command can be used with other commands. It can be piped to other commands to filter the output. Consider the below command:


[haritaraka12078@cxln4 ~]$ ls -t /etc | tail -n 6
services
mailcap
mime.types
ld.so.conf
rpc
Trolltech.conf



--------------------------------------------------------------------------------------------------------------------------------------------------------------------------

MORE


  • As 'cat' command displays the file content. Same way 'more' command also displays the content of a file. 
  • Only difference is that, in case of larger files, 'cat' command output will scroll off your screen while 'more' command displays output one screenful at a time.

Following keys are used in 'more' command to scroll the page:

Enter key     --------------->> To scroll down page line by line.
Space bar     --------------->>  To go to next page.
b key             --------------->> To go to the backward page.
/ key             --------------->>  Lets you search the string.


Syntax:    more <file name>  


  • To scroll down use 'space' button, next page will be displayed.
  • If you want to scroll down the page line by line use 'enter' key.
  • If you want to go to the last or backward page use 'b' key.


---------------------------------------------------------------------------------------------
Options                                 Function
---------------------------------------------------------------------------------------------
more -num                     Limits the line displayed per page.
more -d                             Displays user message at right corner.
more -s                             Squeeze blank lines.
more +/string name             It helps to find the string.
more +num                     Used to display the content from a specific line.


Note: The 'more' command can't be used to display binary files.



--------------------------------------------------------------------------------------------------------------------------------------------------------------------------

LESS


  • The 'less' command is same as 'more' command but include some more features.
  • It automatically adjust with the width and height of the teminal window, 
  • while 'more' command cuts the content as the width of the terminal window get shorter.

less <file name>  

----------------------------------------------------------------------------------------------------
Linux To Find A String
----------------------------------------------------------------------------------------------------
A particular string is searched by typing forward slash (/) and then followed by the string name, at place of file name in the lower left corner of the terminal box.

Syntax:    /string name  
Example:   /add  



----------------------------------------------------------------------------------------------------
less Command Navigation Keys
----------------------------------------------------------------------------------------------------
The 'less' command have navigation keys similar to the 'vim' editor. Let's have a look on some of the navigation keys and other operations of less command.


1. Search Navigation
search navigtion keys will help you in forward and backward search.


Forward search
  • / : search for a pattern for the next occurence
  • n : search for next match
  • N : search for previous match


Backward search

  • ? : search for a pattern for the next occurence
  • n : for next match in backward direction
  • N : for previous match in forward direction


Search path

  • Forward : /\/home\/sssit\/
  • Backward : /home/sssit


2. Screen Navigation
  • Ctrl + f : forwards one window
  • Ctrl + d : forwards half window
  • Ctrl + b : backwards one window
  • Ctrl + u : backwards half window


3. Line Navigation

to move forward or backward line by line
  • j : forward by one line
  • k : backward by one line


4. other Navigations
  • G : used to go to end of the file
  • g : used to go to start of the file
  • q or ZZ : to exit


5. Count Keys
  • 10j : 10 lines forward
  • 10k : 10 lines backward


6. Marked Navigation
  • ma : mark current position with letter 'a'
  • a : go to marked position


7. Multiple File

  • by passing arguments in the same line
less file1 file2  

  • while viewing file1 go to file2
less file1  
      
:e file2  


  • to navigate between files when more then two files are opened
n : go to next file
p : go to previous file



--------------------------------------------------------------------------------------------------------------------------------------------------------------------------

CAT



  • The 'cat' command is the most universal and powerful tool. 
  • It is considered to be one of the most frequently used commands.
  •  It can be used to display the content of a file, copy content from one file to another, concatenate the contents of multiple files, display the line number, display $ at the end of the line, etc.
 
 
cat > [fileName]                                         To create a file.
cat [oldfile] > [newfile]                                 To copy content from older to new file.
cat [file1 file2 and so on] > [new file name] To concatenate contents of multiple files into one.
cat -n/cat -b [fileName]                                 To display line numbers.
cat -e [fileName]                                         To display $ character at the end of each line.
cat [fileName] <<EOF                                 Used as page end marker.



ritaraka12078@cxln4 ~]$ cat>exm.txt
apple is red
mango is yellow
your dress color is red
black color suits you all


[haritaraka12078@cxln4 ~]$ cat>>exm.txt
123456789


[haritaraka12078@cxln4 ~]$ cat exm.txt
apple is red
mango is yellow
your dress color is red
black color suits you all
123456789


[haritaraka12078@cxln4 ~]$ cat exm.txt>exm2.txt
[haritaraka12078@cxln4 ~]$ cat exm2.txt
apple is red
mango is yellow
your dress color is red
black color suits you all
123456789




[haritaraka12078@cxln4 ~]$ cat site.txt
sunday monday tuesday wednesday thursday friday saturday

[haritaraka12078@cxln4 ~]$ cat exm.txt site.txt >combo

[haritaraka12078@cxln4 ~]$ cat combo
apple is red
mango is yellow
your dress color is red
black color suits you all
123456789
sunday monday tuesday wednesday thursday friday saturday




[haritaraka12078@cxln4 ~]$ cat - exm.txt site.txt >combo
9874563212356

[haritaraka12078@cxln4 ~]$ cat combo
9874563212356
apple is red
mango is yellow
your dress color is red
black color suits you all
123456789
sunday monday tuesday wednesday thursday friday saturday




[haritaraka12078@cxln4 ~]$ cat -n combo
     1  9874563212356
     2  apple is red
     3  mango is yellow
     4  your dress color is red
     5  black color suits you all
     6  123456789
     7  sunday monday tuesday wednesday thursday friday saturday



[haritaraka12078@cxln4 ~]$ cat -b combo
     1  9874563212356
     2  apple is red
     3  mango is yellow
     4  your dress color is red
     5  black color suits you all
     6  123456789
     7  sunday monday tuesday wednesday thursday friday saturday



[haritaraka12078@cxln4 ~]$ cat -e exm.txt
apple is red$
mango is yellow$
your dress color is red$
black color suits you all$
123456789$



[haritaraka12078@cxln4 ~]$ cat>exm.txt<<EOF
> apple is red$
> mango is yellow$
> your dress color is red$
> black color suits you all$
> 123456789$
> -bash: warning: here-document at line 141 delimited by end-of-file (wanted `EOF')



[haritaraka12078@cxln4 ~]$ cat weeks.txt | tac | cat | cat | tac
sunday
tuesday
wednesday
saturday
sunday
tuesday
wednesday
saturday


--------------------------------------------------------------------------------------------------------------------------------------------------------------------------

TAC



  • The 'tac' command is the reverse of the 'cat' command. 
  • It is also known as 'cat' backward. It will display the file content in reverse order. 
  • It prints the last line first, then second last and so on. Such way, it prints the first line at last.


[haritaraka12078@cxln4 ~]$ cat file2.txt
dhoni
dravid
sachin
zadeja


[haritaraka12078@cxln4 ~]$ tac file2.txt
zadeja
sachin
dravid
dhoni



[haritaraka12078@cxln4 ~]$ tac exm.txt --separator "red"
$
black color suits you all$
123456789$
$
mango is yellow$
your dress color is redapple is red



[haritaraka12078@cxln4 ~]$ tac exm.txt --separator "yellow"
$
your dress color is red$
black color suits you all$
123456789$
apple is red$
mango is yellow


tac exm.txt --separator "all"
$
123456789$
apple is red$
mango is yellow$
your dress color is red$
black color suits you all [haritaraka12078@cxln4 ~]$