Linux命令行快速技巧之定位一个文件的方法

2019-01-16 20:51:16于丽

你也可以显示一个指定目录的树状图:


$ 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/

总结

以上所述是小编给大家介绍的Linux命令行快速技巧之定位一个文件的方法,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对易采站长站网站的支持!