From e31dffe40db70904f3f6247484f696ec345afee6 Mon Sep 17 00:00:00 2001 From: distant1219 Date: Wed, 17 Oct 2018 10:42:14 +0800 Subject: [PATCH 1/2] Delete 20181012 Command line quick tips- Reading files different ways.md --- ...uick tips- Reading files different ways.md | 120 ------------------ 1 file changed, 120 deletions(-) delete mode 100644 sources/tech/20181012 Command line quick tips- Reading files different ways.md diff --git a/sources/tech/20181012 Command line quick tips- Reading files different ways.md b/sources/tech/20181012 Command line quick tips- Reading files different ways.md deleted file mode 100644 index 87e6e77d1e..0000000000 --- a/sources/tech/20181012 Command line quick tips- Reading files different ways.md +++ /dev/null @@ -1,120 +0,0 @@ -translating by distant1219 - -Command line quick tips: Reading files different ways -====== - -![](https://fedoramagazine.org/wp-content/uploads/2018/10/commandlinequicktips-816x345.jpg) - -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 myfile - one - 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 myfile - five - 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 myfile - 1 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 myfile - one - two - -``` - -In the same way, you can use **tail** to just grab the end of a file: - -``` - - $ tail -n 3 myfile - three - 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. - --------------------------------------------------------------------------------- - -via: https://fedoramagazine.org/commandline-quick-tips-reading-files-different-ways/ - -作者:[Paul W. Frields][a] -选题:[lujun9972][b] -译者:[译者ID](https://github.com/译者ID) -校对:[校对者ID](https://github.com/校对者ID) - -本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出 - -[a]: https://fedoramagazine.org/author/pfrields/ -[b]: https://github.com/lujun9972 From 32a6fc570a53c1ce76001e9f84c10b407a0bb8af Mon Sep 17 00:00:00 2001 From: distant1219 Date: Wed, 17 Oct 2018 10:43:50 +0800 Subject: [PATCH 2/2] Create 20181012 Command line quick tips- Reading files different ways.md --- ...uick tips- Reading files different ways.md | 116 ++++++++++++++++++ 1 file changed, 116 insertions(+) create mode 100644 translated/tech/20181012 Command line quick tips- Reading files different ways.md diff --git a/translated/tech/20181012 Command line quick tips- Reading files different ways.md b/translated/tech/20181012 Command line quick tips- Reading files different ways.md new file mode 100644 index 0000000000..cbd0115294 --- /dev/null +++ b/translated/tech/20181012 Command line quick tips- Reading files different ways.md @@ -0,0 +1,116 @@ +命令行快速提示: 读取文件的不同方式 +====== + +![](https://fedoramagazine.org/wp-content/uploads/2018/10/commandlinequicktips-816x345.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][a] +选题:[lujun9972][b] +译者:[distant1219](https://github.com/distant1219) +校对:[校对者ID](https://github.com/校对者ID) + +本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出 + +[a]: https://fedoramagazine.org/author/pfrields/ +[b]: https://github.com/lujun9972