命令行星期二 —— 第三篇
命令行 命令行星期二
今天，肖茨先生将带领我们游历文件系统的第一部分。我们将学到如何访问、列出目录内的文件，以及首次介绍一些选项的运用。OK，让我们开始学习这周的第一个命令。


### ls


ls 是一个用来列出目录内文件的命令。通过添加**选项**来实现多种不同的用途。简单起见，你可以只需键入 ls 。但是，你也可以在命令后面添加一个选项，它会帮助你修饰你的命令。如果你想要随意摆弄一些信息时，这会帮到你的。举例来说，当你要从一个命令的大量输出中找寻指定信息，你可以用选项缩小范围来实现。


这就是选项的基础概念。我们可以用下面书写格式表示：



```
command(命令) -option(选项) argument(参数)

```

命令，恩...，我们可以输入 pwd、 ls， 或者我们到目前为止所有学过的命令。


以上我们已经阐述了选项的目的。但是我们需要注意一下书写格式：在前头添加一个破折号。所以，如果选项为 l, 你需要在命令后键入**-l**。


**参数**是一个命令的操作对象（在这个例子中，它是一个目录，我们将会学到如何浏览它们）。


接着，让我们尝试在主目录里尝试用 ls 命令列出 /etc 目录内容。这次我们先不添加选项。


![](/data/attachment/album/201407/14/214837zj7d9iyoqo8i8uqy.png)


就这样，列出了一大堆的文件。它们还根据颜色进行了分类。蓝色的是目录，白色的为普通文件，绿色的似乎是某种 shell 脚本文件。除此之外，还有其他不同的颜色来代表不同的文件类型。


接下来，你可以在刚才的命令里添加 -l 选项。添加选项 -l 后也会列出同样的文件和目录，但是以长格式方式输出。如果你需要查看更多信息的话，这个选项将是不错的选择。


![](/data/attachment/album/201407/14/214839hmpzgpfbbtjt86kk.png)


### 长格式


这样，用了长格式后，你可以看到更详细的信息，以及在每行开头类似-rw-r--r-- 令人抓狂的标示。实际上，这只是一种组合，代表了各种意义的信息。


![](/data/attachment/album/201407/14/214842xzigd3nygn1o31i1.png)


(File Name)就是文件的名称。（Modification time）是文件最后修改的时间。(Size) 用 byte 计量的文件大小。(Group) 是组的名称，和拥有者一起构成文件权限。(Owner) 是文件拥有者的名称。最最重要的…


### …文件权限


文件权限在长格式每一行的开端都显得非常的杂乱。第一个字符代表文件类型。如果是 'd' , 意味着它是一个目录。如果是 '-' , 意味着它是一个一般文件。接下来的三个字符分别代表拥有者的可读， 可写 ，可执行的权限。再接着三个字符表示组成员的享有的文件权限，而最后三个字符表示其他人（既不是拥有者也不是同组）对文件的享有权限。


例如，如果有个文件在长格式下显示为：-rw-r--r--, 这说明这是个普通文件（首字符 '-'），拥有者享有可读可写权限，但是没有可执行权限，导致拥有者无法执行该文件（首字符'-'后'rw-'），用户组和其他用户只享有可读权限（你不难发现'r--'字段在后面出现了两次 。如果用户组是 'rwx' 而不是 'r-'，则意味着它们可读，可写，可执行）。


ls 的下个选项是 ls -la .. ，-a 它会列出所有的文件，包括隐藏文件。在通常情况下，隐藏文件是不可见的。用长格式列出当前工作目录下的父目录的所有文件。


### less


less 是一个可以显示你的文本文件的命令。举例来说，你要在 /etc 下找寻名为 os-release 的文本文件。你可以使用 ls /etc 成功实现，而现在你想要阅读它的内容。


你只需要使用 less /etc/os-release。


![](/data/attachment/album/201407/14/214845dlbr5rn54atxb4rr.png)


..就这样了。


你要如何控制 less 呢?


简单的， 你只需要动动键盘就行了！


less 一次只会显示一个页面的文本。往前翻页你需要按 **Page Up**, 或者 **'b'**。往后翻页你可以按**Page Down**, 或者 **空格**。大写的**G**会跳转到文本的末尾，**1G**会跳转到文本的开端。**/字符**会在文本内搜索指定字符（例如，如果你输入 /suse ，它会找寻所有文本含有的 suse 并标记出来）。n 会重复执行你的搜索，**h**会显示所有的选项（h，即帮助的意思）。


![](/data/attachment/album/201407/14/214848ckc0funl61f0laub.jpg)


按q退出 less 命令。


### file


file 会显示文件的类型，是否是你要找的 ASCII 文本，还是 jpg 图片，bash 脚本等。让我们用 /etc/os-release 执行练习。


![](/data/attachment/album/201407/14/214850kc44pnk43lck4al7.png)


这样，如你所见，os-release 是一个 ASCII 文本文件。 请尝试其他文件，并观察结果。


下回见了，记住…


…一定要玩的开心啊！




---


via: <https://news.opensuse.org/2014/07/01/command-line-tuesdays-part-three/>


译者：[Luoxcat](https://github.com/Luoxcat) 校对：[wxy](https://github.com/wxy)


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


OK
# Command Line Tuesdays - Part Three

1. Jul 2014 | Nenad Latinović | No License

Heya Geekos! New week, new part in our CLT series!

Today, mr Shotts takes us on a first part of a guided tour through our file system. We’ll learn how to visit, list files within directories and we’ll learn to use some options for the first time. So let’s begin with the first command of the week.

##### ls

ls is a command used to list files in a directory. It can be used in various ways. These various ways come with using a form of adding an **option** to our command. To make things clearer; You can type only ls. But, you can also add an option, which will modify your command. It can come in useful when you want to sandbox some of the information, when looking for something specific, and the command issues a very long output, you can, for example, narrow it down to only specific information.

That’s what an option is. And formally we can write it down like this:

*command -option argument*

Command is, well, a command we write in (like pwd, ls or anything else we have learned by now).

We already stated above the purpose of an option. But let me note it should be written exactly as it’s in the form; With a dash in front of it. So, if the option is l, you should put** -l** after the command.

An **argument** is an object upon which the command operates (in this case, it will be directories, as we’ll learning how to navigate through them).

So, let’s try out ls, and use it on the /etc directory in our home folder. This time, without any options.

There you go, a whole bunch of files. It also sorts them by colours. The blue ones are directories, the white ones are files, the green one seems like a shell script of some sort. There are more colors, as they represent different file types.

Next, you can use the same command, but with an option -l added. Option -l will list the same files and directories, but in a long format. In case you need more information:

##### The Long Format

So, using the long format, you see much more information, and some crazy looking signs like -rw-r–r– at the beginning of all lines. Actually, here’s a scheme, representing what all of the given information actually means.

File Name is the name of the file. Modification time is the last time the file has been modified. Size is the size of the file in bytes. Group is the name of the group that has file permissions along with the owner, and Owner is the user who owns the file. The most important one is…

##### …File Permissions

That’s the gibberish at the beginning of every line in long format. The first character is the file type. If it’s a ‘d’, it means the file’s a directory. If it’s ‘-‘, it means it’s an ordinary file. The next three characters represent the read, write and execution rights of the file’s owner. The next three are the same rights of the group that also has access to the file, and the last three characters represent rights of everyone else trying to use the file.

So for example, if we have a file which in long format displays: -rw-r–r–, it means it’s an ordinary file (the first ‘-‘), the owner of the file can read and write the file, but he can’t execute it, as it’s not an executable file (the ‘rw-‘ characters after the initial ‘-‘), and the user group and everyone else can only read the file (you can see ‘r–’ sequence repeating twice. If the user group had ‘rwx’ instead of ‘r–’, it would mean they could read, write and execute the file).

Next option for ls is ls -la .. - this will list all of the files, as in a usual command, hidden files are not shown. It will list all files in the parent of the working directory in long format.

##### less

Less is a command which will display your text file. For example, you’re looking for text file os-release in /etc. You have succesfully found it there with ls /etc, and now you want to read it.

You just use less /etc/os-release.

..and there you go.

How do you control less?

Easy, with your keyboard!

less will display only one page of your text at a time. To go forward one page, you can press** Page Up, or ‘b’**. To go back one page, you can use **Page Down, or space**. Capital** G** will take you to the end of the text file, while ‘**1G**’ will take you to the beginning of the text. ** /characters** will search for

*characters*inside the text (for example, if you write /suse, it will search for occurances of suse inside your text and mark them).** n** will repeat the last search you performed, and** h** will display all your options (h, as in haalp!).

You quit less with the letter **q**.

##### file

The file command will show what kind of file is that you’re looking for, be it ASCII text, a jpg image, a bash script etc. As we performed our exercise with /etc/os-release, let’s use it here also.

There you go, os-release is an ASCII text file. Please, do try it out with something else, and see the output.

And until next time, remember to…

#### …have a lot of fun!

**Categories:**
[
Distribution
](/category/distribution)

**Tags:**