繁体   English   中英

如何对目录中的所有文件执行命令,并将每个文件的输出移至新文件夹?

[英]How do I conduct a command on all the files in a directory and move the output for each file to a new folder?

我想对目录中的每个文件执行命令,并将输出存储在新目录中,以使其具有与输入相同的文件名。 我运行的命令是.pl脚本,其格式为:

test.pl输入文件输出文件

例如,我有一个名为input的目录,其中包含以下文件:testa.txt testb.txt

我运行一个for循环,在这两个文件上执行命令:

for file in /Users/test/Desktop/input
do
    test.pl $file /Users/test/Desktop/output/$file
done

但是,以这种方式提供输出路径无效。 我不断收到错误,没有这样的文件或目录。

file获得值/Users/test/Desktop/input ,因此test.pl接收/Users/test/Desktop/output//Users/test/Desktop/input作为最后一个参数。 您将要使用/Users/test/Desktop/input/*类的glob ,然后使用basename剥离目录:

for file in /Users/test/Desktop/input/*
do
    test.pl "$file" "/Users/test/Desktop/output/$(basename "$file")"
done

xargs是一种工具,可读取stdin格式的文件列表,并对获取的每个文件名执行命令。 您需要注意包含空格的文件名。 建议您提供更多详细信息,以获取更准确的答案。 手册页有很多详细信息。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM