LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   How to rename a part of a file or files (https://www.linuxquestions.org/questions/linux-newbie-8/how-to-rename-a-part-of-a-file-or-files-4175461876/)

annonyxxxx 05-14-2013 02:27 AM

How to rename a part of a file or files
 
hey everybody in LQ

I have a few video files with this name structure.

TV Show Season 1 - Ep 1 title.mp4
TV Show Season 1 - Ep 2 title.mp4
TV Show Season 1 - Ep 3 title.mp4
etc..

I want to change it to this

TV Show Season 1 - Ep 01 title.mp4
TV Show Season 1 - Ep 02 title.mp4
TV Show Season 1 - Ep 03 title.mp4

What is the solution? thank you folks

pan64 05-14-2013 02:32 AM

How many files are there? Renaming a few can be done by hand...

annonyxxxx 05-14-2013 03:21 AM

I have 20 files but I want to change the first 9 files. So far I have been doing it manually but it gets tedious after a while.

parnmatt 05-14-2013 03:23 AM

Create a for-in loop over all your *.mp4's
Replace the Ep 1 with Ep 01 etc.
Rename it.

In a file:
Code:

#!/bin/sh
for file in *.mp4; do
    renamedFile=$(echo $file | sed 's/Ep \([0-9]\)/Ep 0\1/')
    echo mv \"$file\" \"$renamedFile\" | sh
done

Or in one line from the Terminal:
Code:

for file in *.mp4; do echo mv \"$file\" \"$(echo $file | sed 's/Ep \([0-9]\)/Ep 0\1/')\" | sh; done

annonyxxxx 05-14-2013 03:38 AM

you're the man parnmatt, it looks like freakin' algebra but it did the job :)

parnmatt 05-14-2013 03:45 AM

You are most welcome.
Programming/Scripting kinda is a bit like maths.

Just remember to mark all helpful posts by clicking the Yes in the bottom corner of those posts.
And don't forget to mark the thread solved (at the top).


All times are GMT -5. The time now is 03:35 AM.