Unexpectedly, There Are So Many Commands to View File Contents in Linux

Today, I would like to introduce some commonly used commands for viewing file contents that I often use, hoping they will be helpful to everyone.

1. head Command

The head command is used to read the first ten lines of a given filename. The basic syntax of the head command is:

head [options] [file(s)]

For example, the following command will display the first ten lines of the file named ‘/etc/passwd’.

# head /etc/passwd 

root:x:0:0:root:/root:/bin/bash 
daemon:x:1:1:daemon:/usr/sbin:/bin/sh 
bin:x:2:2:bin:/bin:/bin/sh 
sys:x:3:3:sys:/dev:/bin/sh 
sync:x:4:65534:sync:/bin:/bin/sync 
games:x:5:60:games:/usr/games:/bin/sh 
man:x:6:12:man:/var/cache/man:/bin/sh 
lp:x:7:7:lp:/var/spool/lpd:/bin/sh 
mail:x:8:8:mail:/var/mail:/bin/sh 
news:x:9:9:news:/var/spool/news:/bin/sh

If multiple files are provided, the head command will display the first ten lines of each file separately. For example, the following command will show ten lines from each file.

# head /etc/passwd /etc/shadow

==> /etc/passwd <== 
root:x:0:0:root:/root:/bin/bash 
bin:x:1:1:bin:/bin:/sbin/nologin 
daemon:x:2:2:daemon:/sbin:/sbin/nologin 
adm:x:3:4:adm:/var/adm:/sbin/nologin 
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin 
sync:x:5:0:sync:/sbin:/bin/sync 
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown 
halt:x:7:0:halt:/sbin:/sbin/halt 
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin 
uucp:x:10:14:uucp:/var/spool/uucp:/sbin/nologin

==> /etc/shadow <== 
root:$6$85e1:15740:0:99999:7:::
bin:*:15513:0:99999:7:::
daemon:*:15513:0:99999:7:::
adm:*:15513:0:99999:7:::
lp:*:15513:0:99999:7:::
sync:*:15513:0:99999:7:::
shutdown:*:15513:0:99999:7:::
halt:*:15513:0:99999:7:::
mail:*:15513:0:99999:7:::
uucp:*:15513:0:99999:7:::

If you need to retrieve more than the default ten lines, use the ‘-n’ option with a specified integer to tell it how many lines to retrieve. For example, the following command will display the first 5 lines of the file ‘/var/log/yum.log’.

# head -n5 /var/log/yum.log

Jan 10 00:06:49 Updated: openssl-1.0.1e-16.el6_5.4.i686
Jan 10 00:06:56 Updated: openssl-devel-1.0.1e-16.el6_5.4.i686
Jan 10 00:11:42 Installed: perl-Net-SSLeay-1.35-9.el6.i686
Jan 13 22:13:31 Installed: python-configobj-4.6.0-3.el6.noarch
Jan 13 22:13:36 Installed: terminator-0.95-3.el6.rf.noarch

2. tail Command

The tail command is the opposite of the head command, used to read the last ten lines of a given filename. The basic syntax of the tail command is:

tail [options] [filenames]

For example, the following command will display the last ten lines of the file named ‘/etc/passwd’.

# tail /etc/passwd 

Debian-exim:x:115:122::/var/spool/exim4:/bin/false
statd:x:111:65534::/var/lib/nfs:/bin/false
sshd:x:112:65534::/var/run/sshd:/usr/sbin/nologin 
backup:x:34:34:backup:/var/backups:/bin/bash 
ntp:x:112:120::/home/ntp:/bin/false
messagebus:x:113:125::/var/run/dbus:/bin/false
landscape:x:114:125::/var/lib/landscape:/bin/false
pollinate:x:115:1::/var/cache/pollinate:/bin/false
md5:x:1000:1000:MD5 User,,,:/home/md5:/bin/bash 

Similarly, if multiple files are provided, the tail command will display the last ten lines of each file separately. For example, the following command will show ten lines from each file.

# tail /etc/passwd /etc/shadow

==> /etc/passwd <== 
Debian-exim:x:115:122::/var/spool/exim4:/bin/false
sshd:x:112:65534::/var/run/sshd:/usr/sbin/nologin 
backup:x:34:34:backup:/var/backups:/bin/bash 
ntp:x:112:120::/home/ntp:/bin/false
messagebus:x:113:125::/var/run/dbus:/bin/false
landscape:x:114:125::/var/lib/landscape:/bin/false
pollinate:x:115:1::/var/cache/pollinate:/bin/false
md5:x:1000:1000:MD5 User,,,:/home/md5:/bin/bash 
mysql:x:116:124:MySQL Server,,,:/nonexistent:/bin/false
ntp:x:117:123:NTP daemon,,,:/home/ntp:/bin/false

==> /etc/shadow <== 
root:$6$2uo5:17373:0:99999:7:::
backup:*:17371:0:99999:7:::
ntp:*:17371:0:99999:7:::
sshd:*:17371:0:99999:7:::
messagebus:*:17371:0:99999:7:::
landscape:*:17371:0:99999:7:::
pollinate:*:17371:0:99999:7:::
md5:$6$51ev:17371:0:99999:7:::
mysql:!:17392:0:99999:7:::
tp:!:17392:0:99999:7:::

Like the head command, if you need to retrieve more than the default ten lines, use the ‘-n’ option with a specified integer to tell it how many lines to retrieve. For example, the following command will display the last 20 lines of the file ‘/var/log/yum.log’.

# tail -n20 /var/log/yum.log

Jan 16 00:05:30 Updated: fipscheck-lib-1.4.1-6.el7.x86_64 
Jan 16 00:05:31 Updated: libselinux-utils-2.5-15.el7.x86_64 
Jan 16 00:05:31 Updated: python-slip-dbus-0.4.0-4.el7.noarch 
Jan 16 00:05:31 Updated: mlterm-common-3.8.7-7.el7.x86_64 
Jan 16 00:05:31 Updated: libselinux-python-2.5-15.el7.x86_64 
Jan 16 00:05:31 Updated: libreport-plugin-rhtsupport-2.1.11-43.el7.x86_64 
Jan 16 00:05:31 Updated: libXpm-3.5.12-1.el7.x86_64 
Jan 16 00:05:32 Updated: libnl3-cli-3.2.21-10.el7.x86_64 
Jan 16 00:05:33 Updated: runc-1.0.0-57.rc8.git9abbd9b.el7.x86_64 
Jan 16 00:05:33 Updated: open-vm-tools-11.2.5-2.el7.x86_64 
Jan 16 00:05:33 Updated: qemu-guest-agent-4.2.0-31.el7_9.x86_64 
Jan 16 00:05:34 Updated: 1:vixie-cron-4.2-25.el7.x86_64 
Jan 16 00:05:34 Updated: libXfont-1.5.4-4.el7.x86_64 
Jan 16 00:05:34 Updated: libreport-filesystem-2.1.11-43.el7.x86_64 
Jan 16 00:05:34 Updated: libXv-1.0.11-1.el7.x86_64 
Jan 16 00:05:34 Updated: libjpeg-turbo-1.2.90-8.el7.x86_64 
Jan 16 00:05:34 Updated: gnutls-dane-3.3.29-9.el7.x86_64 
Jan 16 00:05:34 Updated: libreport-web-2.1.11-43.el7.noarch 
Jan 16 00:05:36 Updated: 1:java-1.8.0-openjdk-headless-1.8.0.272.b10-1.el7_9.x86_64 
Jan 16 00:05:36 Updated: tuned-profiles-oracle-2.11.0-8.el7.noarch 

Note that the tail command can also use the ‘-f’ option to track changes to a file in real-time, displaying the latest content. For example, the following command will display new content from the file ‘/var/log/messages’ in real-time.

# tail -f /var/log/messages

3. more Command

The more command is used to display file contents one page at a time. The basic syntax of the more command is:

more [options] [filenames]

For example, the following command will display the contents of the file named ‘/var/log/messages’ one page at a time.

# more /var/log/messages

Using the more command, you can view the file contents line by line by pressing the Enter key. To scroll down to the next page, press the spacebar. To exit the more command, press the ‘q’ key.

The more command also has some other useful options:

  • ‘-num’: Sets the number of lines displayed at once to num lines. For example, using the command below will display 15 lines per page.
# more -15 /var/log/messages
  • ‘+num’: Starts displaying from line num of the file. For example, using the command below will start displaying content from line 20 of the file.
# more +20 /var/log/messages
  • ‘/pattern’: Searches the file content for a specified pattern and locates the first matching line. For example, using the command below will find lines containing ‘error’.
# more /var/log/messages
/error

In the more command, you can combine the above options for more complex operations. For example, the following command will start displaying content from line 20 of the file and display 10 lines at a time.

# more +20 -10 /var/log/messages

4. less Command

The less command is also used to display file contents one page at a time, but it allows scrolling both up and down and searching for keywords in the file content. The basic syntax of the less command is:

less [options] [filenames]

For example, the following command will display the contents of the file named ‘/var/log/messages’ one page at a time.

# less /var/log/messages

When using the less command, you can use the following keyboard commands:

  • Scroll down one page: press the spacebar
  • Scroll up one page: press the ‘b’ key
  • Scroll down one line: press the ‘j’ key
  • Scroll up one line: press the ‘k’ key
  • Highlight search keywords: press ‘/’ and then enter the keyword to search (press ‘n’ to go to the next match)
  • Exit the less command: press ‘q’ key

The less command also supports the above options, for example, using the ‘-num’ option to display num lines at once.

# less -15 /var/log/messages

Using the ‘+num’ option to start displaying from line num of the file.

# less +20 /var/log/messages

Using the ‘/pattern’ option to search for a specified pattern in the file content.

# less /var/log/messages
/error

In the less command, you can combine the above options for more flexible operations.

Here is a list of websites.

cat urls.txt
https://google.com
https://twitter.com
https://linkedin.com

5. less Command

The less command can also be used with the output of other commands. For example, the following command will use the pipe operator to pass the output of the ‘cat urls.txt’ command to the less command for page-by-page viewing.

cat urls.txt | less

This will open a less window where you can use the less command mentioned above to view the output content. You can press the spacebar to scroll down one page, press ‘b’ to scroll up one page, press ‘/’ to search for keywords, and press ‘q’ to exit the view.

6. cat Command

The cat command is a widely used general-purpose tool. It copies standard input to standard output.

The basic syntax of the cat command is:

cat [options] [filenames] [-] [filenames]

<span>cat</span> is most commonly used to read the contents of a file. To open a file for reading, simply type cat followed by a space and the filename.

# cat /etc/passwd 

root:x:0:0:root:/root:/bin/bash 
daemon:x:1:1:daemon:/usr/sbin:/bin/sh 
bin:x:2:2:bin:/bin:/bin/sh 
sys:x:3:3:sys:/dev:/bin/sh 
sync:x:4:65534:sync:/bin:/bin/sync 
games:x:5:60:games:/usr/games:/bin/sh 
man:x:6:12:man:/var/cache/man:/bin/sh 
lp:x:7:7:lp:/var/spool/lpd:/bin/sh 
…

The cat command can also be used to concatenate multiple files together.

# echo 'Hi devops-Team' &gt; 1 
# echo 'Keep connected' &gt; 2 
# echo 'Share your thought' &gt; 3 
# echo 'connect us [email protected]' &gt; 4
# cat 1 2 3 4 &gt; 5
# cat 5 

Hi devops-Team 
Keep connected 
Share your thought 
connect us [email protected]

It can also be used to create files. This can be done by executing cat followed by the output redirection operator and the name of the file to be created.

# cat &gt; devops.txt

devops is the only website fully dedicated to Linux.

We can customize the end symbol for the ‘cat’ command. Here we implement it.

# cat &gt; test.txt &lt;&lt; end 

I am Avishek 
Here I am writing this post 
Hope you are enjoying 
end
# cat test.txt 

I am Avishek 
Here I am writing this post 
Hope you are enjoying

It can also be used to copy files.

# cat name.txt

I am a Programmer by birth and Admin by profession
# cat name.txt &gt; name1.txt
# cat name1.txt

I am a Programmer by birth and Admin by profession

[Linux 250 Common Commands Quick Reference Manual] Follow [Development Operations Architecture], reply ‘1001’ in the background to get it.

Unexpectedly, There Are So Many Commands to View File Contents in Linux

END

Source: Shishan CodeCopyright belongs to the original author. If there is any infringement, please contact for deletion..Recommended ReadingThe company used pirated AD software and received a lawyer’s letter again…In the future, if your code is written quickly and well, you are a fool!!!Clearly hardware is harder than software, why are hardware engineers’ salaries not as high as software?→ Follow to avoid getting lost ←

Leave a Comment