diff --git a/sources/tech/RHCSA Series/RHCSA Series--Part 02--How to Perform File and Directory Management.md b/sources/tech/RHCSA Series/RHCSA Series--Part 02--How to Perform File and Directory Management.md index abf9910994..f46fd93321 100644 --- a/sources/tech/RHCSA Series/RHCSA Series--Part 02--How to Perform File and Directory Management.md +++ b/sources/tech/RHCSA Series/RHCSA Series--Part 02--How to Perform File and Directory Management.md @@ -1,59 +1,61 @@ -[translating by xiqingongzi] -RHCSA Series: How to Perform File and Directory Management – Part 2 +RHCSA 系列: 如何执行文件并进行文件管理 – Part 2 ================================================================================ -In this article, RHCSA Part 2: File and directory management, we will review some essential skills that are required in the day-to-day tasks of a system administrator. + +在本篇(RHCSA 第二篇:文件和目录管理)中,我们江回顾一些系统管理员日常任务需要的技能 ![RHCSA: Perform File and Directory Management – Part 2](http://www.tecmint.com/wp-content/uploads/2015/03/RHCSA-Part2.png) -RHCSA: Perform File and Directory Management – Part 2 -### Create, Delete, Copy, and Move Files and Directories ### +RHCSA : 运行文件以及进行文件夹管理 - 第二章 +### 创建,删除,复制和移动文件及目录 ### -File and directory management is a critical competence that every system administrator should possess. This includes the ability to create / delete text files from scratch (the core of each program’s configuration) and directories (where you will organize files and other directories), and to find out the type of existing files. +文件和目录管理是每一个系统管理员都应该掌握的必要的技能.它包括了从头开始的创建、删除文本文件(每个程序的核心配置)以及目录(你用来组织文件和其他目录),以及识别存在的文件的类型 -The [touch command][1] can be used not only to create empty files, but also to update the access and modification times of existing files. + [touch 命令][1] 不仅仅能用来创建空文件,还能用来更新已存在的文件的权限和时间表 ![touch command example](http://www.tecmint.com/wp-content/uploads/2015/03/touch-command-example.png) -touch command example +touch 命令示例 -You can use `file [filename]` to determine a file’s type (this will come in handy before launching your preferred text editor to edit it). +你可以使用 `file [filename]`来判断一个文件的类型 (在你用文本编辑器编辑之前,判断类型将会更方便编辑). ![file command example](http://www.tecmint.com/wp-content/uploads/2015/03/file-command-example.png) -file command example +file 命令示例 -and `rm [filename]` to delete it. +使用`rm [filename]` 可以删除文件 ![Linux rm command examples](http://www.tecmint.com/wp-content/uploads/2015/03/rm-command-examples.png) -rm command example +rm 命令示例 + +对于目录,你可以使用`mkdir [directory]`在已经存在的路径中创建目录,或者使用 `mkdir -p [/full/path/to/directory].`带全路径创建文件夹 -As for directories, you can create directories inside existing paths with `mkdir [directory]` or create a full path with `mkdir -p [/full/path/to/directory].` ![mkdir command example](http://www.tecmint.com/wp-content/uploads/2015/03/mkdir-command-example.png) -mkdir command example +mkdir 命令示例 -When it comes to removing directories, you need to make sure that they’re empty before issuing the `rmdir [directory]` command, or use the more powerful (handle with care!) `rm -rf [directory]`. This last option will force remove recursively the `[directory]` and all its contents – so use it at your own risk. +当你想要去删除目录时,在你使用`rmdir [directory]` 前,你需要先确保目录是空的,或者使用更加强力的命令(小心使用它)`rm -rf [directory]`.后者会强制删除`[directory]`以及他的内容.所以使用这个命令存在一定的风险 -### Input and Output Redirection and Pipelining ### +### 输入输出重定向以及管道 ### -The command line environment provides two very useful features that allows to redirect the input and output of commands from and to files, and to send the output of a command to another, called redirection and pipelining, respectively. +命令行环境提供了两个非常有用的功能:允许命令重定向的输入和输出到文件和发送到另一个文件,分别称为重定向和管道 To understand those two important concepts, we must first understand the three most important types of I/O (Input and Output) streams (or sequences) of characters, which are in fact special files, in the *nix sense of the word. +为了理解这两个重要概念,我们首先需要理解通常情况下三个重要的输入输出流的形式 -- Standard input (aka stdin) is by default attached to the keyboard. In other words, the keyboard is the standard input device to enter commands to the command line. -- Standard output (aka stdout) is by default attached to the screen, the device that “receives” the output of commands and display them on the screen. -- Standard error (aka stderr), is where the status messages of a command is sent to by default, which is also the screen. +- 标准输入 (aka stdin) 是指默认使用键盘链接. 换句话说,键盘是输入命令到命令行的标准输入设备。 +- 标准输出 (aka stdout) 是指默认展示再屏幕上, 显示器接受输出命令,并且展示在屏幕上。 +- 标准错误 (aka stderr), 是指命令的状态默认输出, 同时也会展示在屏幕上 In the following example, the output of `ls /var` is sent to stdout (the screen), as well as the result of ls /tecmint. But in the latter case, it is stderr that is shown. +在下面的例子中,`ls /var`的结果被发送到stdout(屏幕展示),就像ls /tecmint 的结果。但在后一种情况下,它是标准错误输出。 ![Linux input output redirect](http://www.tecmint.com/wp-content/uploads/2015/03/Linux-input-output-redirect.png) +输入和输出命令实例 -Input and Output Example - -To more easily identify these special files, they are each assigned a file descriptor, an abstract representation that is used to access them. The essential thing to understand is that these files, just like others, can be redirected. What this means is that you can capture the output from a file or script and send it as input to another file, command, or script. This will allow you to store on disk, for example, the output of commands for later processing or analysis. +为了更容易识别这些特殊文件,每个文件都被分配有一个文件描述符(用于控制他们的抽象标识)。主要要理解的是,这些文件就像其他人一样,可以被重定向。这就意味着你可以从一个文件或脚本中捕获输出,并将它传送到另一个文件、命令或脚本中。你就可以在在磁盘上存储命令的输出结果,用于稍后的分析 To redirect stdin (fd 0), stdout (fd 1), or stderr (fd 2), the following operators are available. @@ -63,102 +65,102 @@ To redirect stdin (fd 0), stdout (fd 1), or stderr (fd 2), the following operato -Redirection Operator -Effect +转向操作 +效果 > -Redirects standard output to a file containing standard output. If the destination file exists, it will be overwritten. +标准输出到一个文件。如果目标文件存在,内容就会被重写 >> -Appends standard output to a file. +添加标准输出到文件尾部 2> -Redirects standard error to a file containing standard output. If the destination file exists, it will be overwritten. +标准错误输出到一个文件。如果目标文件存在,内容就会被重写 2>> -Appends standard error to the existing file. +添加标准错误输出到文件尾部. &> -Redirects both standard output and standard error to a file; if the specified file exists, it will be overwritten. +标准错误和标准输出都到一个文件。如果目标文件存在,内容就会被重写 < -Uses the specified file as standard input. +使用特定的文件做标准输出 <> -The specified file is used for both standard input and standard output. +使用特定的文件做标准输出和标准错误 -As opposed to redirection, pipelining is performed by adding a vertical bar `(|)` after a command and before another one. -Remember: +相比与重定向,管道是通过在命令后添加一个竖杠`(|)`再添加另一个命令 . -- Redirection is used to send the output of a command to a file, or to send a file as input to a command. -- Pipelining is used to send the output of a command to another command as input. +记得: -#### Examples Of Redirection and Pipelining #### +- 重定向是用来定向命令的输出到一个文件,或定向一个文件作为输入到一个命令。 +- 管道是用来将命令的输出转发到另一个命令作为输入。 -**Example 1: Redirecting the output of a command to a file** +#### 重定向和管道的使用实例 #### -There will be times when you will need to iterate over a list of files. To do that, you can first save that list to a file and then read that file line by line. While it is true that you can iterate over the output of ls directly, this example serves to illustrate redirection. +** 例1:将一个命令的输出到文件 ** + +有些时候,你需要遍历一个文件列表。要做到这样,你可以先将该列表保存到文件中,然后再按行读取该文件。虽然你可以遍历直接ls的输出,不过这个例子是用来说明重定向。 # ls -1 /var/mail > mail.txt ![Redirect output of command tot a file](http://www.tecmint.com/wp-content/uploads/2015/03/Redirect-output-to-a-file.png) -Redirect output of command tot a file +将一个命令的输出到文件 -**Example 2: Redirecting both stdout and stderr to /dev/null** +** 例2:重定向stdout和stderr到/dev/null ** -In case we want to prevent both stdout and stderr to be displayed on the screen, we can redirect both file descriptors to `/dev/null`. Note how the output changes when the redirection is implemented for the same command. +如果不想让标准输出和标准错误展示在屏幕上,我们可以把文件描述符重定向到 `/dev/null` 请注意在执行这个命令时该如何更改输出 # ls /var /tecmint # ls /var/ /tecmint &> /dev/null ![Redirecting stdout and stderr ouput to /dev/null](http://www.tecmint.com/wp-content/uploads/2015/03/Redirecting-stdout-stderr-ouput.png) -Redirecting stdout and stderr ouput to /dev/null +重定向stdout和stderr到/dev/null -#### Example 3: Using a file as input to a command #### +#### 例3:使用一个文件作为命令的输入 #### -While the classic syntax of the [cat command][2] is as follows. +当官方的[cat 命令][2]的语法如下时 # cat [file(s)] -You can also send a file as input, using the correct redirection operator. +您还可以使用正确的重定向操作符传送一个文件作为输入。 # cat < mail.txt ![Linux cat command examples](http://www.tecmint.com/wp-content/uploads/2015/03/cat-command-examples.png) -cat command example +cat 命令实例 -#### Example 4: Sending the output of a command as input to another #### +#### 例4:发送一个命令的输出作为另一个命令的输入 #### -If you have a large directory or process listing and want to be able to locate a certain file or process at a glance, you will want to pipeline the listing to grep. +如果你有一个较大的目录或进程列表,并且想快速定位,你或许需要将列表通过管道传送给grep -Note that we use to pipelines in the following example. The first one looks for the required keyword, while the second one will eliminate the actual `grep command` from the results. This example lists all the processes associated with the apache user. +接下来我们使用管道在下面的命令中,第一个是查找所需的关键词,第二个是除去产生的 `grep command`.这个例子列举了所有与apache用户有关的进程 # ps -ef | grep apache | grep -v grep ![Send output of command as input to another](http://www.tecmint.com/wp-content/uploads/2015/03/Send-output-of-command-as-input-to-another1.png) -Send output of command as input to another +发送一个命令的输出作为另一个命令的输入 -### Archiving, Compressing, Unpacking, and Uncompressing Files ### +### 归档,压缩,解包,解压文件 ### -If you need to transport, backup, or send via email a group of files, you will use an archiving (or grouping) tool such as [tar][3], typically used with a compression utility like gzip, bzip2, or xz. - -Your choice of a compression tool will be likely defined by the compression speed and rate of each one. Of these three compression tools, gzip is the oldest and provides the least compression, bzip2 provides improved compression, and xz is the newest and provides the best compression. Typically, files compressed with these utilities have .gz, .bz2, or .xz extensions, respectively. +如果你需要传输,备份,或者通过邮件发送一组文件,你可以使用一个存档(或文件夹)如 [tar][3]工具,通常使用gzip,bzip2,或XZ压缩工具. +您选择的压缩工具每一个都有自己的定义的压缩速度和速率的。这三种压缩工具,gzip是最古老和提供最小压缩的工具,bzip2提供经过改进的压缩,以及XZ提供最信和最好的压缩。通常情况下,这些文件都是被压缩的如.gz .bz2或.xz 注:表格 @@ -166,44 +168,44 @@ Your choice of a compression tool will be likely defined by the compression spee - - - + + + - + - + - + - + - + - + - +
CommandAbbreviationDescription命令缩写描述
–create cCreates a tar archive创建一个tar归档
–concatenate AAppends tar files to an archive向归档中添加tar文件
–append rAppends non-tar files to an archive向归档中添加非tar文件
–update uAppends files that are newer than those in an archive添加比归档中的文件更新的文件
–diff or –compare dCompares an archive to files on disk将归档和硬盘的文件夹进行对比
–list tLists the contents of a tarball列举一个tar的压缩包
–extract or –get xExtracts files from an archive从归档中解压文件
@@ -215,51 +217,51 @@ Your choice of a compression tool will be likely defined by the compression spee -Operation modifier -Abbreviation -Description +操作参数 +缩写 +描述 directory dir C -Changes to directory dir before performing operations +在执行操作前更改目录 same-permissions and same-owner p -Preserves permissions and ownership information, respectively. +分别保留权限和所有者信息 –verbose v -Lists all files as they are read or extracted; if combined with –list, it also displays file sizes, ownership, and timestamps +列举所有文件用于读取或提取,这里包含列表,并显示文件的大小、所有权和时间戳 exclude file -Excludes file from the archive. In this case, file can be an actual file or a pattern. +排除存档文件。在这种情况下,文件可以是一个实际的文件或目录。 gzip or gunzip z -Compresses an archive through gzip +使用gzip压缩文件 –bzip2 j -Compresses an archive through bzip2 +使用bzip2压缩文件 –xz J -Compresses an archive through xz +使用xz压缩文件 -#### Example 5: Creating a tarball and then compressing it using the three compression utilities #### +#### 例5:创建一个文件,然后使用三种压缩工具压缩#### -You may want to compare the effectiveness of each tool before deciding to use one or another. Note that while compressing small files, or a few files, the results may not show much differences, but may give you a glimpse of what they have to offer. +在决定使用一个或另一个工具之前,您可能想比较每个工具的压缩效率。请注意压缩小文件或几个文件,结果可能不会有太大的差异,但可能会给你看出他们的差异 # tar cf ApacheLogs-$(date +%Y%m%d).tar /var/log/httpd/* # Create an ordinary tarball # tar czf ApacheLogs-$(date +%Y%m%d).tar.gz /var/log/httpd/* # Create a tarball and compress with gzip @@ -268,51 +270,51 @@ You may want to compare the effectiveness of each tool before deciding to use on ![Linux tar command examples](http://www.tecmint.com/wp-content/uploads/2015/03/tar-command-examples.png) -tar command examples +tar 命令实例 -#### Example 6: Preserving original permissions and ownership while archiving and when #### +#### 例6:归档时同时保存原始权限和所有权 #### -If you are creating backups from users’ home directories, you will want to store the individual files with the original permissions and ownership instead of changing them to that of the user account or daemon performing the backup. The following example preserves these attributes while taking the backup of the contents in the `/var/log/httpd` directory: +如果你创建的是用户的主目录的备份,你需要要存储的个人文件与原始权限和所有权,而不是通过改变他们的用户帐户或守护进程来执行备份。下面的命令可以在归档时保留文件属性 # tar cJf ApacheLogs-$(date +%Y%m%d).tar.xz /var/log/httpd/* --same-permissions --same-owner -### Create Hard and Soft Links ### +### 创建软连接和硬链接 ### -In Linux, there are two types of links to files: hard links and soft (aka symbolic) links. Since a hard link represents another name for an existing file and is identified by the same inode, it then points to the actual data, as opposed to symbolic links, which point to filenames instead. +在Linux中,有2种类型的链接文件:硬链接和软(也称为符号)链接。因为硬链接文件代表另一个名称是由同一点确定,然后链接到实际的数据;符号链接指向的文件名,而不是实际的数据 -In addition, hard links do not occupy space on disk, while symbolic links do take a small amount of space to store the text of the link itself. The downside of hard links is that they can only be used to reference files within the filesystem where they are located because inodes are unique inside a filesystem. Symbolic links save the day, in that they point to another file or directory by name rather than by inode, and therefore can cross filesystem boundaries. +此外,硬链接不占用磁盘上的空间,而符号链接做占用少量的空间来存储的链接本身的文本。硬链接的缺点就是要求他们必须在同一个innode内。而符号链接没有这个限制,符号链接因为只保存了文件名和目录名,所以可以跨文件系统. -The basic syntax to create links is similar in both cases: +创建链接的基本语法看起来是相似的: - # ln TARGET LINK_NAME # Hard link named LINK_NAME to file named TARGET - # ln -s TARGET LINK_NAME # Soft link named LINK_NAME to file named TARGET + # ln TARGET LINK_NAME #从Link_NAME到Target的硬链接 + # ln -s TARGET LINK_NAME #从Link_NAME到Target的软链接 -#### Example 7: Creating hard and soft links #### +#### 例7:创建硬链接和软链接 #### -There is no better way to visualize the relation between a file and a hard or symbolic link that point to it, than to create those links. In the following screenshot you will see that the file and the hard link that points to it share the same inode and both are identified by the same disk usage of 466 bytes. +没有更好的方式来形象的说明一个文件和一个指向它的符号链接的关系,而不是创建这些链接。在下面的截图中你会看到文件的硬链接指向它共享相同的节点都是由466个字节的磁盘使用情况确定。 -On the other hand, creating a hard link results in an extra disk usage of 5 bytes. Not that you’re going to run out of storage capacity, but this example is enough to illustrate the difference between a hard link and a soft link. +另一方面,在别的磁盘创建一个硬链接将占用5个字节,并不是说你将耗尽存储容量,而是这个例子足以说明一个硬链接和软链接之间的区别。 ![Difference between a hard link and a soft link](http://www.tecmint.com/wp-content/uploads/2015/03/hard-soft-link.png) -Difference between a hard link and a soft link +软连接和硬链接之间的不同 -A typical usage of symbolic links is to reference a versioned file in a Linux system. Suppose there are several programs that need access to file fooX.Y, which is subject to frequent version updates (think of a library, for example). Instead of updating every single reference to fooX.Y every time there’s a version update, it is wiser, safer, and faster, to have programs look to a symbolic link named just foo, which in turn points to the actual fooX.Y. +符号链接的典型用法是在Linux系统的版本文件参考。假设有需要一个访问文件foo X.Y 想图书馆一样经常被访问,你想更新一个就可以而不是更新所有的foo X.Y,这时使用软连接更为明智和安全。有文件被看成foo X.Y的链接符号,从而找到foo X.Y -Thus, when X and Y change, you only need to edit the symbolic link foo with a new destination name instead of tracking every usage of the destination file and updating it. +这样的话,当你的X和Y发生变化后,你只需更新一个文件,而不是更新每个文件。 -### Summary ### +### 总结 ### -In this article we have reviewed some essential file and directory management skills that must be a part of every system administrator’s tool-set. Make sure to review other parts of this series as well in order to integrate these topics with the content covered in this tutorial. +在这篇文章中,我们回顾了一些基本的文件和目录管理技能,这是每个系统管理员的工具集的一部分。请确保阅读了本系列的其他部分,以及复习并将这些主题与本教程所涵盖的内容相结合。 -Feel free to let us know if you have any questions or comments. We are always more than glad to hear from our readers. +如果你有任何问题或意见,请随时告诉我们。我们总是很高兴从读者那获取反馈. -------------------------------------------------------------------------------- via: http://www.tecmint.com/file-and-directory-management-in-linux/ 作者:[Gabriel Cánepa][a] -译者:[译者ID](https://github.com/译者ID) +译者:[xiqingongzi](https://github.com/xiqingongzi) 校对:[校对者ID](https://github.com/校对者ID) 本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创翻译,[Linux中国](https://linux.cn/) 荣誉推出