loop; passing two argument - 1:1 mapping

时间:2015-06-26 10:13:31

标签: bash shell loops

If I have 50 different files to rename (or anything, basically two arguments). How would I do that with a for loop or any other method ?

mv $1 $2

I dont want to pass the argument with each instance. $1 = ab and $2 = 1. 50 differnt filenames.

3 个答案:

答案 0 :(得分:1)

假设您有一个要重命名的当前目录中的txt文件列表:

for f in *.txt; do mv "$f" "renamed_$f"; done

我建议在do之后添加echo以查看命令首先如何实际执行,然后在看起来很好的情况下删除echo。

对于常见的文件重命名任务,您可能还需要考虑使用mmv,它允许您更轻松地进行基于模式的文件重命名。

答案 1 :(得分:0)

Assuming you have filenames.txt like this:

oldname1 newname1
oldname2 newname2

you can do like this:

xargs -L1 mv <filenames.txt

答案 2 :(得分:0)

我想我得到了使用数组的答案

OLD=(file text filename textfile)
NEW=(files1 text3 filename2 textfile3)
for i in {0..3}; do mv ${OLD[$i]} ${NEW[$i]};done