Linux 的Find 命令

find 是 Linux 下最有用的命令之一,熟练的使用它对于提高工作效率很有帮助。下面结介绍它的一些用法

1. 最基本的 用法是:

find dir

这时,它将列出目录 dir下所有的文件和目录。

它相当于 find dir -print

如果使用 print0, 那么,每个匹配的文件名字之间用一个 '\0' 分割(默认用的是换行符)。如果文件名包含空格的话,可以使用 print0

2. 按名字搜索使用 -name 选项。如果要使用两个搜索标准来搜索满足其中之一的文件,可以使用 -o (or).

ls
A file  dir1  dir2  dir3  file  file1  file2  picture1  picture2  picture3
charles@taotao:~/test$ find . \( -name "*dir*" -o -name "*file*" \) -print
./file2
./A file
./file1
./dir3
./dir1
./file
./dir2
-path选项则会把匹配的文件名或者路径名搜出来。

3. -regex 使用正则匹配。

ls 
1.txt  A file  b.mp3  dir1  dir3  file1  picture1  picture3
2.txt  a.mp3   c.avi  dir2  file  file2  picture2
charles@taotao:~/test$ find . -regex  ".*\(\.mp3\|\.avi\)$"
./a.mp3
./b.mp3
./c.avi
4. 找出不符合搜索条件的文件,可以使用 !:

$ find . ! -name "*.txt"
.
./file2
./A file
./picture3
./file1
./picture2
./dir3
./a.mp3
./dir1
./picture1
./file
./b.mp3
./dir2
./c.avi

5. 路径搜索深度。

默认下,find 会递归搜查制定路径下所有子目录。可以使用 -maxdepth 和 -mindepth指定最多要搜查到指定目录下的几级目录,从指定目录的几级目录开始搜索。

如果要仅仅搜索当前目录下的文件,使用 find . -maxdepth 1 ;如果要搜索除当前目录下的所有文件,使用 find . -mindepth 2

6. 根据文件类型搜索

用 -type 指定。 常用的有 -type f (搜索文件), -type d(目录), -type l (符号链接)

7. 根据文件时间。 -atime (上一次的访问时间),  -mtime (文件内容的修改时间), -ctime(文件属性的修改时间)。时间单位是天。数字前 +表示大于,-表示小于。

find . -type f -atime -7 -print
搜索最近7天内被访问的文件

另外还有分钟为单位的选项: -amin, -mmin, -cmin.用法类似。

8. 根据文件大小搜索。使用  -size选项。后面的数字同样可使用 +/-号。 数字后面跟后缀表示单位: b: 512 字节的 block; c: 字节; w: 2 字节; k: 1024 字节; M: 兆字节; G: 1024 兆

/media/DATA/video$ find . -type f -size +2G
./Home Alone 1990 720p BluRay DTS x264-MgB/Home Alone 1990 720p BluRay DTS x264-MgB.mkv
./My.Sassy.Girl.2001.720p.Blu-ray.x264.DTS-HDChina [PublicHD]/My.Sassy.Girl.2001.720p.Blu-ray.x264.DTS-HDChina.mkv
./The Godfather part I.mkv
./The Godfather Part 2 (1974) [1080p]/The.Godfather.Part.2.1974.1080p.BrRip.x264.BOKUTOX.YIFY.mp4
找出所有大于2G的文件。

9. 删除匹配的文件: 用  -delete

/usr/local/bin$ ls
gmplayer  mplayer  qemu-img  qemu-nbd         qemu-system-mipsel
mencoder  qemu-ga  qemu-io   qemu-system-arm  virtfs-proxy-helper
charles@taotao:/usr/local/bin$ find . -name "*qemu*" -delete
find: cannot delete `./qemu-io': Permission denied
find: cannot delete `./qemu-img': Permission denied
find: cannot delete `./qemu-ga': Permission denied
find: cannot delete `./qemu-system-arm': Permission denied
find: cannot delete `./qemu-system-mipsel': Permission denied
find: cannot delete `./qemu-nbd': Permission denied
还可以用:

:/usr/local/bin$ find . -name "*qemu*" -exec rm {} \;
rm: remove write-protected regular file `./qemu-io'? 
10.根据文件的属性(读写,执行权限)和所有者搜索. 使用 -perm 和 -user
~/test$ ls -l
drwxrwxr-x 2 charles charles 4096 Oct 24 01:47 dir1
drwxrwxr-x 2 charles charles 4096 Oct 24 01:47 dir2
drwxrwxr-x 2 charles charles 4096 Oct 24 01:24 dir3
-rw-rw-r-- 1 charles charles    0 Oct 24 01:24 file
-rw-rw-r-- 1 root    root       0 Oct 24 01:24 file1
-rw-rw-r-- 1 root    root       0 Oct 24 01:24 file2
-rw-rw-r-- 1 charles charles    0 Oct 24 01:24 picture1
-rw-rw-r-- 1 charles charles    0 Oct 24 01:24 picture2
-rw-rw-r-- 1 charles charles    0 Oct 24 01:24 picture3
charles@taotao:~/test$ find . -type f -user root
./file2
./file1
11. 在 find 中执行其他的命令。 使用 -exec

比如,把上面的owner为 root的改为 charles,:

~/test$ sudo find . -type f -user root -exec chown charles:charles {} \;
charles@taotao:~/test$ ls -l
drwxrwxr-x 2 charles charles 4096 Oct 24 01:47 dir1
drwxrwxr-x 2 charles charles 4096 Oct 24 01:47 dir2
drwxrwxr-x 2 charles charles 4096 Oct 24 01:24 dir3
-rw-rw-r-- 1 charles charles    0 Oct 24 01:24 file
-rw-rw-r-- 1 charles charles    0 Oct 24 01:24 file1
-rw-rw-r-- 1 charles charles    0 Oct 24 01:24 file2
-rw-rw-r-- 1 charles charles    0 Oct 24 01:24 picture1
-rw-rw-r-- 1 charles charles    0 Oct 24 01:24 picture2
-rw-rw-r-- 1 charles charles    0 Oct 24 01:24 picture3
{}是一个 place holder,它会被替换成搜索到的每个文件。

12. 忽略某些目录。 使用 -prune

 find . -name "dir1" -prune  -o -type f -print
列出当前目录下除  dir1目录以外的所有文件。

13. 拷贝搜索到的文件到另外一个目录。

 find . -type f  -name "*.ko" -exec cp {} ~/tmp/ \;
或者:

 find . -type f  -name "*.ko" | xargs -I {} cp {} ~/tmp 


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值