[英]Bash rename multiple files according to line in another file
I would like to rename multiple files according to the name specified in another file. 我想根据另一个文件中指定的名称来重命名多个文件。 For example, I have a file called names.txt , containing: 例如,我有一个名为names.txt的文件,其中包含:
Name1 Newname1
Name2 Newname2
Name3 Newname3
etc 等等
In this names.txt file the names are not numbered. 在这个names.txt文件中,名称没有编号。 However, the name in column 1 and column 2 are linked together. 但是,第1列和第2列中的名称链接在一起。 I can imaging it should be possible to loop through the file to extract both names and use it in the mv function somehow. 我可以想象,应该可以遍历文件以提取两个名称,并以某种方式在mv函数中使用它。 Thanks! 谢谢!
假设names.txt
中的名称names.txt
包含空格,则可以使用以下命令:
while read a b; do mv "$a" "$b"; done < names.txt
This will work even if you have space : 即使您有空间也可以使用:
SAVE_IFS=$IFS
IFS=$(echo -en "\n\b")
for row in `cat names.txt`
do
echo $row
cur_name=`echo $row | awk -F " " '{print $1}'`
new_name=`echo $row | awk -F " " '{print $2}'`
echo "mv $cur_name $new_name"
done
IFS=$SAVE_IFS
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.