Velvet Star Monitor

Standout celebrity highlights with iconic style.

news

rename files with sed (rearanging filenames)

Writer Emily Wong

I have a bunch of files named like

some_random_str1 - Lecture X of Y some_random_str2.mp4

I would like to change these names to

Lecture X of Y - some_random_str1 some_random_str2.mp4

only 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?

1

2 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
  • -n is a "dry run". Remove or replace by -v to 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.
2

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.

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy