Viewing Files in Linux Using cat, more, and less


Introduction

Sometime in Linux, we do not want to open a file using vi or vim to see the content of file. Instead, we can use other Linux commands like ‘cat’, ‘more’ and ‘less’. Depending on the user requirement, either cat or more or less command can be used. Though there are many options or arguments for each command, but in this article we will try to explore the most commonly used options for each commands with some examples. We will start with most widely used command ‘cat’ and then move on to ‘more’ and ‘less’ command.

‘cat’ command

Approach 1: To view whole content of a file

Suppose there is one file [name cat-file.txt] in Linux and we want to view all the content of that file in screen. Then we can use below command

Example

$cat cat-file.txt

Output

Here
is
the content of
the file.
Thanks

Approach 2: To view whole content of a file with line number

Example

$cat –n cat-file.txt

Output

1  Here
2  is
3  the content of
4  the file.
5  Thanks

We have understood that cat command displays all the content of a file into screen even if the file has so much data. This maybe a limitations of cat commands. Let us see the other two commands.

‘more’ command

Now we need one large file to use more command. Let us create as fille name ‘more.txt’ where content is the history of the user.

$history > more.txt

Approach 1: To view the content of a large file

Example

$more more.txt

Output

1  wireshark --version
2  iw
3  iwconfig
4  iw wlp2s0 list
5  iwlist
6  iwlist  wlp2s0
7  iwlist  wlp2s0 s
8  iw
9  iw | grep -i monitor
10  iw list
11  iwconfig
12  iw wlp2s0 list
13  iw list
14  iw phy0 list
15  iw
16  iw dev phy0 list
17  iw dev wlp2s0 list
18  iwconfig
19  iw phy0 wlp2s0 list
20  iw phy0
21  iw phy0 info
22  iw phy0 list
23  iw list
24  iw phy0 info
25  sudo init 0
26  x11vnc display :0 --forever
27  iwconfig
--More--(3%)

Now this is the output of ‘more’ command for one screen and it’s 3% of the whole content.The more we press ‘enter key’ , we will see more content of the file like below output. We can use ‘spacebar key’ to go faster reading of the file.

Output

73  sudo kill -9 1136
74  sudo kill -9 1198
75  sudo apt install vim
76  sudo kill -9  2115
77  ps -aux | grep apt
78  sudo kill -9  2721
79  sudo kill -9  2717
80  ps -aux | grep apt
81  sudo apt install vim
82  ps -aux | grep apt
83  sudo apt install vim
84  ps aux | grep -i apt
85  sudo lsof /var/lib/dpkg/lock
86  sudo lsof /var/lib/apt/lists/lock
87  sudo lsof /var/cache/apt/archives/lock
88  sudo kill -9 2749
89  ps aux | grep -i apt
90  sudo apt install vim
91  vim ~/.bashrc
92  echo $SSLKEYLOGFILE
93  source ~/.bashrc
94  echo $SSLKEYLOGFILE
95  cat /home/rian/.ssl-key.log
96  touch /home/rian/.ssl-key.log
97  cat /home/rian/.ssl-key.log
98  sudo add-apt-repository ppa:ubuntu-mozilla-daily/firefox-aurora
99  sudo apt-get update
--More--(13%)

Approach 2: To view multiples files

Example

$ more more-file1.txt more-file2.txt

Output

::::::::::::::
more-file1.txt
::::::::::::::
I am more file 1
--More--(Next file: more-file2.txt)

If we press enter then we can see both files.

Output

::::::::::::::
more-file1.txt
::::::::::::::
I am more file 1
::::::::::::::
more-file2.txt
::::::::::::::
I am more file 2

‘less’ command

‘Less’ command is almost similar to ‘more’ command but it has more advantages.

Approach 1: To view the content of a large file

Example

$less more.txt

Output

1  wireshark --version
2  iw
3  iwconfig
4  iw wlp2s0 list
5  iwlist
6  iwlist  wlp2s0
7  iwlist  wlp2s0 s
8  iw
9  iw | grep -i monitor
10  iw list
11  iwconfig
12  iw wlp2s0 list
13  iw list
14  iw phy0 list
15  iw
16  iw dev phy0 list
17  iw dev wlp2s0 list
18  iwconfig
19  iw phy0 wlp2s0 list
20  iw phy0
21  iw phy0 info
22  iw phy0 list
23  iw list
24  iw phy0 info
25  sudo init 0
26  x11vnc display :0 --forever
27  iwconfig
more.txt

Now we may be thinking what the differences between more and less command is.

Therefore, some important differences are like

  • ‘less’ command is faster than ‘more’ commands .

  • ‘less’ command does not read whole content of a file unlike ‘more’ command

Approach 2: To view the content of a large file with line number

Example

$less –N more.txt

Output

Same output as last command but with line number.

Conclusion

From this article, we have learned about three powerful file viewing commands in Linux.

In addition, now we know that if the file is small use ‘cat’ command else we can use ‘more’ or ‘less’ command. Further, if the file size is huge then use ‘less’ command than ‘more’ command as ‘less’ command does not read whole content of file and it’s faster.

Updated on: 29-Mar-2023

667 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements