命令行快速技巧:如何定位一个文件 | Linux 中国

2018 年 11 月 21 日 Linux中国
Linux 命令行专门设计了很多非常有用的命令行工具在你的电脑上查找文件。下面我们看一下它们其中三个:ls、tree 和 find。
-- Adam Šamalík



致谢
编译自 | 
https://fedoramagazine.org/commandline-quick-tips-locate-file/
 
 作者 | Adam Šamalík
 译者 | dianbanjiu 🌟🌟🌟共计翻译:13.0 篇 贡献时间:50 天

我们都会有文件存储在电脑里 —— 目录、相片、源代码等等。它们是如此之多。也无疑超出了我的记忆范围。要是毫无目标,找到正确的那一个可能会很费时间。在这篇文章里我们来看一下如何在命令行里找到需要的文件,特别是快速找到你想要的那一个。

好消息是 Linux 命令行专门设计了很多非常有用的命令行工具在你的电脑上查找文件。下面我们看一下它们其中三个:lstree 和 find

ls

如果你知道文件在哪里,你只需要列出它们或者查看有关它们的信息,ls 就是为此而生的。

只需运行 ls 就可以列出当下目录中所有可见的文件和目录:

   
   
     
  1. $ ls

  2. Documents Music Pictures Videos notes.txt

添加 -l 选项可以查看文件的相关信息。同时再加上 -h 选项,就可以用一种人们易读的格式查看文件的大小:

   
   
     
  1. $ ls -lh

  2. total 60K

  3. drwxr-xr-x 2 adam adam 4.0K Nov 2 13:07 Documents

  4. drwxr-xr-x 2 adam adam 4.0K Nov 2 13:07 Music

  5. drwxr-xr-x 2 adam adam 4.0K Nov 2 13:13 Pictures

  6. drwxr-xr-x 2 adam adam 4.0K Nov 2 13:07 Videos

  7. -rw-r--r-- 1 adam adam 43K Nov 2 13:12 notes.txt

ls 也可以搜索一个指定位置:

   
   
     
  1. $ ls Pictures/

  2. trees.png wallpaper.png

或者一个指定文件 —— 即便只跟着名字的一部分:

   
   
     
  1. $ ls *.txt

  2. notes.txt

少了点什么?想要查看一个隐藏文件?没问题,使用 -a 选项:

   
   
     
  1. $ ls -a

  2. . .bash_logout .bashrc Documents Pictures notes.txt

  3. .. .bash_profile .vimrc Music Videos

ls 还有很多其他有用的选项,你可以把它们组合在一起获得你想要的效果。可以使用以下命令了解更多:

   
   
     
  1. $ man ls

tree

如果你想查看你的文件的树状结构,tree 是一个不错的选择。可能你的系统上没有默认安装它,你可以使用包管理 DNF 手动安装:

   
   
     
  1. $ sudo dnf install tree

如果不带任何选项或者参数地运行 tree,将会以当前目录开始,显示出包含其下所有目录和文件的一个树状图。提醒一下,这个输出可能会非常大,因为它包含了这个目录下的所有目录和文件:

   
   
     
  1. $ tree

  2. .

  3. |-- Documents

  4. | |-- notes.txt

  5. | |-- secret

  6. | | `-- christmas-presents.txt

  7. | `-- work

  8. | |-- project-abc

  9. | | |-- README.md

  10. | | |-- do-things.sh

  11. | | `-- project-notes.txt

  12. | `-- status-reports.txt

  13. |-- Music

  14. |-- Pictures

  15. | |-- trees.png

  16. | `-- wallpaper.png

  17. |-- Videos

  18. `-- notes.txt

如果列出的太多了,使用 -L 选项,并在其后加上你想查看的层级数,可以限制列出文件的层级:

   
   
     
  1. $ tree -L 2

  2. .

  3. |-- Documents

  4. | |-- notes.txt

  5. | |-- secret

  6. | `-- work

  7. |-- Music

  8. |-- Pictures

  9. | |-- trees.png

  10. | `-- wallpaper.png

  11. |-- Videos

  12. `-- notes.txt

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

   
   
     
  1. $ tree Documents/work/

  2. Documents/work/

  3. |-- project-abc

  4. | |-- README.md

  5. | |-- do-things.sh

  6. | `-- project-notes.txt

  7. `-- status-reports.txt

如果使用 tree 列出的是一个很大的树状图,你可以把它跟 less 组合使用:

   
   
     
  1. $ tree | less

再一次,tree 有很多其他的选项可以使用,你可以把他们组合在一起发挥更强大的作用。man 手册页有所有这些选项:

   
   
     
  1. $ man tree

find

那么如果不知道文件在哪里呢?就让我们来找到它们吧!

要是你的系统中没有 find,你可以使用 DNF 安装它:

   
   
     
  1. $ sudo dnf install findutils

运行 find 时如果没有添加任何选项或者参数,它将会递归列出当前目录下的所有文件和目录。

   
   
     
  1. $ find

  2. .

  3. ./Documents

  4. ./Documents/secret

  5. ./Documents/secret/christmas-presents.txt

  6. ./Documents/notes.txt

  7. ./Documents/work

  8. ./Documents/work/status-reports.txt

  9. ./Documents/work/project-abc

  10. ./Documents/work/project-abc/README.md

  11. ./Documents/work/project-abc/do-things.sh

  12. ./Documents/work/project-abc/project-notes.txt

  13. ./.bash_logout

  14. ./.bashrc

  15. ./Videos

  16. ./.bash_profile

  17. ./.vimrc

  18. ./Pictures

  19. ./Pictures/trees.png

  20. ./Pictures/wallpaper.png

  21. ./notes.txt

  22. ./Music

但是 find 真正强大的是你可以使用文件名进行搜索:

   
   
     
  1. $ find -name do-things.sh

  2. ./Documents/work/project-abc/do-things.sh

或者仅仅是名字的一部分 —— 像是文件后缀。我们来找一下所有的 .txt 文件:

   
   
     
  1. $ find -name "*.txt"

  2. ./Documents/secret/christmas-presents.txt

  3. ./Documents/notes.txt

  4. ./Documents/work/status-reports.txt

  5. ./Documents/work/project-abc/project-notes.txt

  6. ./notes.txt

你也可以根据大小寻找文件。如果你的空间不足的时候,这种方法也许特别有用。现在来列出所有大于 1 MB 的文件:

   
   
     
  1. $ find -size +1M

  2. ./Pictures/trees.png

  3. ./Pictures/wallpaper.png

当然也可以搜索一个具体的目录。假如我想在我的 Documents 文件夹下找一个文件,而且我知道它的名字里有 “project” 这个词:

   
   
     
  1. $ find Documents -name "*project*"

  2. Documents/work/project-abc

  3. Documents/work/project-abc/project-notes.txt

除了文件它还显示目录。你可以限制仅搜索查询文件:

   
   
     
  1. $ find Documents -name "*project*" -type f

  2. Documents/work/project-abc/project-notes.txt

最后再一次,find 还有很多供你使用的选项,要是你想使用它们,man 手册页绝对可以帮到你:

   
   
     
  1. $ man find


via: https://fedoramagazine.org/commandline-quick-tips-locate-file/

作者:Adam Šamalík[2] 选题:lujun9972 译者:dianbanjiu 校对:wxy

本文由 LCTT 原创编译,Linux中国 荣誉推出


登录查看更多
1

相关内容

【实用书】学习用Python编写代码进行数据分析,103页pdf
专知会员服务
190+阅读 · 2020年6月29日
一份简明有趣的Python学习教程,42页pdf
专知会员服务
76+阅读 · 2020年6月22日
【实用书】Python机器学习Scikit-Learn应用指南,247页pdf
专知会员服务
257+阅读 · 2020年6月10日
【实用书】Python爬虫Web抓取数据,第二版,306页pdf
专知会员服务
115+阅读 · 2020年5月10日
【干货书】流畅Python,766页pdf,中英文版
专知会员服务
223+阅读 · 2020年3月22日
【干货】大数据入门指南:Hadoop、Hive、Spark、 Storm等
专知会员服务
94+阅读 · 2019年12月4日
【电子书】C++ Primer Plus 第6版,附PDF
专知会员服务
83+阅读 · 2019年11月25日
如何编写完美的 Python 命令行程序?
CSDN
5+阅读 · 2019年1月19日
Python用法速查网站
Python程序员
17+阅读 · 2018年12月16日
Python3.8新特性概览
Python程序员
4+阅读 · 2018年12月8日
Python | Jupyter导出PDF,自定义脚本告别G安装包
程序人生
7+阅读 · 2018年7月17日
教你用Python来玩跳一跳
七月在线实验室
6+阅读 · 2018年1月2日
别@微信团队了,我用Python给自己戴上了圣诞帽!
10个深度学习软件的安装指南(附代码)
数据派THU
17+阅读 · 2017年11月18日
3D Deep Learning on Medical Images: A Review
Arxiv
12+阅读 · 2020年4月1日
Arxiv
24+阅读 · 2020年3月11日
Mesh R-CNN
Arxiv
4+阅读 · 2019年6月6日
Object Detection in 20 Years: A Survey
Arxiv
48+阅读 · 2019年5月13日
3D-LaneNet: end-to-end 3D multiple lane detection
Arxiv
7+阅读 · 2018年11月26日
Deep Learning for Generic Object Detection: A Survey
Arxiv
13+阅读 · 2018年9月6日
VIP会员
相关VIP内容
相关资讯
如何编写完美的 Python 命令行程序?
CSDN
5+阅读 · 2019年1月19日
Python用法速查网站
Python程序员
17+阅读 · 2018年12月16日
Python3.8新特性概览
Python程序员
4+阅读 · 2018年12月8日
Python | Jupyter导出PDF,自定义脚本告别G安装包
程序人生
7+阅读 · 2018年7月17日
教你用Python来玩跳一跳
七月在线实验室
6+阅读 · 2018年1月2日
别@微信团队了,我用Python给自己戴上了圣诞帽!
10个深度学习软件的安装指南(附代码)
数据派THU
17+阅读 · 2017年11月18日
相关论文
3D Deep Learning on Medical Images: A Review
Arxiv
12+阅读 · 2020年4月1日
Arxiv
24+阅读 · 2020年3月11日
Mesh R-CNN
Arxiv
4+阅读 · 2019年6月6日
Object Detection in 20 Years: A Survey
Arxiv
48+阅读 · 2019年5月13日
3D-LaneNet: end-to-end 3D multiple lane detection
Arxiv
7+阅读 · 2018年11月26日
Deep Learning for Generic Object Detection: A Survey
Arxiv
13+阅读 · 2018年9月6日
Top
微信扫码咨询专知VIP会员