gpt4 book ai didi

bash - 如何使用 bash 和 sed 操作文件名?

转载 作者:行者123 更新时间:2023-11-29 09:03:02 25 4
gpt4 key购买 nike

我正在尝试遍历目录中的所有文件。

我想对每个文件做一些事情(将其转换为 xml,示例中未包含),然后将文件写入新的目录结构。

for file in `find /home/devel/stuff/static/ -iname "*.pdf"`;
do
echo $file;
sed -e 's/static/changethis/' $file > newfile +".xml";
echo $newfile;
done

我想要的结果是:

$file => /home/devel/stuff/static/2002/hello.txt

$newfile => /home/devel/stuff/changethis/2002/hello.txt.xml

我该如何更改我的 sed 行?

最佳答案

如果你需要重命名多个文件,我建议使用rename命令:

# remove "-n" after you verify it is what you need
rename -n 's/hello/hi/g' $(find /home/devel/stuff/static/ -type f)

或者,如果您没有rename,试试这个:

find /home/devel/stuff/static/ -type f | while read FILE
do
# modify line below to do what you need, then remove leading "echo"
echo mv $FILE $(echo $FILE | sed 's/hello/hi/g')
done

关于bash - 如何使用 bash 和 sed 操作文件名?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7242832/

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