find
是 Linux 和类 Unix 系统中一个功能强大的命令行工具,用于在文件系统中搜索文件和目录。它可以根据多种条件(如名称、类型、大小、修改时间等)来查找文件,并结合执行操作(如删除、复制或打印)。本文将介绍 find
命令的 30 个实用示例,帮助用户更好地掌握该命令。
find /path/to/search -name "filename.txt"
此命令将在指定路径下查找名为 filename.txt
的文件。
find /path/to/search -iname "FILENAME.TXT"
使用 -iname
选项可以忽略文件名的大小写。
.txt
文件find /path/to/search -name "*.txt"
find /path/to/search -empty
find /path/to/search -type f
此处 -type f
表示查找普通文件,如果需要查找目录,则使用 -type d
。
find /path/to/search -size +100M
此命令将查找大于 100MB 的文件。
find /path/to/search -size -10M
此命令将查找小于 10MB 的文件。
find /path/to/search -mtime -1
find /path/to/search -atime -1
find /path/to/search -ctime -1
find /path/to/search -perm 777
find /path/to/search -name "temp*" -exec rm {} \;
find /path/to/search -name "*.log" -exec gzip {} \;
find /source/path -name "*.jpg" -exec cp {} /destination/path/ \;
find /source/path -name "*.mp3" -exec mv {} /destination/path/ \;
find /path/to/search -type l
find /path/to/search -type d -empty
find /path/to/search -mmin -10
find /path/to/search -amin -60
find /path/to/search -cmin -1440
find /path/to/search ! -user username
find /path/to/search ! -group groupname
find /path/to/search -exec ls -lh {} \;
find /path/to/search -type f -exec du -h {} \;
find /path/to/search -type f -exec sed -i 's/old_text/new_text/g' {} \;
find /path/to/search -type f | wc -l
find /path/to/search -type d | wc -l
find /path/to/search -type f -exec du -h {} \; | sort -rh | head -n 10
find /path/to/search -type f -printf '%T+ %p\n' | sort | head -n 10
find /path/to/search -type f -printf '%T+ %p\n' | sort -r | head -n 10
通过这些示例,您可以根据实际需求灵活地使用 find
命令。记住,find
是一个非常强大且灵活的工具,熟练掌握它可以大大提高您的工作效率。