We can view the content of a file by using the following commands:
cattacrevheadtaillessmore
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
-noption:
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
-boption:
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.
- To create a new file with some content:
cat > filename
data
ctrl+d - To append data to an existing file:
cat >> filename
extra data
ctrl+d - To view the content of a file:
cat < filename or
cat filename - To copy the content of one file to another:
cat input.txt > output.txt - 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 - To append the content of one file to another:
cat file1.txt >> file2.txt catcommand 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 forhead,tail,more,less
- 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- 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- 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 - 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 - To view file content page by page:
more file1.txt - Controls:
Enter: next lineSpace Bar: next pageq: quit
- To view the content page by page, with both forward and backward navigation:
less file1.txt - Controls:
d: next pageb: previous page
- 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