Script to rename directories


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Script to rename directories
# 1  
Old 05-01-2012
Script to rename directories

I am trying to create a script that will search for a directory named bob, and then rename that directory to peter. I do not want the directory path to change.

so /folders/bob

would become /folders/peter

It seems to be renaming the bob folder to peter correctly, but the peter folder ends up in the directory I am currently in. How can I make it so that it stays in the proper directory? I have been searching around for answers but other people seem to be renaming/moving their files to specific directories. Since I am searching for the file, I will not know what directory it is until the search result comes back.

Thank you for your time

This is what I tried

Code:
for i in `find . -name "bob"`
 do
      mv "$i" "peter"
 done

# 2  
Old 05-02-2012
I don't recommend using for to iterate over the output of a program.
You can use a while read loop, so that paths with spaces aren't broken. Even better, you can use find's -exec so path's with newline aren't troublesome either.

If you have bash or korn shell you can use ${var%/*} to turn /full/path/to/bob into /full/path/to, in this way you can place peter in that destination.

Code:
find . -depth -name "bob" -exec bash -c 'dest=${1%/*}; mv "$1" "$dest/peter"' _ {} \;

# 3  
Old 05-02-2012
Your script can rename:./path/to/a/lower/directory/bob
to just plain ./peter.
Code:
find . -type d -name bob |
while read dname
do
   dir=$(dirname $dname)
   mv $dname ${dir}/peter
done

 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Rename files from multiple directories along with directory indicator

Hi, Friends, i have a requirement where i need to rename my files residing in multiple sub directories and move them to one different directory along with some kind of directory indicator. For eg: test--is my parent directory and it has many files such as a1.txt a2.txt a3.txt ... (5 Replies)
Discussion started by: gnnsprapa
5 Replies

2. Shell Programming and Scripting

Rename the files in all the directories and sub-directories

Hi all, I have more than 12000 files in 46 different directories and each directory has 2 sub-directories named “dat” or “gridded”. Dat sub-directories have files with extension “jpg.dat” and gridded sub-directories have files with extension “.jpg”. I need to... (1 Reply)
Discussion started by: AshwaniSharma09
1 Replies

3. Shell Programming and Scripting

Find directories with same name and rename them

Hello Im trying to make a script in bash shell programming to find subdirectories with the same name into the same directory and rename one of them!! Could you please help me? Thanks in advance (1 Reply)
Discussion started by: BTKBaaMMM
1 Replies

4. Shell Programming and Scripting

Rename files in sub directories with sequential numbers

I can rename a file with sequential numbers from 1 to N with this script: num=1 for file in *.dat;do mv "$file" "$(printf "%u" $num).txt" let num=num+1 done The script begins with renaming a some.dat file to 1.dat.txt and goes on sequentially renaming other DAT files to... (1 Reply)
Discussion started by: shoaibjameel123
1 Replies

5. Shell Programming and Scripting

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... (2 Replies)
Discussion started by: Susan_45
2 Replies

6. Shell Programming and Scripting

Rename files and directories with special characters

Hello guys, I was looking for a shell script that removes all the special characters from the files and the subdirectories recursively. I could not locate it any more. Dose any body have a similar script that dose that? Thanks for the help. AV (0 Replies)
Discussion started by: avatar_007
0 Replies

7. Shell Programming and Scripting

bash script to rename multiple directories

Hello I have a directory structure with year in format 4 digits, e.g 2009, below which is month format 1 or 2 digits, e.g 1 or 12, blow which is day format 1 or 2 digits, e.g 1 or 31. I want to change the names of lots of directories to the be Year - 4 digits , e.g 2009 - No change here... (4 Replies)
Discussion started by: garethsays
4 Replies

8. Shell Programming and Scripting

Script for parsing directories one level and finding directories older than n days

Hello all, Here's the deal...I have one directory with many subdirs and files. What I want to find out is who is keeping old files and directories...say files and dirs that they didn't use since a number of n days, only one level under the initial dir. Output to a file. A script for... (5 Replies)
Discussion started by: ejianu
5 Replies

9. Shell Programming and Scripting

Rename files recursivly in specified directories

Hi, This is what I would like to do. 1. Find all directories named "ByHost" in a specified directory 2. Rename all .plist files inside "ByHost" directories This is the way I have been able to do it so far. #!/bin/sh # # Rename ByHost files # # Thomas Berglund, 13.07.08 # Get the... (2 Replies)
Discussion started by: Thomas Berglund
2 Replies

10. Shell Programming and Scripting

Rename files/directories based on their name

i have hundreds of directories that have to be renamed. the directory structure is fairly uniform which makes the scripting a little simpler. suppose i have many directories like this */*/*/*abc* (in other words i have similar directory names 3 dirs deep that all contain the pattern abc in... (8 Replies)
Discussion started by: quantumechanix
8 Replies
Login or Register to Ask a Question