I'm trying to use a lookup table to rename files in a batch using a while loop.
Here are my filenames:
file1_a.txt
file2_a.txt
file3_b.txt
file4_b.txt
file5_c.txt
I also have a lookup table, tab-separated (rename.tsv). The first 1st column is the original string, and the 2nd column is the new string.
file1 file10
file2 file20
file3 file3
file4 file4
The expected output should be changing all the filenames as follows:
file10_a.txt
file20_a.txt
file3_b.txt
file4_b.txt
file5_c.txt
Here's my bash script. I'm using 'rename' and not 'mv' to make use of regex.
while IFS='\t' read orig new; do
origfile="$orig"
newfile="$new"
rename -n -v "s/$origfile/$newfile/" *.txt
done < rename.tsv
This script produces no output, but also no errors, so it's not clear what I'm doing wrong. Any help is appreciated.
If your rename
command is not perl based, it doesn't support "/org/new/"
syntax, which is a perl expression. Please try instead:
while IFS=$'\t' read -r orig new; do
rename -v "$orig" "$new" *.txt
done < rename.tsv
Please note the usage of $'\t'
, not '\t'
.
As a side note, your redundant variable assignments seem unnecessary.