Skip to content

Latest commit

 

History

History
233 lines (206 loc) · 4.09 KB

File metadata and controls

233 lines (206 loc) · 4.09 KB

View Content of the Files

We can view the content of a file by using the following commands:

  1. cat
  2. tac
  3. rev
  4. head
  5. tail
  6. less
  7. more

1. View Content of the File by using cat Command:

cat < file1.txt  

or

cat file1.txt  

< is optional.

cat < file1.txt  
This is first line  
This is second line  
This is third line  
This is extra line  
cat file1.txt  
This is first line  
This is second line  
This is third line  
This is extra line  
  • To include line numbers, use the -n option:
cat -n file1.txt  
 1 This is first line  
 2 This is second line  
 3 This is third line  
 4 This is extra line  
  • To skip blank lines, use the -b option:
cat -b file1.txt  
 1 This is first line  
 2 This is second line  
 3 This is third line  
 4 This is extra line  
  • To view content of multiple files:
cat file1.txt file2.txt file3.txt  

Note: The word cat is derived from con'cat'enation.

Various Utilities of cat Command:

  1. To create a new file with some content:
cat > filename  
data  
ctrl+d  
  1. To append data to an existing file:
cat >> filename  
extra data  
ctrl+d  
  1. To view the content of a file:
cat < filename  

or

cat filename  
  1. To copy the content of one file to another:
cat input.txt > output.txt  
  1. To copy the content of multiple files to a single file:
cat file1.txt file2.txt file3.txt > file4.txt # overwrite file4.txt content
cat file1.txt file2.txt file3.txt >> file4.txt # append to file4.txt  
  1. To append the content of one file to another:
cat file1.txt >> file2.txt  
  • cat command will display total file content at a time. It is best suitable for the small files, If the files contains thousand lines of content then it is not recommended to use cat command, For this requirement we should go for head, tail, more, less

2. tac Command:

  • It displays the file content in reverse order of lines (vertical reversal):
tac abc.txt  
akshay@Ubuntu:~/Desktop$ cat abc.txt
Java
Python 

Mango 
Berreis
Bananas

Devops
CI/CD

akshay@Ubuntu:~/Desktop$ tac abc.txt
CI/CD
Devops

Bananas
Berreis
Mango 

Python 
Java

3. rev Command:

  • It reverses each line content (horizontal reversal):
rev abc.txt  
akshay@Ubuntu:~/Desktop$ cat abc.txt
Java
Python 

Mango 
Berreis
Bananas

Devops
CI/CD
akshay@Ubuntu:~/Desktop$ rev abc.txt
avaJ
 nohtyP

 ognaM
sierreB
sananaB

spoveD
DC/IC

4. head Command:

  • To view the top few lines of the file:
head file1.txt  
  • Default: displays the top 10 lines.
  • To display the top 30 lines:
head -n 30 file1.txt  

or

head -30 file1.txt  
  • To display all lines except the last 20:
head -n -20 file1.txt  
  • In linux every character takes one byte
  • To display the first 100 bytes:
head -c 100 file1.txt  

5. tail Command:

  • To view the last few lines of the file:
tail file1.txt  
  • Default: displays the last 10 lines.
  • To display the last 30 lines:
tail -n 30 file1.txt  
# or  
tail -n -30 file1.txt
# or  
tail -30 file1.txt  
# All doing the same thing
  • To display lines from the 4th to the last:
tail -n +4 file1.txt  
  • To display 200 bytes from the bottom:
tail -c 200 file1.txt  

6. more Command:

  • To view file content page by page:
more file1.txt  
  • Controls:
    • Enter: next line
    • Space Bar: next page
    • q: quit

7. less Command:

  • To view the content page by page, with both forward and backward navigation:
less file1.txt  
  • Controls:
    • d: next page
    • b: previous page

Examples:

  • To display lines from the 3rd to 7th:
head -7 file1.txt | tail -5  
  • To display lines from the 21st to 30th:
head -30 file1.txt | tail -10