Rename files recursively

About writing shell scripts and making the most of your shell
Forum rules
Topics in this forum are automatically closed 6 months after creation.
Locked
CoryD

Rename files recursively

Post by CoryD »

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!
Last edited by LockBot on Wed Dec 28, 2022 7:16 am, edited 1 time in total.
Reason: Topic automatically closed 6 months after creation. New replies are no longer allowed.
CoryD

Re: Rename files recursively

Post by CoryD »

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

Post by awi »

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
Locked

Return to “Scripts & Bash”