I have a series of files I need to rename which all have the pattern " (*)" in them. Eg;
001 (1).txt
002 (1).txt
003 (2).txt
My desired output is to rename the files to 001.txt, 002.txt, 003.txt. Note the real files vary in length and extension type (this is my test batch).
I have searched the net and SE and come up with the following which gives me the desired output in the terminal.
ls *\(*\)* | sed 's/\(.*\)\( (.*)\)\(.*\)/mv & \1\3/'
mv 001 (1).txt 001.txt
mv 002 (1).txt 002.txt
mv 003 (2).txt 003.txt
However once I pipe it to sh I get the error below.
sh: line 1: syntax error near unexpected token `('
sh: line 1: `mv 001 (1).txt 001.txt'
I know it has to do with the \( (.*)\)
section. I've tried escaping the internal parentheses but then that doesn't give me the desired output. I've also searched for regex ways to say search for the whitespace followed by any 3 characters but have had no luck.
I know there are easier ways to achieve what I want but I went with sed as thus far it was the only thing I could vaguely understand. Very much appreciate any input into getting the above line working and then considering easier approaches.
(
and)
and ended up with something likefor file in ./*; do mv "$file" "${file/ \(*\)/}"; done
– lmsurprenant Dec 05 '19 at 19:34