In a folder, i've 600 files, numbered from 001 to 600. It looks like foo_001.bar. In a text file, i've the number & titles of this folder. Now i want to rename foo_001.bar with the corresponding 001 title foobar from the text file.
But i don't have a clue how to do this properly on Linux Mint. Can someone help me or give me a tip?
Content of the titles.txt looks like this. With a tab (can be altered easy off course) between the number and the title.
001 title of 1
002 this is 2
003 and here goes 3
004 number four
005 hi this is five
etc
Content of the folder looks like this. No exceptions.
file_001.ext
file_002.ext
file_003.ext
file_004.ext
file_005.ext
etc
Just loop through your file with read
, get the seperated columns with awk
cut
(Thank you, @Jack) and mv
your file accordingly. In this very simple implementation I assume that your text file containing the new names is located at ./filenames
and your script is called from the directory containing your files.
#!/bin/bash
while IFS='' read -r line || [[ -n "$line" ]]; do
NR=$(echo "$line" | cut -f 1)
NAME=$(echo "$line" | cut -f 2)
if [ -f "foo_${NR}.ext" ] ; then
mv "foo_${NR}.ext" "$NAME"
fi
done < filenames