Rename files based on folder name

Asked by Jim Hutchinson

I'm looking for the proper command or script that will rename many files sequentially but prefix them with the name of the folder. For example, I have photos organized in folders by date. The folder for 2006 has folders for Jan, Feb, March and so on. In these folders the pics are just named 001.jpg, 002.jpg and so on. I'd like move all the pics in the sub folders to the main 2006 folder but obviously there are a lot with the same name. I'd like to rename the pics in the Jan to be Jan_001.jpg and so on. I know I can do this folder by folder specifying the pattern but I'll have to go through a lot of folders for each year of pics.

I'd like a command that will go through the 2006 folder recursively and rename all the pics in the sub folders based on the name of the folder. Then it should move them all to the parent folder. I hope that makes sense. I know how to use find to rename or move but not how to have it choose patterns based on the name of the folder.

Thanks.

Question information

Language:
English Edit question
Status:
Solved
For:
Ubuntu Edit question
Assignee:
No assignee Edit question
Solved by:
Nikoleta Verbeck
Solved:
Last query:
Last reply:
Revision history for this message
François Tissandier (baloo) said :
#1

Did you check metamorphose? It's a software to rename files. But it's not a script, so if you need to automatize everything, that's maybe not a solution.

You can find a .deb here:
http://file-folder-ren.sourceforge.net/

Revision history for this message
Jim Hutchinson (jphutch) said :
#2

Thanks for the tip. I installed that but I don't think it will do what I want - at least I don't see how. I'm betting there is a CLI command or sequence of commands that will do what I want. I just don't know what it would be.

Revision history for this message
Best Nikoleta Verbeck (nerdynick) said :
#3

Here you go jim try this out.

http://downloads.nerdynick.org/files/Python/rename.tar.gz

Example usage is included in the src

Revision history for this message
Nikoleta Verbeck (nerdynick) said :
#4

Here is the other one to try. This one goes thought a folder with multiple folders and gets the images out of there.

http://downloads.nerdynick.org/files/Python/rename2.tar.gz

Revision history for this message
Jim Hutchinson (jphutch) said :
#5

Thanks NerdyNick, that solved my question.

Revision history for this message
Jim Hutchinson (jphutch) said :
#6

Oops, should have marked the second link as that's the one that did the trick. Anyway, thanks for the help Nick. You are a python god.

Revision history for this message
spxero (spxero) said :
#7

Thank you NerdyNick! This was exactly what I was looking for!

Revision history for this message
Selene ToyKeeper (toykeeper) said :
#8

I know this is an old question, but I thought I should mention there is a stock utility to do this, called "rename". It renames files based on regular expressions. Here is an example of renaming files based on directory names... First, I made some directories with similar files:

> mkdir a b c
> touch {a,b,c}/{1,2,3}

Then, I renamed them. (run once with "-n" (dry run) to make sure it will work, then again with "-v" (verbose) to actually make the changes)

> rename -v 's/(.*)\/(.*)/$1_$2/;' */*
a/1 renamed as a_1
a/2 renamed as a_2
a/3 renamed as a_3
b/1 renamed as b_1
b/2 renamed as b_2
b/3 renamed as b_3
c/1 renamed as c_1
c/2 renamed as c_2
c/3 renamed as c_3

You can also do it with a shorter command, just by replacing slashes with underscores:

> rename -v 's|/|_|;' */*

Details on the syntax are here:

http://perldoc.perl.org/perlre.html

Revision history for this message
eli555 (emhaikera) said :
#9

Thank you Selene but the command:

>rename -v 's|/|_|;' */*

causes the contents of the folder be renamed to the folders name but the content is moved out of its original folder and into the pwd because the path is changed. The command below places the content in the original folder by keeping its path the same.

>rename -v 's/(.*)\/(.*)/$1\/$1_$2/;' */*

Revision history for this message
Selene ToyKeeper (toykeeper) said :
#10

eli555, thanks for the update. It will likely be useful for some of the dozens of people notified about this question.

However, I think the goal of the original question was to move and rename the files into their parent directory. :)

Revision history for this message
eli555 (emhaikera) said :
#11

My apologies Selene, I didn't read the question throughly. Hope the command aids someone out there.