I have a number of home video files in different directories that have a year at the end of the directory name to indicate the year that the video was filmed. It is also at the end of the filenames in that directory and before the extension. I want to put parentheses around the year.

Some files and directories may also have a year (or numbers that look like a year) in the filename in other positions, but I just want to put parenthesis around the year at the end. For example here is one of the directories:

Videos/Xmas 2004 - Time To Go 2004


Xmas 2004 - Time To To 2004.mp4  
Xmas 2004 - Time To Go 2004.txt  
Xmas 2004 - Time To Go 2004.readme  

I want to bulk rename all my media files to end up with this naming system:

Videos/Xmas 2004 - Time To Go (2004)


Xmas 2004 - Time To To (2004).mp4  
Xmas 2004 - Time To Go (2004).txt  
Xmas 2004 - Time To Go (2004).readme

How do I do this? I can use the "find" command to get the files and directories:

find /home/user/Videos/ -iname "* [1-2][0-9][0-9][0-9]"    # Find directories
find /home/user/Videos/ -iname "* [1-2][0-9][0-9][0-9].*"  # Find files

I am wondering how I can use the "-exec {} \;" structure to use the rename or mv command (or sed or awk) or do I write a bash script to manipulate the filename string and put that in the "-exec {} \;"


This script will print required mv commands to stdout. You should save them to a file (sh script > file) and inspect them. If you find them safe to execute, then you can then run the commands in file (sh file). Or you may change print to system in the script itself.

find $1 -type f > /tmp/to_rename
find $1 -type d >> /tmp/to_rename
perl -lne '
    $re = qr/([12]\d\d\d)(\.\w*)?$/;
    if (/$re/) {
        $old = $_;
        print("mv \"$old\" \"$_\"");
' /tmp/to_rename
  • @PeterFreeman if you find the answer acceptable please mark it so. Cheers. :) – pii_ke Dec 27 '16 at 12:05

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.