views:

92

answers:

1

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.xxx
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?

+1  A: 

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.

Christian Jonassen
yes. That's correct. there would be a number at the end of each file that I want to rename.
GeoffreyF67
So the script won't have to read your list of filenames then? All prefixes are the same in the set of files, and are to be changed into the same? Or not?
Christian Jonassen
Sorry - the prefixes are the same in the files that are in the folder. The prefixes that are in the file are not.
GeoffreyF67
Then how do you know what file to rename? By the number at the end?
Christian Jonassen
It doesn't really matter. I just want to get one random file to rename to a name that's in the text file.
GeoffreyF67
Updated my answer. If it doesn't do exactly what you like, please explain and I will fix it. :)
Christian Jonassen