Visit Our UNIX and Linux User Community


rename direcories recusively


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting rename direcories recusively
# 1  
Old 08-15-2009
rename direcories recusively

Hi everone

I need to rename my directory tree. I want to define the renaming by regular expressions so i am free to what ever I like to do.

how would I rename an path like this one:

/thE/Long/patH-tO/my.litte/filE.TxT

to

/the/long/path_to/my_little/file.txt

I tried this one, but it does not realy work:
Code:
for f in `find $1 -type d`; do
echo $f
g=`expr "$f" : '\(.*\)' | tr '[A-Z]' '[a-z]'`
mv "$f" "$g"
done

thank you already Smilie
# 2  
Old 08-15-2009
Code:
g=`expr "$f" : '\(.*\)' | tr '[A-Z]' '[a-z]'`

you can try replacing this line with
Code:
g=$(echo "$f" | tr [A-Z] [a-z])

# 3  
Old 08-15-2009
still this does not work.
it needs to run recursively BUT have the perviously renamed directory name on hand.
if it does not have the "new" name it will not -find- it in the next renaming step.

any ideas?
# 4  
Old 08-15-2009
EDIT: The code has been modified so only the child directory is modified.
Here is a sh script that will surely work:
Code:
#!/bin/sh

cat /dev/null > tmp
for f in $(find -type d); do
  echo "$f" >> tmp
done

cat /dev/null > tmp2
while read line; do
  depth=$(echo "$line" | grep -o "/" | wc -l)
  echo "$depth|$line" >> tmp2
done < tmp

sort -nr tmp2 > tmp
while read line; do
  folder=$(echo "$line" | cut -d'|' -f2)
    parent=${folder%/*}
    child=$(echo ${folder##*/} | tr [A-Z] [a-z])
  newfolder=$(echo "$parent/$child")
  echo mv "$folder" "$newfolder"
done < tmp

exit 0

It's a bit hackish but the trick is to rename deeper directories first
- The first block of code creates a tmp file containing what find finds
- The second block calculates the depth of the directories and writes to tmp2, for each directory, a line in the form "5|/path/to/the/direc/tory". Then the file is sorted so deeper directory are first in the file tmp
- The third block actually rename each child directory with a pattern of choice
Remove the echo on the last line in the third loop to rename the files

I hope you'll find this script useful!

Last edited by tukuyomi; 08-15-2009 at 06:41 PM..
# 5  
Old 08-15-2009
i will try your script in a minute...

here is what I came up with...

but i can not "cd" in the for loop.(the commented line in function stepin)..any ideas?

Code:
#!/bin/bash

MYNAME=$0
FROM=$1
TO=$2

function list(){
# lists all directories on the current level
for f in `find ./ -type d`; do echo $f; done | cut -d / -f 1,2 | uniq | tail -n +2
}

function rename(){
# renames all directories on this level
# then calls "stepin" which moves on directorylevel deeper
for l in `list`
    do
    n=`echo $l | sed "s/${FROM}/${TO}/g"`
    if [ "$l" != "$n" ];then
        echo "RENAMING:        " $l " --> " $n
        mv $l $n
        echo ""
    fi
done
stepin
}

function stepin(){
# lists all directories on the current level to "cd" into them
# from here "one level deeper" rename does his job again
echo "stepping in..."
#for s in `list`; do echo "next dir is: "$s "it contains"; ls -l $s; done
for s in `list`; do echo "next dir is: "$s "it contains"; ls -l $s; cd $s; rename; done
}

echo "OLD"; list; echo "OLD_END"
rename
echo "NEW"; list; echo "NEW_END"

thank you so much Smilie
# 6  
Old 08-16-2009
I added a bit of code next to your for s in `list` loop to go back to the parent directory, so ls and cd won't fail.
Code:
#At the top of the script
CURRDIR="$PWD"

#your script....

for s in `list`; do echo "next dir is: "$s "it contains"; ls -l $s; cd $s; rename; done; 
cd ..
#Do not go back more than the working directory
if [ "$PWD" = "$CURRDIR" ]; then exit 0; fi

# 7  
Old 08-16-2009
thank you so much ...
it took me a night and your help...but now it s working.
but onestly...i realy need you to explain to me what your lines do?

cd ..

moeves on directory backwards so from /home/user to /home
but don't I want to move forward?! at least that is waht the function "stepin" is supposed to do" so e.g. /home/user/ to /home/user/direcorty_in_home

this is twisted..sorry I dont get it....could you PELASE comment so I get out this with having learned something?!

Smilie thank you

---------- Post updated at 07:03 AM ---------- Previous update was at 06:52 AM ----------

tested it ...still not working all the way
see the underscores in "level_2, level_3 and level_4" of the first directory?

I am tired...why is it not working Smilie

this explains it all...Image

Previous Thread | Next Thread
Test Your Knowledge in Computers #1010
Difficulty: Easy
The number 200 in base 8 equals 128 in base 10.
True or False?

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Using rename

Renaming files using rename on the following files rename 's/.99999/.99999.sac.pzs/g' *.99999 sac_pzs_iv_esml_hhz__2013.074.10.18.23.0000_2599.365.23.59.59.99999 sac_pzs_iv_favr_hhe__2010.187.00.00.00.0000_2599.365.23.59.59.99999... (1 Reply)
Discussion started by: kristinu
1 Replies

2. UNIX for Dummies Questions & Answers

Using rename

I want to rename files Files show like this 1977SSD_rsdtst.pdf 1976SDP_rstdtsr.pdf 1943FDT_rstdsrt.pdf 1996DFF_stdstrd.pdf I want to introduce _ after the year, and move the characters to the end to get 1977_rsdtst_SSD.pdf 1976_rstdtsr_SDP.pdf 1943_rstdsrt_FDT.pdf... (4 Replies)
Discussion started by: kristinu
4 Replies

3. Shell Programming and Scripting

How to use function 'rename' ?

hello, all I have following files: file_1 file_2 file_3 ... file_9 Now I want to rename them as: file_001 file_002 file_003 ... file_009 how to use function 'rename' to accomplish this task? (2 Replies)
Discussion started by: littlewenwen
2 Replies

4. Shell Programming and Scripting

rename file

Hi, I have files in the format thread_1_seq_1965.440.755943413 thread_1_seq_1966.577.755943443 thread_2_seq_2034.383.388388888 I want to rename this in the form of 1_1965.arc 1_1966.arc 2_2034.arc Discard everything except the th char and . The character position... (3 Replies)
Discussion started by: bang_dba
3 Replies

5. UNIX for Dummies Questions & Answers

Rename a file

how to rename a file if I don't know file location ? Can we below command : $ find / | mv file1.txt I am not sure........can any1 help me out ? Thanks, Tushar Joshi:mad: (1 Reply)
Discussion started by: tusharjoshi
1 Replies

6. UNIX for Dummies Questions & Answers

need help with rename

hi guys i am writing a script to change the filename which is enterered as input to lower case letter even if one letter is upper case i have to change it to lower case i get the input and use sed comand should i use like that sed/s/a-z/A-Z/d will it be like that can u please help me (8 Replies)
Discussion started by: farhan_t49
8 Replies

7. Shell Programming and Scripting

Rename files

Hello, I've a list of file like this img_001 img_22 img_44 and I would rename all with this form photo_0001 photo_0002 photo_0003 photo_0004 suggestions?Thanks to all. (2 Replies)
Discussion started by: cv313x
2 Replies

8. Shell Programming and Scripting

rename

hi, im doin an assignment which requires you to build a shell script to do the MS DOS style equivilant (sp?) of the rename function. What i have to do is a loop which checks the following rules and output messages (same): Can't have anything after target Can't have more than one dot Can't... (1 Reply)
Discussion started by: bohoo
1 Replies

9. UNIX for Dummies Questions & Answers

how can I rename the following=-^

I have a file named -^, I want to look at it, rename, etc. Any help out there?? (5 Replies)
Discussion started by: nj78
5 Replies

10. UNIX for Dummies Questions & Answers

rename using mv ?

Hi all how can can remove the underscore from this number in this series _1234567.abc _1234567.abcd I was trying mv _1234567* 1234567 but did not work ? thanks s (2 Replies)
Discussion started by: simon2000
2 Replies

Featured Tech Videos