命令行小技巧：读取文件的不同方式
文件
![](/data/attachment/album/201810/21/000526kk3y3iwphyhkocw4.jpg)


作为图形操作系统，Fedora 的使用是令人愉快的。你可以轻松地点击完成任何任务。但你可能已经看到了，在底层还有一个强大的命令行。想要在 shell 下体验，只需要在 Fedora 系统中打开你的终端应用。这篇文章是向你展示常见的命令行使用方法的系列文章之一。


在这部分，你将学习如何以不同的方式读取文件，如果你在系统中打开一个终端完成一些工作，你就有可能需要读取一两个文件。


### 一应俱全的大餐


对命令行终端的用户来说， `cat` 命令众所周知。 当你 `cat` 一个文件，你很容易的把整个文件内容展示在你的屏幕上。而真正发生在底层的是文件一次读取一行，然后一行一行写入屏幕。


假设你有一个文件，叫做 `myfile`， 这个文件每行只有一个单词。为了简单起见，每行的单词就是这行的行号，就像这样：



```
one
two
three
four
five
```

所以如果你 `cat` 这个文件，你就会看到如下输出：



```
$ cat myfile
one
two
three
four
five
```

并没有太惊喜，不是吗？ 但是有个有趣的转折，只要使用 `tac` 命令，你可以从后往前 `cat` 这个文件。（请注意， Fedora 对这种有争议的幽默不承担任何责任！）



```
$ tac myfile
five
four
three
two
one
```

`cat` 命令允许你以不同的方式装饰输出，比如，你可以输出行号：



```
$ cat -n myfile
     1 one
     2 two
     3 three
     4 four
     5 five
```

还有其他选项可以显示特殊字符和其他功能。要了解更多, 请运行 `man cat` 命令， 看完之后，按 `q` 即可退出回到 shell。


### 挑选你的食物


通常，文件太长会无法全部显示在屏幕上，您可能希望能够像文档一样查看它。 这种情况下，可以试试 `less` 命令：



```
$ less myfile
```

你可以用方向键，也可以用 `PgUp`/`PgDn` 来查看文件， 按 `q` 就可以退回到 shell。


实际上，还有一个 `more` 命令，其基于老式的 UNIX 系统命令。如果在退回 shell 后仍想看到该文件的内容，则可能需要使用它。而 `less` 命令则让你回到你离开 shell 之前的样子，并且清除屏幕上你看到的所有的文件内容。


### 一点披萨或甜点


有时，你所需的输出只是文件的开头。 比如，有一个非常长的文件，当你使用 `cat` 命令时，会显示这个文件所有内容，前几行的内容很容易滚动过去，导致你看不到。`head` 命令会帮你获取文件的前几行：



```
$ head -n 2 myfile
one
two
```

同样，你会用 `tail` 命令来查看文件的末尾几行：



```
$ tail -n 3 myfile
three
four
five
```

当然，这些只是在这个领域的几个简单的命令。但它们可以让你在阅读文件时容易入手。




---


via: <https://fedoramagazine.org/commandline-quick-tips-reading-files-different-ways/>


作者：[Paul W. Frields](https://fedoramagazine.org/author/pfrields/) 选题：[lujun9972](https://github.com/lujun9972) 译者：[distant1219](https://github.com/distant1219) 校对：[wxy](https://github.com/wxy)


本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译，[Linux中国](https://linux.cn/) 荣誉推出


OK
Fedora is delightful to use as a graphical operating system. You can point and click your way through just about any task easily. But you’ve probably seen there is a powerful command line under the hood. To try it out in a shell, just open the Terminal application in your Fedora system. This article is one in a series that will show you some common command line utilities.

In this installment you’ll learn how to read files in different ways. If you open a Terminal to do some work on your system, chances are good that you’ll need to read a file or two.


## The whole enchilada

The **cat** command is well known to terminal users. When you **cat** a file, you’re simply displaying the whole file to the screen. Really what’s happening under the hood is the file is read one line at a time, then each line is written to the screen.

Imagine you have a file with one word per line, called *myfile*. To make this clear, the file will contain the word equivalent for a number on each line, like this:

one two three four five

So if you **cat** that file, you’ll see this output:

$cat myfileone two three four five

Nothing too surprising there, right? But here’s an interesting twist. You can also **cat** that file backward. For this, use the **tac** command. (Note that Fedora takes no blame for this debatable humor!)

$tac myfilefive four three two one

The **cat** file also lets you ornament the file in different ways, in case that’s helpful. For instance, you can number lines:

$cat -n myfile1 one 2 two 3 three 4 four 5 five

There are additional options that will show special characters and other features. To learn more, run the command **man cat**, and when done just hit **q** to exit back to the shell.

## Picking over your food

Often a file is too long to fit on a screen, and you may want to be able to go through it like a document. In that case, try the **less** command:

$less myfile

You can use your arrow keys as well as **PgUp/PgDn** to move around the file. Again, you can use the **q** key to quit back to the shell.

There’s actually a **more** command too, based on an older UNIX command. If it’s important to you to still see the file when you’re done, you might want to use it. The **less** command brings you back to the shell the way you left it, and clears the display of any sign of the file you looked at.

## Just the appetizer (or dessert)

Sometimes the output you want is just the beginning of a file. For instance, the file might be so long that when you **cat** the whole thing, the first few lines scroll past before you can see them. The **head** command will help you grab just those lines:

$head -n 2 myfileone two

In the same way, you can use **tail** to just grab the end of a file:

$tail -n 3 myfilethree four five

Of course these are only a few simple commands in this area. But they’ll get you started when it comes to reading files.

## jun.zhou

thankyou.The article is very simple,but userfull.

## João Rodrigues

The name cat comes from the word concatenate (and not from the animal).

Another useful uses of head and tail (that I always mix-up) are the signed “-n” arguments.

tail -n +NUM makes tail start outputting at line NUM

So, if you would want to print all the lines except the first one, you’d use

$ tail -n +2 myfile

two

three

four

five

head -n -NUM outputs every line except the last NUM lines.

So, if you want to output everything but the last line:

$ head -n -1 myfile

one

two

three

four

## Paul W. Frields

Great additional tips, João! We were hoping these short articles would invite helpful comments like this.

## svsv sarma

Very educative and informative indeed. Different moods of the same command is interesting. With GUI, Terminal has lost its importance. But in Linux, particularly in Fedora, Terminal has its own significance, for use with DNF, APT and Dracut etc. I am beginning and trying hard to understand its power now.

However, with the command $ cat or $ tac, the Terminal stuns and no output is shown!

## Paul W. Frields

The reason no output is shown is that, if you don’t include a file name, both

catandtacexpect to be fed data from the standard input (usually provided by a pipe). We’ll definitely cover using the pipe in a future article.## ryuuy

better way is using Ruby

https://github.com/tombenner/ru

https://www.sitepoint.com/ru-ruby-shell/

$ru ‘map(:to_i).sum’ myfile

sum of numbers in file

## Rich

Launching an entire interpreted language to apply line numbers to text files is not a great use of the Unix commandline; especially when atomic Unix commands provide the functionality that’s sought.

That becomes particularly important when using shell built-in commands, like “printf” or “read”. One of the goals of scripting should be to do as much as possible in one process, without loading up external commands, or deferring to other interpreters. This is why you’ll often see forum applause when someone solves a tricky regex using only bash built-in string manipulation and matching; or using a minimum of external atomic commands like “tr”, “cat” and “tac”, etc.

## peter p

i also like using sed for printing lines of a file.

sed 25q

will print the first 25 lines.

while

sed -n 7,10p

prints lines 7 thru 10.

## Declan McNulty

My favourite is “tail -f “. The f stands for “follow”. As the file changes on disk, any additional lines appended to the end are immediately displayed.

Very useful for following a log file in real time for troubleshooting.

For example you could run:

tail -f /var/log/messages

…while asking a user to repeat some action that did not complete as expected last time.

## Rich

…or uppercase, “tail -F /path/to/file” which is useful for following a logfile that either doesn’t exist yet, or you expect to be rotated.

## Daniel

a very useful flag for tail is:

tail -f myfile

to output appended data as the file grows.

## John G

I had not noticed that

will keep the file output in the terminal after you exit, where

will not.

Thanks for this interesting fact!

## Mehdi

Nice article!

Also, one useful of tail that I learned from our network guy is the following:

tail -f filename

This shows the end content of the file real-time; very useful for observing log files for example on a development system.

## Ralph

You mentioned that less does clear the screen on quit, while more leaves the last viewed screen visible, and recommended to use the more pager if one wanted the last screen still visible on return to the shell prompt.

Although this is true for less’es default behaviour there is no need to use more any more (no pun intended) and one should ditch it, unless one is forced to view text files on legacy Unices like e.g. AIX, HP-UX or Solaris, where less per se isn’t available.

For the folks who are only out and about on Linux less should almost always be preferred.

If you want less to not clear the screen on quit simply use the -X option.

e.g.

less -X /etc/fstab

Now when you press q you will be returned to the shell prompt, but still see above it the last viewed screen page of the file you viewed.

If you require this behaviour in general and you are tired of always typing extra options that you need (also others besides -X), you could either set an alias (e.g. in your ~/.bashrc and export BASH_ENV=$HOME/.bashrc),

or export the environment variable LESS and assign it any numer of options that you could read about in man less (e.g. also in .bashrc or .bash_profile).

Or simply pass LESS=”-X” as environment to the immediately to the issued command.

e.g.

LESS=-X man less

## João Rodrigues

A less known fact about man is that you can render man pages in postscript format.

$ man -t bash > bash.ps

$ xdg-open bash.ps

It’s great if you need a printed version of a man page.

You can also read man pages in html

$ man –html=firefox bash

or

$ export BROWSER=$(which firefox)

$ man -H bash

## fmatejic

while read line; do printf “$line\n”; done < /etc/fstab

Without calling extend command 😉

## Barry

It’s interesting to me as someone who has used UNIX (since BSD 4.2) and Linux since the kernel version was less than 1! That newcomers see Linux as a GUI OS with an add-on command line! The history of UNIX/Linux is that it is a command line OS with a bolt-on GUI!

The power of the command line is incredible and no effort spent learning it is a waste. You can achieve lots, quickly and easily.

If you use Linux, learn something about the command line, don’t fear it! Its not difficult and could make you more productive.

As an example imagine you have 1000 photos in a directory with names like IMG0001.PNG and you wanted to rename them to img-.png where was replaced with the creation date… Could you do this from the GUI? I could (given a hot cup of coffee and a few mins) craft a command or script to do this.

## Ivan

Why not using awk? 🙂

1) Print the line from 3 to end:

awk ‘{if (NR>=3 ) {print $0}}’ filename

2) Print the line between 2 and 30:

awk ‘{if (NR>=2 && NR <=30 ) {print $0}}’ filename

3) print the lines from 1 to 10:

awk ‘{if (NR<=10) {print $0}}’ filename

## Rahul Karkhanis

Wonderfully written article …

My favourite is “nl”

The “nl ” also lets you ornament the file in different ways, in case that’s helpful. For instance, you can number lines:

$ nl myfile

1 one

2 two

3 three

4 four

5 five

do refer man page for much more interesting options