Tuesday, August 11, 2015

bash - Linux Bulk rename media files and directories to place parentheses around the year at the end of the filename and directory name

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

contains:


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)

contains


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 {} \;"

No comments:

Post a Comment

linux - How to SSH to ec2 instance in VPC private subnet via NAT server

I have created a VPC in aws with a public subnet and a private subnet. The private subnet does not have direct access to external network. S...