Merge pull request #11092 from dianbanjiu/translate-new

translated
This commit is contained in:
Xingyu.Wang 2018-11-07 10:47:51 +08:00 committed by GitHub
commit c0a3ced6f4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 228 additions and 229 deletions

View File

@ -1,229 +0,0 @@
translating by dianbanjiu Commandline quick tips: How to locate a file
======
![](https://fedoramagazine.org/wp-content/uploads/2018/10/commandlinequicktips-816x345.jpg)
We all have files on our computers — documents, photos, source code, you name it. So many of them. Definitely more than I can remember. And if not challenging, it might be time consuming to find the right one youre looking for. In this post, well have a look at how to make sense of your files on the command line, and especially how to quickly find the ones youre looking for.
Good news is there are few quite useful utilities in the Linux commandline designed specifically to look for files on your computer. Well have a look at three of those: ls, tree, and find.
### ls
If you know where your files are, and you just need to list them or see information about them, ls is here for you.
Just running ls lists all visible files and directories in the current directory:
```
$ ls
Documents Music Pictures Videos notes.txt
```
Adding the **-l** option shows basic information about the files. And together with the **-h** option youll see file sizes in a human-readable format:
```
$ ls -lh
total 60K
drwxr-xr-x 2 adam adam 4.0K Nov 2 13:07 Documents
drwxr-xr-x 2 adam adam 4.0K Nov 2 13:07 Music
drwxr-xr-x 2 adam adam 4.0K Nov 2 13:13 Pictures
drwxr-xr-x 2 adam adam 4.0K Nov 2 13:07 Videos
-rw-r--r-- 1 adam adam 43K Nov 2 13:12 notes.txt
```
**Is** can also search a specific place:
```
$ ls Pictures/
trees.png wallpaper.png
```
Or a specific file — even with just a part of the name:
```
$ ls *.txt
notes.txt
```
Something missing? Looking for a hidden file? No problem, use the **-a** option:
```
$ ls -a
. .bash_logout .bashrc Documents Pictures notes.txt
.. .bash_profile .vimrc Music Videos
```
There are many other useful options for **ls** , and you can combine them together to achieve what you need. Learn about them by running:
```
$ man ls
```
### tree
If you want to see, well, a tree structure of your files, tree is a good choice. Its probably not installed by default which you can do yourself using the package manager DNF:
```
$ sudo dnf install tree
```
Running tree without any options or parameters shows the whole tree starting at the current directory. Just a warning, this output might be huge, because it will include all files and directories:
```
$ tree
.
|-- Documents
| |-- notes.txt
| |-- secret
| | `-- christmas-presents.txt
| `-- work
| |-- project-abc
| | |-- README.md
| | |-- do-things.sh
| | `-- project-notes.txt
| `-- status-reports.txt
|-- Music
|-- Pictures
| |-- trees.png
| `-- wallpaper.png
|-- Videos
`-- notes.txt
```
If thats too much, I can limit the number of levels it goes using the -L option followed by a number specifying the number of levels I want to see:
```
$ tree -L 2
.
|-- Documents
| |-- notes.txt
| |-- secret
| `-- work
|-- Music
|-- Pictures
| |-- trees.png
| `-- wallpaper.png
|-- Videos
`-- notes.txt
```
You can also display a tree of a specific path:
```
$ tree Documents/work/
Documents/work/
|-- project-abc
| |-- README.md
| |-- do-things.sh
| `-- project-notes.txt
`-- status-reports.txt
```
To browse and search a huge tree, you can use it together with less:
```
$ tree | less
```
Again, there are other options you can use with three, and you can combine them together for even more power. The manual page has them all:
```
$ man tree
```
### find
And what about files that live somewhere in the unknown? Lets find them!
In case you dont have find on your system, you can install it using DNF:
```
$ sudo dnf install findutils
```
Running find without any options or parameters recursively lists all files and directories in the current directory.
```
$ find
.
./Documents
./Documents/secret
./Documents/secret/christmas-presents.txt
./Documents/notes.txt
./Documents/work
./Documents/work/status-reports.txt
./Documents/work/project-abc
./Documents/work/project-abc/README.md
./Documents/work/project-abc/do-things.sh
./Documents/work/project-abc/project-notes.txt
./.bash_logout
./.bashrc
./Videos
./.bash_profile
./.vimrc
./Pictures
./Pictures/trees.png
./Pictures/wallpaper.png
./notes.txt
./Music
```
But the true power of find is that you can search by name:
```
$ find -name do-things.sh
./Documents/work/project-abc/do-things.sh
```
Or just a part of a name — like the file extension. Lets find all .txt files:
```
$ find -name "*.txt"
./Documents/secret/christmas-presents.txt
./Documents/notes.txt
./Documents/work/status-reports.txt
./Documents/work/project-abc/project-notes.txt
./notes.txt
```
You can also look for files by size. That might be especially useful if youre running out of space. Lets list all files larger than 1 MB:
```
$ find -size +1M
./Pictures/trees.png
./Pictures/wallpaper.png
```
Searching a specific directory is also possible. Lets say I want to find a file in my Documents directory, and I know it has the word “project” in its name:
```
$ find Documents -name "*project*"
Documents/work/project-abc
Documents/work/project-abc/project-notes.txt
```
Ah! That also showed the directory. One thing I can do is to limit the search query to files only:
```
$ find Documents -name "*project*" -type f
Documents/work/project-abc/project-notes.txt
```
And again, find have many more options you can use, the man page might definitely help you:
```
$ man find
```
--------------------------------------------------------------------------------
via: https://fedoramagazine.org/commandline-quick-tips-locate-file/
作者:[Adam Šamalík][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/asamalik/
[b]: https://github.com/lujun9972

View File

@ -0,0 +1,228 @@
命令行快捷提示:如何定位一个文件
======
![](https://fedoramagazine.org/wp-content/uploads/2018/10/commandlinequicktips-816x345.jpg)
我们都会有文件存储在电脑里 —— 目录,相片,源代码等等。它们是如此之多。也无疑超出了我的记忆范围。要是毫无目标,找到正确的那一个可能会很费时间。在这篇文章里我们来看一下如何在命令行里找到需要的文件,特别是快速找到你想要的那一个。
好消息是 Linux 命令行专门设计了很多非常有用的命令行工具在你的电脑上查找文件。下面我们看一下它们其中三个ls、tree 和 tree。
### ls
如果你知道文件在哪里你只需要列出它们或者查看有关它们的信息ls 就是为此而生的。
只需运行 ls 就可以列出当下目录中所有可见的文件和目录:
```
$ ls
Documents Music Pictures Videos notes.txt
```
添加 **-l** 选项可以查看文件的相关信息。同时再加上 **-h** 选项,就可以用一种人们易读的格式查看文件的大小:
```
$ ls -lh
total 60K
drwxr-xr-x 2 adam adam 4.0K Nov 2 13:07 Documents
drwxr-xr-x 2 adam adam 4.0K Nov 2 13:07 Music
drwxr-xr-x 2 adam adam 4.0K Nov 2 13:13 Pictures
drwxr-xr-x 2 adam adam 4.0K Nov 2 13:07 Videos
-rw-r--r-- 1 adam adam 43K Nov 2 13:12 notes.txt
```
**ls** 也可以搜索一个指定位置:
```
$ ls Pictures/
trees.png wallpaper.png
```
或者一个指定文件 —— 即便只跟着名字的一部分:
```
$ ls *.txt
notes.txt
```
少了点什么?想要查看一个隐藏文件?没问题,使用 **-a** 选项:
```
$ ls -a
. .bash_logout .bashrc Documents Pictures notes.txt
.. .bash_profile .vimrc Music Videos
```
**ls** 还有很多其他有用的选项,你可以把它们组合在一起获得你想要的效果。可以使用以下命令了解更多:
```
$ man ls
```
### tree
如果你想查看你的文件的树状结构tree 是一个不错的选择。可能你的系统上没有默认安装它,你可以使用包管理 DNF 手动安装:
```
$ sudo dnf install tree
```
如果不带任何选项或者参数地运行 tree将会以当前目录开始显示出包含其下所有目录和文件的一个树状图。提醒一下这个输出可能会非常大因为它包含了这个目录下的所有目录和文件
```
$ tree
.
|-- Documents
| |-- notes.txt
| |-- secret
| | `-- christmas-presents.txt
| `-- work
| |-- project-abc
| | |-- README.md
| | |-- do-things.sh
| | `-- project-notes.txt
| `-- status-reports.txt
|-- Music
|-- Pictures
| |-- trees.png
| `-- wallpaper.png
|-- Videos
`-- notes.txt
```
如果列出的太多了,使用 -L 选项,并在其后加上你想查看的层级数,可以限制列出文件的层级:
```
$ tree -L 2
.
|-- Documents
| |-- notes.txt
| |-- secret
| `-- work
|-- Music
|-- Pictures
| |-- trees.png
| `-- wallpaper.png
|-- Videos
`-- notes.txt
```
你也可以显示一个指定目录的树状图:
```
$ tree Documents/work/
Documents/work/
|-- project-abc
| |-- README.md
| |-- do-things.sh
| `-- project-notes.txt
`-- status-reports.txt
```
如果使用 tree 列出的是一个很大的树状图,你可以把它跟 less 组合使用:
```
$ tree | less
```
再一次tree 有很多其他的选项可以使用你可以把他们组合在一起发挥更强大的作用。man 手册页有所有这些选项:
```
$ man tree
```
### find
那么如果不知道文件在哪里呢?就让我们来找到它们吧!
要是你的系统中没有 find你可以使用 DNF 安装它:
```
$ sudo dnf install findutils
```
运行 find 时如果没有添加任何选项或者参数,它将会递归列出当前目录下的所有文件和目录。
```
$ find
.
./Documents
./Documents/secret
./Documents/secret/christmas-presents.txt
./Documents/notes.txt
./Documents/work
./Documents/work/status-reports.txt
./Documents/work/project-abc
./Documents/work/project-abc/README.md
./Documents/work/project-abc/do-things.sh
./Documents/work/project-abc/project-notes.txt
./.bash_logout
./.bashrc
./Videos
./.bash_profile
./.vimrc
./Pictures
./Pictures/trees.png
./Pictures/wallpaper.png
./notes.txt
./Music
```
但是 find 真正强大的是你可以使用文件名进行搜索:
```
$ find -name do-things.sh
./Documents/work/project-abc/do-things.sh
```
或者仅仅是名字的一部分 —— 像是文件后缀。我们来找一下所有的 .txt 文件:
```
$ find -name "*.txt"
./Documents/secret/christmas-presents.txt
./Documents/notes.txt
./Documents/work/status-reports.txt
./Documents/work/project-abc/project-notes.txt
./notes.txt
```
你也可以根据大小寻找文件。如果你的空间不足的时候,这种方法也许特别有用。现在来列出所有大于 1 MB 的文件:
```
$ find -size +1M
./Pictures/trees.png
./Pictures/wallpaper.png
```
当然也可以搜索一个具体的目录。假如我想在我的 Documents 文件夹下找一个文件,而且我知道它的名字里有 “project” 这个词:
```
$ find Documents -name "*project*"
Documents/work/project-abc
Documents/work/project-abc/project-notes.txt
```
除了文件它还显示目录。你可以限制仅搜索查询文件:
```
$ find Documents -name "*project*" -type f
Documents/work/project-abc/project-notes.txt
```
最后再一次find 还有很多供你使用的选项要是你想使用它们man 手册页绝对可以帮到你:
```
$ man find
```
--------------------------------------------------------------------------------
via: https://fedoramagazine.org/commandline-quick-tips-locate-file/
作者:[Adam Šamalík][a]
选题:[lujun9972][b]
译者:[dianbanjiu](https://github.com/dianbanjiu)
校对:[校对者ID](https://github.com/校对者ID)
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出
[a]: https://fedoramagazine.org/author/asamalik/
[b]: https://github.com/lujun9972