Bulk rename in sh
I had a directory of files like "really long name with spaces 14.jpg"
that I wanted to rename to files like "14.jpg"
.
My solution was the following.
ls -d *jpg | sed 's/.* \([0-9]\+\.jpg\)/mv "\0" \1/' | sh
So I can decipher this later:
- The
sed
filter replaces the lines in thels
output withmv
commands from original to stripped filename; these are then piped tosh
. - Parens and the Kleene plus must be escaped.
sed
uses basic regexp which isn't supposed to use Kleene plus but somehow it works. Adding-r
for extended makes it complain.\0
expands to the entire matched string,\1
expands to the matched parenthesized group.