Linux Tips – How to Display the File Contents on Linux
This tutorial provides 14 tips to display the file contents on the linux terminal using 14 different commands. They are
- Using cat command
- Using lolcat command
- Using head command
- Using tail command
- Using echo command
- Using more command
- Using less command
- Using nano command
- Using nl command
- Using paste command
- Using xargs command
- Using awk command
- Using cut command
- Using grep command.
1. Displaying File Contents using Cat Command
- This command is manly used to create and display the file contents in linux / unix terminal
- This command will take only one argument which is the name of the file name.
Syntax
cat filename
Example
2. Displaying File Contents using lolcat Command
- This special command is used to display the file contents in multicoloring unlike cat command
- By default, this command is not installed on your machine. So you need to install this command based on your distribution before using this command.
Syntax
lolcat filename
Example
3. Displaying File Contents using Head Command
- This command is used to display the file contents on the terminal
- By default, it will show the top 10 lines of text file.
Syntax
head filename
Example
4. Displaying File Contents using Tail Command
- Like head command, this command is also used to display the file contents on the terminal
- By default, it will show the bottom 10 lines of text file.
Syntax
tail filename
Example
5. Displaying File Contents using Echo Command
- With help of command substitution, the echo command can be used to display the file contents
- The echo command is displaying the file contents using the input redirection operator named < from the input file.
- First, inputs from file can be obtained using the input operator < after that, it will be displayed on terminal using echo command via command substitution $(command)
- It is an important to note that, this method will show entire inputs into a single line unlike other commands
Syntax
echo $(< filename)
Example
6. Displaying File Contents using More Command
- This command is also used to display the file contents on the terminal
- This will take an input text file and show its contents in pager form, displaying file contents one screen at a time in case the file is too large.
Syntax
more filename
Example
7. Displaying File Contents using Less Command
- Like more command, this command is also used to display the file contents on the terminal
- Press q symbol from keyboard to exit from editor while using less command
Syntax
less filename
Example
8. Displaying File Contents using Nano Command
- This is actually editor used in linux / unix terminal
- This command will take a file as an input and display the file contents in its terminal window.
- press CTRL + x key, to exit from this editor
Syntax
nano filename
Example
9. Displaying File Contents using Nl Command
- This command is also used to display the file contents on the terminal with line numbers
- Unlike other commands, this command will additionally show the line numbers by default
Syntax
nl filename
Example
10. Displaying File Contents using Paste Command
- This command is actually joining the files horizontally
- This will also display the file contents if only one file will be given as the argument to this command (if single file is used in the paste command will show the contents of text file.)
Syntax
paste filename
Example
11. Displaying File Contents using Xargs Command
- This command is also used to display the file contents on the terminal with help of input redirection operator <
- Unlike other commands, this command will display the entire file contents into single line by default
- With special option -L1, we can show the file contents line by line. Here option -L1 adds a new line for each line. -L2 adds a new line for every two lines.
Syntax
xargs -L1 filename
Example
12. Displaying File Contents using Awk Command
- It is also possible to display the file contents using the awk command
- To print the file contents, use $0 inside the single quote followed by curly braces.
- $0 prints the contents line by line.
Syntax
awk ‘{print $0}’ filename
Example
13. Displaying File Contents using Cut Command
- Normally cut command is used to cut the specific column(s) of a text file
- With special option we can display the file contents through cut command
- Use the option cut command with option -c1- to display the file contents. Here 1- indicates cut contents from 1st column till the end.
Syntax
cut -c1- filename
14. Displaying File Contents using Grep Command
- Normally grep command can be used to search a pattern in the text file
- With special pattern, it is possible to display the file contents on the terminal using grep command
- The special pattern is “.*” which matches anything in the file, so while file comes out
Syntax
grep “.*” filename
Example