开发者

How can I rename files from a list in bash?

开发者 https://www.devze.com 2022-12-31 23:41 出处:网络
I have a file that contains filenames like this: my_cool_file.xxx my_cool_file2.xxx my_cool_file3.xxx I have a folder that has:

I have a file that contains filenames like this:

my_cool_file.xxx
my_cool_file2.xxx
my_cool_file3.xxx

I have a folder that has:

some_file.x开发者_开发问答xx
some_file2.xxx
some_file3.xxx

I would like to have a bash script to take one line from the filename file and rename one file in the folder.

Is there a way to do this?


Note: This answer isn't that good generally, but it solved the OP's problem.

Do all files end with _file.xxx or _fileN.xxx?

Basically, is what you want to change just the prefix of the filenames?

Assuming I understand your problem correctly, and that the file without a number is last in file_list.txt the following should do the trick


COUNTER=0
for FILE in `ls *.xxx | sort`; do
    COUNTER=$(($COUNTER+1))
    echo Moving $FILE to `sed -n ${COUNTER}p file_list.txt`
    #mv "${FILE}" `sed -n ${COUNTER}p file_list.txt`
done

Comment in the line with mv if you want the actual move to happen.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号