- Learn Linux Quickly
- Ahmed AlKabary
- 971字
- 2021-06-11 18:43:47
File viewing commands
In some cases, you may just want to view a file without editing it. While you can still use text editors like nano or vi to view files, there are much faster ways to view a file in Linux.
The cat command
The cat command is one of the most popular and frequently used commands in Linux. The cat (short for concatenate) command concatenates and prints files to the standard output (terminal).
To view the facts.txt file that we created, you can run the cat facts.txt command:
elliot@ubuntu-linux:~$ cat facts.txt
Apples are red.
Grapes are green.
Bananas are yellow.
Cherries are red.
Sky is high.
Earth is round.
Linux is awesome!
Cherries are red.
Cherries are red.
Cherries are red.
You can now view the contents of the file facts.txt from the comfort of your terminal without having to open any text editor.
The cat command can do more than just viewing a file. It can also concatenate (put together) files. To demonstrate, create the following three files with your favorite text editor:
- file1.txt (Insert the line "First File")
- file2.txt (Insert the line "Second File")
- file3.txt (Insert the line "Third File")
Now let's view each of the three files using the cat command:
elliot@ubuntu-linux:~$ cat file1.txt
First File
elliot@ubuntu-linux:~$ cat file2.txt
Second File
elliot@ubuntu-linux:~$ cat file3.txt
Third File
Now let's concatenate both file1.txt and file2.txt together by running the cat file1.txt file2.txt command:
elliot@ubuntu-linux:~$ cat file1.txt file2.txt
First File
Second File
We can also concatenate all three files:
elliot@ubuntu-linux:~$ cat file1.txt file2.txt file3.txt
First File
Second File
Third File
Keep in mind that order matters; for example, running the cat file2.txt file1.txt command:
elliot@ubuntu-linux:~$ cat file2.txt file1.txt
Second File
First File
This will output the text in file2.txt first before file1.txt.
The tac command
The tac command is the twin brother of the cat command. It is basically cat written in reverse, and it does the same thing as the cat command but in a reversed fashion!
For example, if you want to view the facts.txt file in reverse order, you can run the tac facts.txt command:
elliot@ubuntu-linux:~$ tac facts.txt
Cherries are red.
Cherries are red.
Cherries are red.
Linux is awesome!
Earth is round.
Sky is high.
Cherries are red.
Bananas are yellow.
Grapes are green.
Apples are red.
The tac command also concatenates files, just like the cat command.
The more command
Viewing files with the cat command is a good choice when the file is small, and there aren't many lines of text to display. If you want to view a big file, it's better to use the more command. The more command displays the content of a file one page at a time; it is basically a paging program.
Let's view the contents of the file /etc/services with the more command:
elliot@ubuntu-linux:~$ more /etc/services
# Network services, Internet style
# Note that it is presently the policy of IANA to assign a single well-known
# port number for both TCP and UDP; hence, officially ports have two entries
# even if the protocol doesn't support UDP operations.
tcpmux 1/tcp # TCP port service multiplexer
systat 11/tcp users
netstat 15/tcp ftp 21/tcp
fsp 21/udp fspd
ssh 22/tcp # SSH Remote Login Protocol
telnet 23/tcp
smtp 25/tcp mail
whois 43/tcp nicname
tacacs 49/tcp # Login Host Protocol (TACACS)
tacacs 49/udp
--More--(7%)
It will show you the first page of the /etc/services files, and there is a percentage value at the bottom line that shows how far you have progressed through the file. You can use the following keys to navigate in more:
- Enter > to scroll down one line.
- Space Bar > to go to the next page.
- b > to go back one page.
- q > to quit.
The /etc/services file stores information on numerous services (applications) that can run on Linux.
The less command
The less command is an improved version of the more command. Yes, you read this correctly; less is better than more! In fact, the famous idiom less is more originated from the idea that less offers more than more.
The less command is another pager program, just like more; it allows you to view text files one page at a time. The advantage of less is that you can use the UP/DOWN arrow keys to navigate through the file. Also, less is faster than more.
You can view the /etc/services file with less by running the command:
elliot@ubuntu-linux:~$ less /etc/services
You can also use more navigation keys with less.
Heads or tails?
As its name suggests, the head command displays the first few lines of a file. By default, it shows the first ten lines of a file. For example, we know that facts.txt has ten lines in it, and so running the head facts.txt command will display all the file contents:
elliot@ubuntu-linux:~$ head facts.txt
Apples are red.
Grapes are green.
Bananas are yellow.
Cherries are red.
Sky is high.
Earth is round.
Linux is awesome!
Cherries are red.
Cherries are red.
Cherries are red.
You can also pass the -n option to specify the number of lines you wish to view. For example, to display the first three lines of facts.txt, you can run the head -n 3 facts.txt command:
elliot@ubuntu-linux:~$ head -n 3 facts.txt
Apples are red.
Grapes are green.
Bananas are yellow.
On the other hand, the tail command displays the last few lines of a file. By default, it shows the last ten lines. You can also use the -n option to specify the number of lines you wish to view. For example, to display the last two lines in facts.txt, you can run the tail -n 2 facts.txt command:
elliot@ubuntu-linux:~$ tail -n 2 facts.txt
Cherries are red.
Cherries are red.
Do you know what time it is? It's time for some knowledge check questions.
- DBA攻堅指南:左手Oracle,右手MySQL
- 云原生Spring實戰
- Oracle數據庫從入門到運維實戰
- Mastering Yii
- Learning Network Forensics
- QGIS:Becoming a GIS Power User
- C#程序設計
- Gradle for Android
- Highcharts Cookbook
- 自然語言處理Python進階
- Kotlin從基礎到實戰
- C#應用程序設計教程
- IoT Projects with Bluetooth Low Energy
- Penetration Testing with the Bash shell
- Modernizing Legacy Applications in PHP