6 Ways to View Linux File Content

  • 5,000
  • Tác giả: admin
  • Ngày đăng:
  • Lượt xem: 5
  • Tình trạng: Còn hàng

One of the most important ways you can make use of your Linux PC is through files.

Almost every tệp tin that you directly work on in the Linux terminal can be manipulated as a text tệp tin. This includes configuration files, log files about various services and processes in the system, and script files that come bundled with the Linux Distribution of your choice or which you write yourself.

Knowing how vĩ đại quickly view the nội dung of these files in the Linux terminal is an invaluable skill that not only saves you time but also helps you learn more about the system. This tutorial will show you how vĩ đại view these kinds of files in the Linux terminal.

cat

The cat command is a simple and extremely useful command for viewing tệp tin contents on Linux.

You can use the cát command vĩ đại quickly print tệp tin nội dung vĩ đại the standard output in the terminal or alternatively concatenate the output.

The syntax for the cát command is as follows:

$ cát [OPTION] [FILE1] …

For example, if you have a tệp tin called simple-list.txt, which contains a list of some items, you can view the contents of this tệp tin by using the command

$ cát simple-list.txt

For example:

$ cát simple-list.txt 
bananas 
strawberries 
grapes 
apples 
watermelons 
oranges 
blueberries 
lemons 
peaches 
avocados 
pineapples 
cherries 
cantaloupe 
raspberries 
pears 
limes 
blackberries 
clementine 
mangoes 
plums 

You can also list multiple files at the same time vĩ đại get the output of all files in the terminal at once, as shown below:

$ cát testfile1.txt testfile2.txt 
these are the contents of testfilel.txt. 
and this is the over of testfilel.txt 
these are the contents of testfile2.txt. 
and this is the over of testfile2.txt

nl

If you want vĩ đại be able vĩ đại see the line number for each line in a tệp tin, the nl command does exactly that. You can use this command in the same way as the cát command, and the only difference is that nl has line numbers enabled by mặc định.

Using the cát command, you can get the same result with the -e option. Here is an example output of the same simple-list.txt tệp tin output with nl

$ nl simple-list.txt 
1 bananas 
2 strawberries 
3 grapes 
4 apples 
5 watermelons
6 oranges 
7 blueberries 
8 lemons 
9 peaches 
10 avocados 
11 pineapples 
12 cherries 
13 cantaloupe 
14 raspberries 
15 pears 
16 limes 
17 blackberries 
18 clementine 
19 mangoes 
20 plums 

The line numbers can be changed vĩ đại be left justified by using the -nln option. There are other options as well you can use with the nl command, listed under man nl

more

So far, you have needed vĩ đại scroll through the output using your mouse scroll wheel or Shift+Page-up and Shift+Page-down buttons on the keyboard.

However, more command provides a much easier way vĩ đại view longer files that vì thế not fit completely within the terminal window.

The same simple-list.txt tệp tin is shown when viewed through more commands:

Navigation in the viewing window of more commands can be either line by line or page by page. You can use Enter key vĩ đại navigate by one line at a time or the Space key vĩ đại navigate by one page at a time.

The B key is used vĩ đại go back vĩ đại the previous page. More command also tells you when you reach the over of the tệp tin as well, as shown below:

You can quit the viewing window of more commands at any time by pressing q, which returns you vĩ đại the terminal screen. In this way, more commands can be used vĩ đại view long config or log files without breaking the flow of the terminal.

less

Less is a more modern take on more command. Back when fewer commands were introduced in the Linux ecosystem, most commands did not tư vấn scrolling back up.

In addition vĩ đại letting users scroll up and down, less command also supports horizontal scrolling, supports better handling for binary files, and can perform a tìm kiếm through text.

Let’s look at our simple-list.txt tệp tin through less:

$ less simple-list.txt

You can use arrow keys on the keyboard vĩ đại navigate, in addition vĩ đại the navigation keys for more commands. While you are in the viewing window of less, you can use /word vĩ đại tìm kiếm through the tệp tin contents for word.

This is shown in the screenshot below where the user searches for táo Apple in the tệp tin contents:

Less command also supports more advanced features such as being able vĩ đại view tệp tin contents at the first occurrence of a specific word. To vì thế this, the command syntax is as follows:

$ less +/target-word /path-to-file

For example, vĩ đại open the apples.txt tệp tin at the first occurrence of the word cultivar, the following command would be used:

$ less +/cultivar apples.txt

head

If you just want vĩ đại quickly view the first ten lines of a tệp tin, you can vì thế that through

The head command as shown below:

$ head simple-list.txt 
bananas 
strawberries 
grapes apples 
watermelons 
oranges 
blueberries 
lemons 
peaches 
avocados

By mặc định, only the first ten lines are shown in the terminal, but you can change the number of lines displayed by using the -n option, as shown below:

$ head -n 5 simple-list.txt 
bananas 
strawberries 
grapes 
apples 
watermelons

Similarly, the -c option can be used vĩ đại print a specific number of bytes from the tệp tin vĩ đại the terminal.

tail

The tail command works lượt thích the head command, with the only major difference being that it shows the last ten lines of the tệp tin instead of the first ten lines.

$ tail simple-list.txt 
cherries 
cantaloupe 
raspberries 
pears 
limes 
blackberries 
clementine
mangoes 
plums 
tangerines 
$ tail -n 5 simple-list.txt 
blackberries 
clementine
mangoes 
plums 
tangerines

In addition, both head and tail commands can be used with other tệp tin viewing commands shown in the tutorial vĩ đại produce better output for users.

For example, the nl command can be used vĩ đại first show the tệp tin with line numbers. Then the result can be piped vĩ đại less vĩ đại view the last three lines of the tệp tin, as shown below:

$ nl simple-list.txt | tail -n 3 
19 mangoes 
20 plums 
21 tangerines 

Frequently asked questions about viewing Linux files

How can I see the type of a tệp tin before I view it in the terminal?

You can kiểm tra the type of a tệp tin through the tệp tin command, the syntax of which is as follows:

$ tệp tin [OPTIONS] target-file

How can I view the contents of a really big log tệp tin, for example, an apache or Nginx log tệp tin, vĩ đại find a specific line?

You can combine multiple commands through piping vĩ đại not only tìm kiếm through big log files but vĩ đại display the tìm kiếm result in a well-organized output.

For example, using grep with nl and tail, you can find out when was the last few times a specific tệp tin was requested on your trang web server, as shown below:

$ nl /var/log/apache/mywebsite.log | grep target-file.jpg | tail -n 5

The above command will show the last tệp tin times target-file.jpg was requested on your trang web server.

How can I navigate in the viewing windows of more and fewer commands?

The space key is used vĩ đại scroll down a full page with more and fewer commands.

To scroll down line-by-line in less, use the Enter key on the keyboard.

To scroll back up a page, use the b key.

How can I tìm kiếm for a specific term or word in the viewing window of more or less command while I am reading?

To tìm kiếm for a specific word in the contents of the tệp tin you are currently viewing in more or less, use the syntax /word-to-search.

If I want vĩ đại open an image or rich text document tệp tin through the terminal, what command can I use?

You can use the general command xdg-view, or any variant of it for the specific distribution you have installed, for example, gnome-view or kde-view, vĩ đại open a rich text or image tệp tin.

$ xdg-view enwiki.png

This will open the tệp tin in the mặc định application for the target tệp tin type.