gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-04 12:06:45 30 4
gpt4 key购买 nike

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

test.pl 输入文件输出文件

例如,我有一个名为 input 的目录,其中包含以下文件:种皮.txt测试文件.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 作为最后一个参数。你会想要使用 glob/Users/test/Desktop/input/* 然后使用 basename 剥离目录:

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

关于linux - 如何对目录中的所有文件执行命令并将每个文件的输出移动到新文件夹?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50439691/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com