Reputation: 55
I need to rename over 1500 *.jpg files. I have text file with filenames list with 2 columns, tab separated:
Old filenames - new filenames
How i can check first 4 digits from first column and old filename and rename to another 4 digits from 2 column?
//sorry for my english
Upvotes: 1
Views: 1543
Reputation: 13610
I already posted this answer for another question but this one seemed more relatable in terms of the question, the keywords and the complexity, I will added it here too.
You can use small linux app name krename, with javascript plugin turned on. It is a free tool with very powerful renaming capabilities.
Go to plugins tab and add your javascript function to function definitions section, something like this:
var files = [
"Mickey",
"Donald",
"Duffy"
];
function rename(){
// krename_index is one of many special variables which can be added via ui
return files[krename_index];
}
This is a simple script that gets the job done, but it can be as complex as you like.
Go to filename tab and call your function in the template input as shown below:
[js; rename()]
You can prefix the above code with $
to keep the original file name and add to it. You can use "Functions" button to experiment further.
Upvotes: 1
Reputation: 274532
Use a loop as shown below:
while read -r old new
do
arr=( ${old}_*.jpg )
if (( ${#arr[@]} == 1 ))
then
mv "${arr[0]}" "$new.jpg"
else
echo "Error: Multiple files found for $old: ${arr[@]}"
fi
done < file
Note that there is a safety check to ensure that you don't have multiple files with the same prefix. For example, if you have 1305_1.jpg
and 1305_2.jpg
you can't rename them both to 1979.jpg
, so the script will print an error.
Upvotes: 4
Reputation: 3806
Try this, and remove echo after verify it's that you want. changefile
is the name of the file that contains changes
while read from to; do
echo "mv ${from}* $to"
done < changefile
You should reverse order changefile to ensure that 1306 is applied BEFORE than 130 and BEFORE than 13
Upvotes: 2