find】 在指定目录下查找文件【linux】


格式

find 路径 [参数] 文件名

-name :文件名称符合 name 的文件

-iname :文件名称符合 name 的文件,iname 会忽略大小写

-type:找出符合文件类型的文件

-pid:找出对应的进程id的文件

-size [+ -]:按照指定大小搜索文件(c为字节、k为KB单位、M为MB单位、G为GB单位)

-perm [/|-]mode:根据权限查找(mode:精确权限匹配;/mode:任何一类用户(u,g,o)的权限中的任何一位(r,w,x)符合条件即满足; -mode:每一类用户(u,g,o)的权限中的每一位(r,w,x)同时符合条件即满足。)

-uid :按照用户 ID 査找所有者是指定 ID 的文件

-gid :按照用户组 ID 査找所属组是指定 ID 的文件

-user :按照用户名査找所有者是指定用户的文件

-group :按照组名査找所属组是指定用户组的文件

文件类型:

f/- 普通文件 保存数据

d 目录文件 存放文件

l 符号链接文件 指向其他文件

b 块设备 文件 访问设备

c 字符设备文件 访问设备

p 管道文件 进程间通信

s 套接字文件 进程间通信

示例

不区分大小写的扩展名查找文件

find . -iname "*.jpg"

查找目录

find . -type d

查找文件

find . -type f

按八进制权限查找文件

find . -type f -perm 777

查找设置了 setuid 位的文件

find . -xdev ( -perm -4000 ) -type f -print0 | xargs -0 ls -l

查找扩展名为“.txt”的文件并将其删除

find ./path/ -name '*.txt' -exec rm '{}' \;

查找扩展名为“.txt”的文件并在其中查找字符串

find ./path/ -name '*.txt' | xargs grep 'string'

查找大于 5 Mb 的文件并按大小排序

find . -size +5M -type f -print0 | xargs -0 ls -Ssh | sort -z

找到大于2MB的文件并将其列出

find . -type f -size +20000k -exec ls -lh {} \; | awk '{ print $9 ":" $5 }'

查找超过7天前修改的文件并列出文件信息

find . -type f -mtime +7d -ls

查找一个用户拥有的符号链接并列出文件信息

find . -type l –user=username -ls

搜索和删除空目录

find . -type d -empty -exec rmdir {} \;

搜索名为build的目录,最大深度为2个目录

find . -maxdepth 2 -name build -type d

搜索所有不在.git目录下的文件

find . ! -iwholename '.git' -type f

查找与 MY_FILE_HERE 具有相同节点(硬链接)的所有文件

find . -type f -samefile MY_FILE_HERE 2>/dev/null

查找当前目录下的所有文件并修改其权限

find . -type f -exec chmod 644 {} \;

查找扩展名为“.txt”的文件并使用 vim 编辑所有文件。vim对所有文件只启动一次。

find . -iname '*.txt' -exec vim {} +

find】 【linux】


To find files by case-insensitive extension (ex: .jpg, .JPG, .jpG):

find . -iname "*.jpg"

To find directories:

find . -type d

To find files:

find . -type f

To find files by octal permission:

find . -type f -perm 777

To find files with setuid bit set:

find . -xdev ( -perm -4000 ) -type f -print0 | xargs -0 ls -l

 To find files with extension '.txt' and remove them:

find ./path/ -name '*.txt' -exec rm '{}' \;

To find files with extension '.txt' and look for a string into them:

find ./path/ -name '*.txt' | xargs grep 'string'

To find files with size bigger than 5 Mb and sort them by size:

find . -size +5M -type f -print0 | xargs -0 ls -Ssh | sort -z

To find files bigger thank 2 MB and list them:

find . -type f -size +20000k -exec ls -lh {} \; | awk '{ print $9 ": " $5 }'

To find files modified more than 7 days ago and list file information

find . -type f -mtime +7d -ls

To find symlinks owned by a user and list file information

find . -type l --user=username -ls

To search for and delete empty directories

find . -type d -empty -exec rmdir {} \;

To search for directories named build at a max depth of 2 directories

find . -maxdepth 2 -name build -type d

To search all files who are not in .git directory

find . ! -iwholename '*.git*' -type f

To find all files that have the same node (hard link) as MY_FILE_HERE

find . -type f -samefile MY_FILE_HERE 2>/dev/null

To find all files in the current directory and modify their permissions

find . -type f -exec chmod 644 {} \;

To find files with extension '.txt.' and edit all of them with vim

vim is started only once for all files

find . -iname '*.txt' -exec vim {} +


腾图小抄 SCWY.net v0.03 小抄561条 自2022-01-02访问368381次