Find and rename all folders with name X.


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Find and rename all folders with name X.
# 1  
Old 05-16-2007
Find and rename all folders with name X.

Is there a command I can use to rename all directories with a certain name to a new name. For instance from my root directory I want to change all folders named '123' to '321' that are in the root directory or any subdirectory.
Thanks in advance!
# 2  
Old 05-16-2007
mkingrey,
Since this will rename directories, please, first try the following
displaying it only.
Do not remove the comments in the "mv" line until you are completely
satisfied with the displays:
Code:
PrevName="123"
NewName="321"
for iFile in `find . -type d -name $PrevName`
do
  oFile=`echo $iFile | sed "s/\(.*\)$PrevName/\1${NewName}/"`
  echo "iFile = <"${iFile}"> oFile = <"$oFile">"
  #######mv $iFile $oFile
done


Last edited by Shell_Life; 05-16-2007 at 05:22 PM..
# 3  
Old 05-16-2007
PrevName="123"
NewName="321"
for iFile in `find . -type d -name $PrevName`
do
oFile=`echo $iFile | sed "s/\(.*\)$PrevName/\1${NewName}/"`
echo "iFile = <"${iFile}"> oFile = <"$oFile">"
#######mv $iFile $oFile
done



Thanks, I am actually trying to rename directories instead of files...will the above work for what I need it for? (I just changed the type from f to d)
# 4  
Old 05-16-2007
Mkingrey,
Try it.
With the comment in the "mv" statement, it will only display.
# 5  
Old 05-17-2007
Probably need to add the "-depth" option to the find command else once a directory is renamed it will invalidate any renames that need to happen within it.

Code:
find . -type d -depth -name $PrevName

# 6  
Old 06-06-2007
How can I make this prompt me to enter the PrevName and NewName when the program is first run?

Also do I just need to save this somewhere on my unix box use chmod filename to compile and then enter the file name at the command prompt?

Thanks!!
# 7  
Old 06-06-2007
Mkingrey,
One way to prompt for a variable:
Code:
echo "Enter name: "
read mVar

 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Advanced & Expert Users

Find files in specific folders

Hi Team, I am new to the linux commands and I really need help . I would be really thankful if I can get some inputs. I have below folders in the path "/home/temp" 20170428 20170427 20170429 changes tempI need to get the files generated in the last 15 mins in all the above folders... (4 Replies)
Discussion started by: JackJinu
4 Replies

2. Shell Programming and Scripting

Recursivly rename folders removing characters as required

HI guys here's hoping some on pout the can help I have a large library of epub and mobi file creates some what by calibre. Output of tree listing below I would like to recursively rename the directories removing the brackets and numbers I have been scratching my head over... (4 Replies)
Discussion started by: dunryc
4 Replies

3. Shell Programming and Scripting

Moving files to folders and rename

Hello! I am new to this. I have many files from b_ap00 to b_ap80, and I need to move them to folder 00 to 80 respectively, where b_ap00 is in folder 00, b_ap01 is in folder 01. On top of this, I need to rename the file once they are inside the folder to b_ot, and subsequently run it (ifort -o... (8 Replies)
Discussion started by: krustytherusty
8 Replies

4. Shell Programming and Scripting

Find and Rename files using (find mv and sed)

In response to a closed thread for degraff63 at https://www.unix.com/shell-programming-scripting/108882-using-mv-find-exec.html the following command might do it as some shells spit it without the "exec bash -c " part: Find . -name "*.model" -exec bash -c "mv {} \`echo {} | sed -e 's//_/g'\`"... (0 Replies)
Discussion started by: rupert160
0 Replies

5. UNIX for Dummies Questions & Answers

Rename files to indexnumber in multiple folders

I have multiple subfolders with multiple jpg I want the group of files in each subfolder renamed to their index number (3 digits) in the folder, so: folder/a.jpg = folder/001.jpg folder/b.jpg = folder/002.jpg folder/c.jpg = folder/003.jpg folder2/1.jpg = folder2/001.jpg folder2/2.jpg =... (2 Replies)
Discussion started by: cmanniche
2 Replies

6. UNIX for Dummies Questions & Answers

Find folders that do NOT contain a certain file

I'm no linux guru so any help would be greatly appreciated! I need to output all folders that do not contain a file of a certain extension. Currently I have the following find / ! -name '*.txt' -printf %h\\n This doesn't work because although it finds folders that do not contain *.txt,... (2 Replies)
Discussion started by: hodnov
2 Replies

7. Shell Programming and Scripting

Find empty folders

In current folder, there are many subfolders, subfolder's subfolders... under it. How can I find out the empty folders with no files in it. I only need the top folder list. For example, I have folders like below: a/b/c a/b/x/x.txt a/s a/s/y I need get the folder a/s, but not... (6 Replies)
Discussion started by: rdcwayx
6 Replies

8. UNIX for Dummies Questions & Answers

Removing empty folders using 'find'

Hey there! I try to use 'find' to remove empty directories like this: find . -depth -type d -empty -exec rm -rf {} ';' It works just fine, but there are some directories i want to exclude. So i tried to do sth like this: find . -depth -type d -empty -exec grep -v "not this one please" -exec... (5 Replies)
Discussion started by: deTTo
5 Replies

9. UNIX for Advanced & Expert Users

find only folders

is there an option in find command to search only for folders (not subfolders). thx (1 Reply)
Discussion started by: melanie_pfefer
1 Replies

10. UNIX for Advanced & Expert Users

Script to Find Diff from two folders

Hi I would like to find the diff between two folders: Ex: Folder:1 html.java go.java ten.java Folder:2html.java go.java ten.java you.java Questions comes: Folder:1 contains a files with old version, know the files were modified and updated with the new version in Folder2. I... (2 Replies)
Discussion started by: gkrishnag
2 Replies
Login or Register to Ask a Question