linux的31个find命令
-
find命令是Linux系统中的一个非常强大的文件查找工具。它可以根据指定的条件在文件系统中查找文件并进行相关操作。下面是关于find命令的31个常用用法:
1. 查找指定名称的文件:
find /path/to/directory -name “filename”2. 查找指定后缀名的文件:
find /path/to/directory -name “*.extension”3. 查找匹配多个条件的文件:
find /path/to/directory -name “filename” -type f -size +10M4. 查找除指定名称外的文件:
find /path/to/directory ! -name “filename”5. 查找符号链接文件:
find /path/to/directory -type l6. 查找指定用户的文件:
find /path/to/directory -user username7. 查找指定用户组的文件:
find /path/to/directory -group groupname8. 查找某个时间段内修改过的文件:
find /path/to/directory -mtime -79. 查找大于指定大小的文件:
find /path/to/directory -size +10M10. 查找所有空文件:
find /path/to/directory -empty11. 查找所有具有指定权限的文件:
find /path/to/directory -perm 77712. 查找在指定目录下的文件:
find /path/to/directory -path “dirname”13. 查找最近访问过的文件:
find /path/to/directory -atime -714. 查找最近修改过的文件:
find /path/to/directory -mtime -715. 查找最近变动过的文件:
find /path/to/directory -ctime -716. 查找大于指定时间的文件:
find /path/to/directory -newermt “2021-01-01”17. 查找并执行命令:
find /path/to/directory -exec command {} \;18. 查找并显示文件详细信息:
find /path/to/directory -ls19. 查找指定类型的文件:
find /path/to/directory -type f20. 查找指定目录及其子目录下的文件:
find /path/to/directory -type f -name “filename” -print21. 查找指定目录下的文件数目:
find /path/to/directory -type f | wc -l22. 查找指定大小范围的文件:
find /path/to/directory -size +10M -size -100M23. 查找修改时间在指定日期之后的文件:
find /path/to/directory -newermt “2021-01-01”24. 查找并删除匹配文件:
find /path/to/directory -type f -name “filename” -delete25. 查找并归档文件到指定目录:
find /path/to/directory -type f -name “filename” -exec mv {} /path/to/archive \;26. 查找并压缩文件到指定目录:
find /path/to/directory -type f -name “filename” -exec tar -czvf /path/to/archive.tar.gz {} \;27. 查找并列出文件的大小:
find /path/to/directory -type f -exec du -sh {} \;28. 查找并按大小排序文件:
find /path/to/directory -type f -exec du -sh {} \; | sort -rh29. 查找并统计文件个数:
find /path/to/directory -type f | wc -l30. 查找并统计文件大小总和:
find /path/to/directory -type f -exec du -c {} + | grep total$31. 查找并修改文件权限:
find /path/to/directory -type f -exec chmod 644 {} \;通过这31个find命令的使用示例,我们可以更好地利用Linux系统中的find命令,快速、方便地查找和操作文件。希望这些例子可以帮助您更好地理解和使用find命令。
2年前 -
find命令是Linux中一个非常强大的工具,用于在目录树中查找文件和目录。它提供了多种选项和参数,使其非常灵活和功能强大。以下是31个常用的find命令:
1. 查找文件名为file.txt的文件:
`find /path/to/directory -name file.txt`2. 查找所有以.txt结尾的文件:
`find /path/to/directory -name “*.txt”`3. 查找所有大于10MB的文件:
`find /path/to/directory -size +10M`4. 查找所有小于1KB的文件:
`find /path/to/directory -size -1K`5. 查找所有修改时间在7天之前的文件:
`find /path/to/directory -mtime +7`6. 查找所有权限为777的文件:
`find /path/to/directory -perm 777`7. 查找所有目录:
`find /path/to/directory -type d`8. 查找所有文件:
`find /path/to/directory -type f`9. 查找所有空目录:
`find /path/to/directory -type d -empty`10. 查找所有用户是root的文件:
`find /path/to/directory -user root`11. 查找所有属组是users的文件:
`find /path/to/directory -group users`12. 查找所有大于10天且权限为可执行的文件:
`find /path/to/directory -mtime +10 -perm /a=x`13. 查找所有以”abc”开头的文件:
`find /path/to/directory -name “abc*”`14. 查找所有不以”abc”开头的文件:
`find /path/to/directory ! -name “abc*”`15. 查找所有符号链接文件:
`find /path/to/directory -type l`16. 查找所有不可读的文件:
`find /path/to/directory ! -readable`17. 查找所有不可写的文件:
`find /path/to/directory ! -writable`18. 查找所有不可执行的文件:
`find /path/to/directory ! -executable`19. 查找最近访问时间在7天之内的文件:
`find /path/to/directory -atime -7`20. 查找最近修改时间在7天之内的文件:
`find /path/to/directory -mtime -7`21. 查找最近状态变化时间在7天之内的文件:
`find /path/to/directory -ctime -7`22. 查找所有属主为root、属组为users、权限为777的文件:
`find /path/to/directory -user root -group users -perm 777`23. 查找所有以.txt结尾且大于10MB的文件:
`find /path/to/directory -name “*.txt” -size +10M`24. 查找所有属主为root或属组为users的文件:
`find /path/to/directory \( -user root -o -group users \)`25. 查找所有大于10MB且属主不是root的文件:
`find /path/to/directory -size +10M ! -user root`26. 查找所有在10天之前修改过的文件,并将其删除:
`find /path/to/directory -mtime +10 -exec rm {} \;`27. 查找所有文件,并将它们移动到另一个目录:
`find /path/to/directory -type f -exec mv {} /path/to/destination/ \;`28. 查找所有文件,并在每个文件名末尾添加一个后缀:
`find /path/to/directory -type f -exec sh -c ‘mv “$0” “$0_new”‘ {} \;`29. 查找所有文件,并将它们复制到另一个目录:
`find /path/to/directory -type f -exec cp {} /path/to/destination/ \;`30. 查找所有文件,并将它们打包为一个压缩文件:
`find /path/to/directory -type f -exec tar -czvf archive.tar.gz {} +`31. 查找所有文件,并将它们删除:
`find /path/to/directory -type f -exec rm {} +`以上是31个常见的find命令示例,不同的选项和参数可以组合使用来满足不同的需求。使用find命令可以方便地在Linux系统中查找和操作文件和目录。
2年前 -
一、介绍
find命令是Linux系统中一个非常强大的工具,用于在指定目录下搜索满足条件的文件。它的使用非常灵活,可以根据文件名、文件类型、文件大小、文件权限等多种条件来进行筛选。本文将介绍31个常用的find命令,分为以下几个方面进行讲解:文件名相关的find命令、文件类型相关的find命令、文件大小相关的find命令、文件权限相关的find命令、其他常用的find命令等。
二、文件名相关的find命令
1. 按文件名搜索
“`
find /path/to/dir -name “filename”
“`
在指定的目录 /path/to/dir 及其子目录中搜索名为 “filename” 的文件。
2. 按文件名模式搜索
“`
find /path/to/dir -name “*.txt”
“`
使用通配符 * 来匹配多个字符,搜索以 .txt 结尾的文件。
3. 按正则表达式搜索
“`
find /path/to/dir -regex “.*\.txt$”
“`
使用正则表达式匹配文件名,搜索以 .txt 结尾的文件。
4. 忽略大小写搜索
“`
find /path/to/dir -iname “filename”
“`
在搜索文件名时,忽略大小写。三、文件类型相关的find命令
5. 按文件类型搜索
“`
find /path/to/dir -type f
“`
搜索普通文件。
6. 按目录类型搜索
“`
find /path/to/dir -type d
“`
搜索目录。
7. 按链接类型搜索
“`
find /path/to/dir -type l
“`
搜索符号链接。
8. 按文件类型和大小搜索
“`
find /path/to/dir -type f -size +10M
“`
搜索大于10MB的普通文件。四、文件大小相关的find命令
9. 按文件大小搜索
“`
find /path/to/dir -size 10k
“`
搜索大小为10KB的文件。
10. 按文件大小范围搜索
“`
find /path/to/dir -size +10M -size -100M
“`
搜索大小在10MB和100MB之间的文件。
11. 按文件大小单位搜索
“`
find /path/to/dir -size +10M
“`
通过指定单位(k/M/G)来搜索文件大小。五、文件权限相关的find命令
12. 按文件权限搜索
“`
find /path/to/dir -perm 644
“`
搜索文件权限为 644 的文件。
13. 按文件权限位搜索
“`
find /path/to/dir -perm /u=rwx,g=rx,o=r
“`
搜索文件权限位为 u=rwx, g=rx, o=r 的文件。
14. 按文件访问权限搜索
“`
find /path/to/dir -readable
find /path/to/dir -writable
find /path/to/dir -executable
“`
分别搜索可读、可写、可执行的文件。六、其他常用的find命令
15. 按文件创建时间搜索
“`
find /path/to/dir -newermt “2021-01-01”
“`
搜索在指定日期之后创建的文件。
16. 按文件访问时间搜索
“`
find /path/to/dir -atime +7
“`
搜索访问时间在7天之前的文件。
17. 按文件修改时间搜索
“`
find /path/to/dir -mtime -7
“`
搜索修改时间在7天之内的文件。
18. 按文件状态修改时间搜索
“`
find /path/to/dir -ctime 1
“`
搜索文件状态修改时间在1天之前的文件。
19. 按文件所有者搜索
“`
find /path/to/dir -user username
“`
搜索文件所有者为指定用户的文件。
20. 按文件所属组搜索
“`
find /path/to/dir -group groupname
“`
搜索文件所属组为指定的组的文件。
21. 按文件属主根据用户ID搜索
“`
find /path/to/dir -uid userid
“`
搜索文件属主为指定用户ID的文件。
22. 按文件属组根据组ID搜索
“`
find /path/to/dir -gid groupid
“`
搜索文件属组为指定组ID的文件。
23. 限制搜索深度
“`
find /path/to/dir -maxdepth 2
“`
限制搜索的深度为2级目录。
24. 排除特定目录
“`
find /path/to/dir -not -path “/path/to/exclude/*”
“`
排除特定目录 /path/to/exclude 及其子目录。
25. 指定搜索范围
“`
find /path/to/a -name ‘*.txt’ -o -path “/path/to/b/*”
“`
指定搜索范围为目录 /path/to/a 及其子目录,或者目录 /path/to/b 及其子目录。
26. 使用find结果执行操作
“`
find /path/to/dir -name ‘*.txt’ -exec cp {} /path/to/dest \;
“`
根据搜索结果执行命令,将符合条件的文件复制到指定目录。七、其他进阶用法
27. 查找含有指定内容的文件
“`
find /path/to/dir -type f -exec grep -l “keyword” {} \;
“`
查找在指定目录及其子目录中含有关键词 “keyword” 的文件。
28. 查找并删除指定类型的文件
“`
find /path/to/dir -type f -name “*.txt” -exec rm {} \;
“`
查找并删除指定目录及其子目录中的名为 “*.txt” 的文件。
29. 查找并统计指定类型的文件数量
“`
find /path/to/dir -type f -name “*.txt” | wc -l
“`
查找并统计指定目录及其子目录中名为 “*.txt” 的文件的数量。
30. 查找并修改文件权限
“`
find /path/to/dir -type f -exec chmod 644 {} \;
“`
查找并修改指定目录及其子目录中的文件权限为 644。
31. 查找并压缩文件
“`
find /path/to/dir -type f -name “*.txt” -exec tar -czvf archive.tar.gz {} +
“`
查找并将指定目录及其子目录中的名为 “*.txt” 的文件压缩为 archive.tar.gz 文件。总结:
本文总结了31个常用的find命令,涵盖了文件名、文件类型、文件大小、文件权限等多个方面。通过学习和掌握这些命令,可以更加灵活地利用find命令进行文件搜索和操作。希望可以对您有所帮助。2年前