missing之找各种东西的技巧---1

2021-01-18  本文已影响0人  墨道院

在我们使用Linux过程中,查找各种文件,代码的活动占据很大比重,所以此文专门对查找方法做个总结和罗列。主要分为三大块:

查找文件

一般情况下,最基本的查找命令就是find命令。find很强大,基本可以满足日常查找文件的需求,他可以递归得在某一目录下按照某种方式去找到符合条件的文件,下面几个例子基本cover了大多数场景:

# Find all directories named src
find . -name src -type d
# Find all python files that have a folder named test in their path
find . -path '*/test/*.py' -type f
# Find all files modified in the last day
find . -mtime -1
# Find all zip files with size in range 500k to 10M
find . -size +500k -size -10M -name '*.tar.gz'

find命令除了列出符合条件的文件,还能对其进行一些操作,例子如下:

# Delete all files with .tmp extension
find . -name '*.tmp' -exec rm {} \;
# Find all PNG files and convert them to JPG
find . -name '*.png' -exec convert {} {}.jpg \;

我们可以看出来:-exec参数后面就是对结果文件列表所做的操作,那个大括号{}就代表前面find命令找到的文件列表。

上一篇 下一篇

猜你喜欢

热点阅读