To View Files
The cat
command usage
The cat
command prints file contents to standard output (terminal) in forward order, while the tac
command prints contents in reverse order. In addition, when a shell command is executed, the system automatically opens three standard files: standard input (stdin), standard output (stdout), and standard error output (stderr), which handle input, output, and error messages for the process, respectively.
For example, we can view the file passwd
in the /etc
directory:
sudo cat /etc/passwd
You can add the -n
parameter to display the line number:
sudo cat -n /etc/passwd
labex:project/ $ sudo cat -n /etc/passwd
1 root:x:0:0:root:/root:/bin/bash
2 daemon:x:1:1:daemon:/usr/sbin:/usr/sbin/nologin
3 bin:x:2:2:bin:/bin:/usr/sbin/nologin
4 sys:x:3:3:sys:/dev:/usr/sbin/nologin
5 sync:x:4:65534:sync:/bin:/bin/sync
...
more
and less
The less
is a more advanced and versatile pager than the more
. It allows both forward and backward navigation within files, and does not require the entire input file to be read before starting, resulting in faster startups with large input files. It also makes less use of termcap (or terminfo on some systems), allowing it to run on a variety of terminals, and even provides limited support for hardcopy terminals. Its command interface is based on both more
and vi
, providing advanced functionality with the flexibility to use decimal numbers for certain commands.
Use more
to open the passwd
:
sudo more /etc/passwd
labex:project/ $ sudo more /etc/passwd
root:x:0:0:root:/root:/bin/bash
daemon:x:1:1:daemon:/usr/sbin:/usr/sbin/nologin
bin:x:2:2:bin:/bin:/usr/sbin/nologin
You can use Enter
to scroll down one line, Space
to scroll down one screen, h
to display the help, and q
to exit.
To Use head
and tail
to View a File
head
displays the first count lines or bytes of each of the specified files or the standard input if no files are selected. If count is omitted, it defaults to 10. tail
is similar to head
. The difference is that the tail
displays the last count of lines of files. For example, we want to view the information of new users, and, as we all know, the latest information must be at the end of the file. In this case, we can use tail
to view it:
sudo tail /etc/passwd
labex:project/ $ sudo tail /etc/passwd
usbmux:x:107:46:usbmux daemon,,,:/var/lib/usbmux:/usr/sbin/nologin
rtkit:x:108:113:RealtimeKit,,,:/proc:/usr/sbin/nologin
...