Recursively rename directories


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Recursively rename directories
# 1  
Old 07-21-2011
Recursively rename directories

I have this directory tree under /apps/myapp/data:

imageshack.us/photo/my-images/703/foldersc.png

How to recursively rename ONLY directories with 5 digits (00000, 00100, 00200,..., 00007, 00107,...)?

I want to add to their name two more zeros:

Before: 00107
After: 0000107

Thanks in advance!
# 2  
Old 07-21-2011
You could do this:
Code:
find /path/to/your/dir -type d -name "[0-9][0-9][0-9][0-9][0-9]" -printf "mv %p \$(echo %p | sed 's_[0-9]*\$_00&_' ) \n"

Which will print the move command, so that you can test part of it, and if you like what you see, you just pipe it to sh, to run it:
Code:
find /path/to/your/dir -type d -name "[0-9][0-9][0-9][0-9][0-9]" -printf "mv %p \$(echo %p | sed 's_[0-9]*\$_00&_' ) \n" | sh

---------- Post updated at 01:59 AM ---------- Previous update was at 01:48 AM ----------

Alternative, and maybe more simple way:
Code:
find /path/to/dir -type d  | egrep '/[0-9]{5}$' | rename -n 's/([0-9]+)/00\1/'

which will do a dry-run, tell you what it would rename, and if you are happy, just run it by omittin the -n switch of rename.

This assumes, there is a perl-regex version of rename installed on your system. You will find out by running 'rename' without arguments; if you see something like
Code:
$ rename
Usage: rename [-v] [-n] [-f] perlexpr [filenames]

Then you're good to go.
# 3  
Old 07-25-2011
Thanks, I will try the first method (I do not have a perl-regex version of rename installed).
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Find and rename file recursively

Hi, I have a directory which contains multiple files with .txt extension, i want to rename all these file to .bak extension using find command, this is what i've tried, please help me to correct this : find /home/application/test -name '*.txt' -exec rename 's/txt/bak/' {} \; seems to... (8 Replies)
Discussion started by: mukulverma2408
8 Replies

2. Shell Programming and Scripting

Recursively Searcing file in the directories

i have directory dgf in the dgf( some other Sub-dir are there) 00 01 02 03 04 in all the Sub directory there is a SG.csv .. i want the scripts should run one by one Sub-dir and print the result for that particular Sub-dir ..then go to next Sub-Dir and print the result....... please... (6 Replies)
Discussion started by: Aditya.Gurgaon
6 Replies

3. Shell Programming and Scripting

Recursively rename some files

Hello, I have one directory with 3 level sub-directories, and about houndard files under those directories. I need a shell script to rename all patern mateched directories and files. For example: the patern is AA in the directory or file name. Orignal directory:... (2 Replies)
Discussion started by: mail4mz
2 Replies

4. UNIX for Dummies Questions & Answers

List directories and sub directories recursively excluding files

Hi, Please help me, how to get all the direcotries, its sub directories and its sub directories recursively, need to exclude all the files in the process. I wanted to disply using a unix command all the directories recursively excluding files. I tried 'ls -FR' but that display files as... (3 Replies)
Discussion started by: pointers
3 Replies

5. Shell Programming and Scripting

Rename files recursively

hi I have files named 123_234_aaa.jpg 234_231_345.jpg and i wish to rename these files to aaa.jpg and 345.jpg. i.e inital number,_,next number should be removed form the file name. Please let me know how can i do it. (2 Replies)
Discussion started by: vasuarjula
2 Replies

6. UNIX for Advanced & Expert Users

Recursively delete only specified directories with given pattern

Hi All, We have a requirement to recursively delete the directories and its subdirectories older than 60 days based on timestamp (folder creation timestamp)under certain directory. However it has some specific requirements. The directories will continue to be there upto any depth. the... (0 Replies)
Discussion started by: rcvasu
0 Replies

7. UNIX for Dummies Questions & Answers

Batch rename recursively

I would like to replace multiple underscores with hyphens but I have 26,000 files to rename. They are all in one file structure and multiple sub-directories. It would be much simpler if I had a script to do it. Here are some samples of the file names: Example 1... (3 Replies)
Discussion started by: ..Chris..
3 Replies

8. UNIX for Dummies Questions & Answers

How to display directories recursively?

Cannot find how to list the directory structure of a volume recursively. Do not want the files reported. Say I have 100 directories and 10,000 files, I do not want 10,000 lines of output. (If this is relevant, I am using the terminal on my OSX Mac). I hope this is easy - there should be an easy... (5 Replies)
Discussion started by: jwriter
5 Replies

9. UNIX for Dummies Questions & Answers

Recursively deleting directories

Say I have a directory call test, and several directories nested in it, and several directories nested in them. And I want to remove all directories within "test" and its subdirectories that have the name "cvs", how can I do this? I tried rm -r cvs, but that only removed the top level direcotry... (4 Replies)
Discussion started by: mikeshank
4 Replies
Login or Register to Ask a Question