Rename files recursively

Forum rules
Before you post please read how to get help
CoryD
Level 1
Level 1
Posts: 11
Joined: Sun Oct 09, 2011 2:44 pm

Rename files recursively

Postby CoryD » Wed Nov 02, 2011 6:57 pm

Hello all, Ive been trying to rename my music files from an old (and insane) naming convention that went something like this "BANDNAME - Song" to just "Song" (and of course there in many different formats) After searching around I found a lot of folks doing similar things... just not quite what I need to do.

I can rename the files pretty easy with rename 's/.+ - //' *.mp3 (or whatever format) ... this does a good job of removing the "BANDNAME - " part

The trouble is that I have a folder for every artist then sub directories for each album, then the music files themselves... but there is no recursive function for the rename command.

I've tried using find... adding that rename function with echo and then copy and pasting the results to run a ton of rename commands all at once... but I just couldn't get that to work... it keeps putting the files into the main directory I was in ~/Music

I've also tried sed but I really dont know what Im doing lol. I suppose I could just do the rename command in every directory but wheres the fun and learning in that right?

I am a bit of a newbie so any help in developing some kind of script that will do this recursive rename trick would be greatly appreciated. Thanks!

CoryD
Level 1
Level 1
Posts: 11
Joined: Sun Oct 09, 2011 2:44 pm

Re: Rename files recursively

Postby CoryD » Wed Nov 02, 2011 9:32 pm

Perhaps just taking a command like this: find -type f | sed 's/.\//rename /' and somehow adding that 's/.+ - //' command after rename... then copy the results and run them? Im not sure how to add the substitution inside the substitution tho :)

This dosent work: $ find -type f | sed 's/.\//rename \'s/.+ - //\' /'

awi
Level 1
Level 1
Posts: 13
Joined: Wed Nov 23, 2011 8:23 pm
Location: Asunción, Paraguay
Contact:

Re: Rename files recursively

Postby awi » Mon Dec 26, 2011 7:33 pm

Assuming your directories names does not contains spaces, you could use these:

Code: Select all

START_DIR=$PWD; for DIR in `find -type d`; do cd $DIR; echo "current_dir for rename $PWD"; find -maxdepth 1 -type f -name "*.mp3" -print0 | xargs -0 rename -v 's/.+\-//'; cd $START_DIR; echo "current_dir $PWD";  done


Here you save the current directory, these will be your "root" directory for musics

Code: Select all

START_DIR=$PWD


With the "for" you loop through all the directories

Code: Select all

for DIR in `find -type d`

These sentences change you to each directory inside de loop

Code: Select all

 cd $DIR; echo "current_dir for rename $PWD";


Once you're in a subdirectory you rename all the files in the current directory. The "-print0" tells find to return the list with a '\0' terminator, and "-0" argument for xargs takes the output and passes to rename command.

Code: Select all

 find -maxdepth 1 -type f -name "*.mp3" -print0 | xargs -0 rename -v 's/.+\-//'

At the end of the loop, we return to the start point

Code: Select all

cd $START_DIR; echo "current_dir $PWD";

If your directories have spaces or special characters, then you'll have to use find's "-print0" option to iterate, and you might also need a special separator for use in the "for" loop.

Here an example execution:

Code: Select all

user@kate /tmp/A $ ls -R
.:
A-asdf.mp3  b  c

./b:
B-aqqqq.mp3

./c:
C-llll.mp3
user@kate /tmp/A $ START_DIR=$PWD; for DIR in `find -type d`; do cd $DIR; echo "current_dir for rename $PWD"; find -maxdepth 1 -type f -name "*.mp3" -print0 | xargs -0 rename -v 's/.+\-//'; cd $START_DIR; echo "current_dir $PWD";  done
current_dir for rename /tmp/A
./A-asdf.mp3 renamed as asdf.mp3
current_dir /tmp/A
current_dir for rename /tmp/A/c
./C-llll.mp3 renamed as llll.mp3
current_dir /tmp/A
current_dir for rename /tmp/A/b
./B-aqqqq.mp3 renamed as aqqqq.mp3
current_dir /tmp/A
user@kate /tmp/A $ ls -R
.:
asdf.mp3  b  c

./b:
aqqqq.mp3

./c:
llll.mp3


Return to “Scripts & Bash”

Who is online

Users browsing this forum: No registered users and 2 guests