Merge pull request #18468 from geekpi/translating

translated
This commit is contained in:
geekpi 2020-05-21 08:48:04 +08:00 committed by GitHub
commit 3689b64700
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 143 additions and 146 deletions

View File

@ -1,146 +0,0 @@
[#]: collector: (lujun9972)
[#]: translator: (geekpi)
[#]: reviewer: ( )
[#]: publisher: ( )
[#]: url: ( )
[#]: subject: (Tricks for getting around your Linux file system)
[#]: via: (https://www.networkworld.com/article/3533421/tricks-for-getting-around-your-linux-file-system.html)
[#]: author: (Sandra Henry-Stocker https://www.networkworld.com/author/Sandra-Henry_Stocker/)
Tricks for getting around your Linux file system
======
The cd command is probably one of the first 10 that any Linux user learns, but it's not the only way to navigate the Linux file system.Here are some other ways.
Thinkstock
Whether you're moving around the file system, looking for files or trying to move into important directories, Linux can provide a lot of help. In this post, we'll look at a number of tricks to make moving around the file system and both finding and using commands that you need a little easier.
### Adding to your $PATH
One of the easiest and most useful ways to ensure that you don't have to invest a lot of time into finding commands on a Linux system is to add the proper directories to your $PATH variable. The order of directories that you add to your $PATH variable is, however, very important. They determine the order in which the system will look through the directories to find the command to run -- stopping when it finds the first match.
You might, for example, want to put your home directory first so that, if you create a script that has the same name as some other executable, it will be the one that you end up running whenever you type its name.
[RELATED: Linux hardening: a 15-step checklist for a secure Linux server][1]
To add your home directory to your $PATH variable, you could do this:
```
$ export PATH=~:$PATH
```
The **~** character represents your home directory.
If you keep your scripts in your bin directory, this would work for you:
```
$ export PATH=~/bin:$PATH
```
You can then run a script located in your home directory like this:
[][2]
```
$ myscript
Good morning, you just ran /home/myacct/bin/myscript
```
**IMPORTANT:** The commands shown above _add_ to your search path because $PATH (the current path) is included. They don't override it. Your search path should be configured in your **.bashrc** file, and any changes you intend to be permanent should be added there as well.
### Using symbolic links
Symbolic links provide an easy and obvious way to record the location of directories that you might need to use often. If you manage content for a web site, for example, you might want to get your account to "remember" where the web files are located by creating a link like this:
```
ln -s /var/www/html www
```
The order of the arguments is critical. The first (/var/www/html) is the target and the second is the name of the link that you will be creating. If you're not currently located in your home directory, the following command would do the same thing:
```
ln -s /var/www/html ~/www
```
After setting this up, you can use "cd www" to get to **/var/www/html**.
### Using shopt
The **shopt** command also provides a way to make moving to a different directory a bit easier. When you employ **shopt'**s **autocd** option, you can go to a directory simply by typing its name. For example:
```
$ shopt -s autocd
$ www
cd -- www
/home/myacct/www
$ pwd -P
/var/www/html
$ ~/bin
cd -- /home/myacct/bin
$ pwd
/home/myacct/bin
```
In the first set of commands above, the **shopt** command's **autocd** option is enabled. Typing **www** then invokes a "cd www" command. Because this symbolic link was created in one of the **ln** command examples above, this moves us to **/var/www/html**. The **pwd -P** command displays the actual location.
In the second set, typing **~/bin** invokes a **cd** into the **bin** directory in the user's home.
Note that the **autocd** behavior will _not_ kick in when what you type is a command   even if it's also the name of a directory.
The **shopt** command is a bash builtin and has a lot of options. This one just means that you don't have to type "cd" before the name of each directory you want to move into.
To see **shopt**'s other options, just type "shopt".
### Using $CDPATH
Probably one of the most useful tricks for moving into particular directories is adding the paths that you want to be able to move into easily to your **$CDPATH**. This creates a list of directories that will be moved into by typing only a portion of the full path names.
There is one aspect of this that may be just a little tricky. Your **$CDPATH** needs to include the directories that _contain_ the directories that you want to move into, not the directories themselves.
For example, say that you want to be able to move into the **/var/www/html** directory simply by typing "cd html" and into subdirectories in /var/log using only "cd" and the simple directory names. In this case, this **$CDPATH** would work:
```
$ CDPATH=.:/var/log:/var/www
```
Here's what you would see:
```
$ cd journal
/var/log/journal
$ cd html
/var/www/html
```
Your **$CDPATH** kicks in when what you type is _not_ a full path. Then it looks down its list of directories in order to see if the directory you identified exists in one of them. Once it finds a match, it takes you there.
Keeping the "." at the beginning of your **$CDPATH** means that you can move into local directories without having to have them defined in the **$CDPATH**.
```
$ export CDPATH=".:$CDPATH"
$ Videos
cd -- Videos
/home/myacct/Videos
```
It's not hard to move around the Linux file system, but you can save a few brain cells if you use some handy tricks for getting to various locations easily.
Join the Network World communities on [Facebook][3] and [LinkedIn][4] to comment on topics that are top of mind.
--------------------------------------------------------------------------------
via: https://www.networkworld.com/article/3533421/tricks-for-getting-around-your-linux-file-system.html
作者:[Sandra Henry-Stocker][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://www.networkworld.com/author/Sandra-Henry_Stocker/
[b]: https://github.com/lujun9972
[1]: https://www.networkworld.com/article/3143050/linux/linux-hardening-a-15-step-checklist-for-a-secure-linux-server.html#tk.nww-fsb
[2]: https://www.networkworld.com/blog/itaas-and-the-corporate-storage-technology/?utm_source=IDG&utm_medium=promotions&utm_campaign=HPE22140&utm_content=sidebar (ITAAS and Corporate Storage Strategy)
[3]: https://www.facebook.com/NetworkWorld/
[4]: https://www.linkedin.com/company/network-world

View File

@ -0,0 +1,143 @@
[#]: collector: (lujun9972)
[#]: translator: (geekpi)
[#]: reviewer: ( )
[#]: publisher: ( )
[#]: url: ( )
[#]: subject: (Tricks for getting around your Linux file system)
[#]: via: (https://www.networkworld.com/article/3533421/tricks-for-getting-around-your-linux-file-system.html)
[#]: author: (Sandra Henry-Stocker https://www.networkworld.com/author/Sandra-Henry_Stocker/)
使用 Linux 文件系统的技巧
======
cd 命令可能是任何 Linux 用户学习的前 10 个命令之一,但这并不是在 Linux 文件系统中切换的唯一方法,这里还有其他一些方法。
无论你是在文件系统中四处查看、寻找文件还是尝试进入重要目录Linux 都可以提供很多帮助。在本文中,我们将介绍一些技巧,使你可以在文件系统中移动,查找和使用所需的命令也更加轻松。
### 添加到 $PATH
确保你不必花费大量时间在 Linux 系统上查找命令的最简单、最有用的方法之一就是在 $PATH 变量中添加适当的目录。但是,添加到 $PATH 变量中的目录顺序非常重要。它们确定系统在目录中查找要运行命令的目录顺序--在找到第一个匹配项时停止。
例如,你可能希望将家目录放在第一个,这样,如果你创建的脚本与其他可执行文件有相同的名称,那么只要输入该脚本的名称,它便会运行。
要将家目录添加到 $PATH 变量中,可以执行以下操作:
```
$ export PATH=~:$PATH
```
**~** 字符代表家目录。
如果将脚本保存在 bin 目录中,下面的会有效:
```
$ export PATH=~/bin:$PATH
```
然后,你可以运行位于家目录中的脚本,如下所示:
[][2]
```
$ myscript
Good morning, you just ran /home/myacct/bin/myscript
```
**重要提示:**上面的命令_添加_搜索路径因为 $PATH (当前路径)包含了。它们不会覆盖它。你的搜索路径应该在 **.bashrc** 文件中配置,任何持久更改也需要在这里添加。
### 使用符号链接
符号链接提供了一种简单而明显的方式来记录可能经常需要使用的目录的位置。例如,如果你管理网站的内容,那么可能需要通过创建如下链接来使你的帐户“记住”网页文件的位置:
```
ln -s /var/www/html www
```
参数的顺序很重要。第一个(/var/www/html是目标第二个是你创建的链接的名称。如果你当前不在家目录中那么以下命令将执行相同的操作
```
ln -s /var/www/html ~/www
```
设置好之后,您可以使用 “cd www” 进入 **/var/www/html**。
### 使用 shopt
**shopt** 命令还提供了一种让移动到其他目录更加容易的方法。当你使用 **shopt****autocd** 选项时,只需输入名称即可转到目录。例如:
```
$ shopt -s autocd
$ www
cd -- www
/home/myacct/www
$ pwd -P
/var/www/html
$ ~/bin
cd -- /home/myacct/bin
$ pwd
/home/myacct/bin
```
在上面的第一组命令中,启用了 shopt 命令的 autocd 选项。输入 **www**,然后调用 “cd www” 命令。由于此符号链接是在上面的 **ln** 命令示例之一中创建的,因此将我们移至 **/var/www/html**。 **pwd -P** 命令显示实际位置。
在第二组中,键入 **~/bin** 会在用户家目录调用 **cd** 进入 **bin** 目录。
请注意,当你输入的是命令时,**autocd** 行为将_不会_生效即使它也是目录的名称。
**shopt** 是 bash 内置,它有很多选项。这只是意味着你不必在要进入每个目录的名称之前输入 “cd”。
要查看 **shopt** 的其他选项,只需输入 “shopt”。
### 使用 $CDPATH
进入特定目录的最有用技巧之一可能是将你希望能够轻松进入的路径添加到 **$CDPATH** 中。这将创建一个只需输入完整路径名的一部分即可进入的目录列表。
一方面,这可能有点棘手。你的 **$CDPATH** 需要包含要移动到的目录的目录,而不是目录本身。
例如,假设你希望仅通过输入 “cd html” 就可以移至 **/var/www/html** 目录,并仅使用 “cd” 和简单目录名即可移至 /var/log 中的子目录。在这种情况下,此 **$CDPATH** 将起作用:
```
$ CDPATH=.:/var/log:/var/www
```
你将看到:
```
$ cd journal
/var/log/journal
$ cd html
/var/www/html
```
当你输入的不是完整路径时,**$CDPATH** 就会生效。它向下查看其目录列表,以查看指定的目录是否存在于其中一个目录中。找到匹配项后,它将带你到那里。
保持 “.” 在 **$CDPATH** 开头意味着你可以进入本地目录,而不必在 **$CDPATH** 中定义它们。
```
$ export CDPATH=".:$CDPATH"
$ Videos
cd -- Videos
/home/myacct/Videos
```
在 Linux 文件系统键切换并不难,但是如果你使用一些方便的技巧轻松地到达各个位置,那你可以节省一些大脑细胞。
加入 [Facebook][3] 和 [LinkedIn][4] 上的 Network World 社区,评论热门主题。
--------------------------------------------------------------------------------
via: https://www.networkworld.com/article/3533421/tricks-for-getting-around-your-linux-file-system.html
作者:[Sandra Henry-Stocker][a]
选题:[lujun9972][b]
译者:[geekpi](https://github.com/geekpi)
校对:[校对者ID](https://github.com/校对者ID)
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出
[a]: https://www.networkworld.com/author/Sandra-Henry_Stocker/
[b]: https://github.com/lujun9972
[2]: https://www.networkworld.com/blog/itaas-and-the-corporate-storage-technology/?utm_source=IDG&utm_medium=promotions&utm_campaign=HPE22140&utm_content=sidebar (ITAAS and Corporate Storage Strategy)
[3]: https://www.facebook.com/NetworkWorld/
[4]: https://www.linkedin.com/company/network-world