如何为我找到的每个文件执行多个命令
How to perform multiple commands for each file I find
所以我在特定目录中搜索文件,对于我找到的每个文件,我想对这些文件执行一系列相同的命令。
我用这个查找命令搜索这些文件:
那么循环就是你的朋友
find . -maxdepth 1 -type f -name "file*" | while read file; do
echo $file;
# perform other operations on $file here
done
如果你不是while循环的朋友
$ ls -1 file*
file.txt
file1.txt
$ find . -maxdepth 1 -type f -name "file*" | xargs -n1 -I_val -- sh -c 'echo command1_val; echo command2_val'
command1./file.txt
command2./file.txt
command1./file1.txt
command2./file1.txt
在上面的命令中,使用 _val 代替 {} 以避免不必要的引用 (inspired by)
所以我在特定目录中搜索文件,对于我找到的每个文件,我想对这些文件执行一系列相同的命令。
我用这个查找命令搜索这些文件:
那么循环就是你的朋友
find . -maxdepth 1 -type f -name "file*" | while read file; do
echo $file;
# perform other operations on $file here
done
如果你不是while循环的朋友
$ ls -1 file*
file.txt
file1.txt
$ find . -maxdepth 1 -type f -name "file*" | xargs -n1 -I_val -- sh -c 'echo command1_val; echo command2_val'
command1./file.txt
command2./file.txt
command1./file1.txt
command2./file1.txt
在上面的命令中,使用 _val 代替 {} 以避免不必要的引用 (inspired by)