Computing.Net > Forums > Unix > Find and replace directories

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

Find and replace directories

Reply to Message Icon

Name: mking (by mkingrey)
Date: May 16, 2007 at 10:49:14 Pacific
OS: XP
CPU/Ram: N/A
Product: N/A
Comment:

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!




Sponsored Link
Ads by Google

Response Number 1
Name: nails
Date: May 16, 2007 at 12:47:29 Pacific
Reply:

Is this a unix question? Your OS says XP


0

Response Number 2
Name: mking (by mkingrey)
Date: May 16, 2007 at 12:54:55 Pacific
Reply:

Yes its UNIX -that is in there by default.

Thanks.


0

Response Number 3
Name: nails
Date: May 16, 2007 at 13:37:07 Pacific
Reply:

quick and dirty:

#!/bin/ksh

find . -type -D -name 123 -print |while read i
do
d1=$(dirname $i)/321

mv $i $d1
done

The script assumes that the sub-directories are not in use, that you permission to rename directories, and that directory names contain no white space.


0

Response Number 4
Name: mking (by mkingrey)
Date: May 16, 2007 at 14:09:27 Pacific
Reply:

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)/321

cp $i $d1
done


0

Response Number 5
Name: nails
Date: May 16, 2007 at 14:29:34 Pacific
Reply:

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/fred

The 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.


0

Related Posts

See More



Response Number 6
Name: mking (by mkingrey)
Date: June 11, 2007 at 09:50:34 Pacific
Reply:

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/ksh

echo "Enter Export Resp ID: "
read PrevName
echo "Enter Import Resp ID: "
read NewName

find . -type d -name $PrevName -print |while read i
do
d1=$(dirname $i)/$NewName

mv $i $d1
done


0

Sponsored Link
Ads by Google
Reply to Message Icon






Post Locked

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


Go to Unix Forum Home


Sponsored links

Ads by Google


Results for: Find and replace directories

One more Q. (find and replace) www.computing.net/answers/unix/one-more-q-find-and-replace/5826.html

Need help with a search and replace logi www.computing.net/answers/unix/need-help-with-a-search-and-replace-logi/3296.html

unix script - find and replace www.computing.net/answers/unix/unix-script-find-and-replace/5816.html