I want to rename a bunch of files using bash, transforming this file pattern:
prefix - name - suffix.txt
Into this one:
name.txt
For that I wrote the following script:
find . -name "*.txt" | while read f
do
mv "${f}" "${f/prefix - /}"
done
find . -name "*.txt" | while read f
do
mv "${f}" "${f/ - suffix/}"
done
It works, but I’d like to perform the renaming using a single loop. Is it possible?
Another approach, for fun, using regular expressions:
Actually, using the simple pattern ‘*.txt’ here has two problems:
Using
findcomplicates the procedure, but is more correct: