如何移动命令输出的所有文件?

ber*_*436 22 command-line scripting bash command move

我有这个 grep 命令来查找文件中没有“附件”一词的文件。

grep -L -- Attachments *
Run Code Online (Sandbox Code Playgroud)

我想移动从该命令输出的所有文件。我如何在 bash 中做到这一点?我使用管道吗?我是否在完整脚本中使用更冗长的 if/then 语句?

Ant*_*hon 36

您想要做的是使用管道和 greps-Z选项:

使用 GNU grep 和 mv

grep -LZ -- Attachments * | xargs -0 mv -t target_directory
Run Code Online (Sandbox Code Playgroud)

-Z联合xargs -0柄任何文件名有特殊字符。

使用 BSD grep 和 mv(就像在 MacOS X 上一样)

grep -L --null -- Attachments * |
while IFS= read -r -d "" file; do 
    mv "./$file" target_directory
done
Run Code Online (Sandbox Code Playgroud)

在 BSD 上,grep -Z表示decompressgrep --null适用于 BSD 和 GNU。BSDmv缺少选项-t


Gra*_*eme 18

如果您知道文件名包含可能产生匹配的新行、制表符、空格或 glob 组合时没有,这对于一次性情况可能更容易:

mv $(grep -L Attachments *) dest_dir
Run Code Online (Sandbox Code Playgroud)