Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
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!

quick and dirty:
#!/bin/ksh
find . -type -D -name 123 -print |while read i
do
d1=$(dirname $i)/321mv $i $d1
doneThe script assumes that the sub-directories are not in use, that you permission to rename directories, and that directory names contain no white space.

If I wanted to create copies of the old dirs with the new names I'm assuming I can use:
#!/bin/ksh
find . -type -D -name 123 -print |while read i
do
d1=$(dirname $i)/321cp $i $d1
done

No, that's a bad assumption. Copying directory structures is completely different from renaming directories. Tar or cpio are typically used. Consider this discussion:
The tape archive command, tar, can be used to copy all files and directories from one location to another. To copy all files and directories from under /usr/nails to /usr/fred, type:# untested
cd /usr/nails; tar cf - .|(cd /usr/fred; tar xf - )This command changes to the source directory and creates a tar archive. The "f" option with a dash means use standard output and the period includes the current directory in the archive. Since this command is piped, the standard output of the archive becomes the standard input of the tar extraction creating the required copy. The parenthesis groups commands before the tar extraction takes place.
The copy input-output command, cpio, can also be used to copy directories:
# untested
cd /usr/nails; find . -depth -print|cpio -pd /usr/fredThe pass mode, "-p," of cpio allows a list of files piped from the standard input to be copied to the target directory, /usr/fred. Be sure to change to the source directory and then do a find. A find from root such as
find /usr/nails -depth -print...
will create a directory structure under /usr/fred with the full path of the source directory. For example, a file /usr/nails/sample would be moved as /usr/fred/usr/nails/sample.

I added inputs to catch the folder names I want to copy and name, but aside from that its the same as your script. The script appears to be finding and outputting the correct folders on the screen but it does not actually make the copies. Any ideas?
Thx
#!/bin/kshecho "Enter Export Resp ID: "
read PrevName
echo "Enter Import Resp ID: "
read NewNamefind . -type d -name $PrevName -print |while read i
do
d1=$(dirname $i)/$NewNamemv $i $d1
done

![]() |
![]() |
![]() |

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.
| Ads by Google |