
大家好, 我是 老麦, 一个运维老兵, 现在专注于 Golang,DevOps,云原生基础设施建设。
状态: 未更新
原文链接: https://typonotes.com/posts/2023/03/22/linux-command-find/
在Linux中,find命令可以用来查找文件和目录。下面是一些使用find命令的技巧:
find /path/to/directory -type ffind /path/to/directory -type dfind /path/to/directoryfind /path/to/directory -name "filename*"find /path/to/directory -name "*filename"find /path/to/directory -type f -exec grep -l "string" {} +find /path/to/directory -type f -mtime -5find /path/to/directory -type f -atime +5find /path/to/directory -type f -size +10Mfind 命令可以用于查找文件和目录,而 xargs 命令则可以接收来自其他命令的输出作为参数,并将这些参数提供给其他命令作为输入。这两个命令的组合使用可以非常强大。
例如,假设你想要在某个目录下查找所有的 .txt 文件并对它们执行某个操作,比如删除。你可以使用以下命令:
find /path/to/directory -name "*.txt" | xargs rm -f这个命令将查找 /path/to/directory 目录下所有以 .txt 结尾的文件,并将它们的路径传递给 xargs 命令。xargs 命令将每个文件路径作为参数传递给 rm 命令,从而删除这些文件。
你也可以使用 find 命令和 xargs 命令来执行其他操作。例如,假设你想要在某个目录下查找所有的 .txt 文件并将它们转换为 .md 文件。你可以使用以下命令:
find /path/to/directory -name "*.txt" | xargs -I{} mv {} {}.md这个命令将查找 /path/to/directory 目录下所有以 .txt 结尾的文件,并将它们的路径传递给 xargs 命令。-I{} 选项告诉 xargs 命令将每个文件路径作为占位符传递给 mv 命令。mv 命令将文件重命名为以 .md 结尾的文件。
总之, find 命令和 xargs 命令可以非常方便地组合使用,以便在 Linux 系统中执行各种文件和目录操作。
如果一个目录文件很多, 多到直接使用 rm -rf * 会卡是的情况, 使用 find + xargs 删除文件简直是绝配。
find ./ -type f | xargs -n 10 rm -f 这个命令的意思是
find 查找文件-n 10 每批次 10个文件。rm -f 强制删除不确认。