rename files with sed (rearanging filenames)
Emily Wong
I have a bunch of files named like
some_random_str1 - Lecture X of Y some_random_str2.mp4I would like to change these names to
Lecture X of Y - some_random_str1 some_random_str2.mp4only constant pattern in names in Lecture X of Y where X and Y are numbers and may be different in different names
any idea for oneliner with sed or other regex tool?
12 Answers
Using the rename (Debian/ubuntu) or prename (RedHat/CentOS)(aka "Larry Wall's Perl rename") command from your usual repository:
rename -n 's/(.*) - (Lecture \d+ of \d+) (.*).mp4/$2 - $1 $3.mp4/' *.mp4-nis a "dry run". Remove or replace by-vto actually execute the command- Assuming X and Y are numbers
- The expression can be anything Perl would accept
- Single quotes necessary around the expression to limit the use of backslashes.
Do cd into the folder holding the files.
then run (possibly with ./ replaced with a differing path, instead of cd above) :
find ./ -type f | while read n ; do nn="$( echo "$n" | sed -re 's/(.*) - (Lecture [0-9]+ of [0-9]+) (.*).mp4/\2 - \1 \3.mp4/' )"; echo "mv '$n' '$nn'" ; done
Verify that the commands generated do look OK.
If you find all is good; run the same again, but with | bash appended.