Take the 2-minute tour ×
Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

I need to scan all directories & files ander / directory And to rename the host - "node1" with "node_STAR" in file name or directory name

Can some one have brilliant idea how to do that maybe with find or perl ( in order to insert this to my shell script) Any way it's must be very fast in spite we scananing all Linux directories and files

example on - file

     /var/tmp/node1 will be /var/tmp/node_STAR file

example on - directory

     /etc/my_hosts/node1  will be /etc/my_hosts/node_STAR dir
share|improve this question

2 Answers 2

Something like:

rename 's/node1/node_STAR/' `find /var/tmp /etc/my_hosts -name 'node1'`
share|improve this answer

Since passing the filenames via a backtick-call of find might fail if the number of affected files is large, it's better to use xargs:

find . -name '*node1*'  -print0 | xargs -0 rename 's/node1/node_STAR/'

This command will replace all file and directory names containing the string node1starting at the current directory (.). If you want to replace only exact file names, remove the asterisks from the find parameter, and to restrict it to files or directories, use the parameter type -f or type -d respectively.

share|improve this answer
    
Using a backtick call will also fail if filenames include spaces, but so will the command shown in your answer. Use find ... -print0 | xargs -0 ... –  Dennis Williamson Jan 20 '11 at 16:41
    
Hi dennis - how it can be file with space? –  yael Jan 20 '11 at 17:10
    
@Dennis: Yep, you are right. Edited it accordingly. Thanks. –  Sven Jan 20 '11 at 17:41
    
@yael: It's easy: Just enter touch 'myfile with space' and you have a file name with two spaces in it. Unfortunately, it's a perfectly legal character for filenames. Unfortunately because it leads to way too many problems if you are not careful, as I demonstrated myself :( –  Sven Jan 20 '11 at 17:44
1  
@yael: In Linux and Unix, any character except for null (0x00) and slash (/) can be in a filename, including space, tab, newline, carriage return and non-printing control characters. –  Dennis Williamson Jan 20 '11 at 18:06

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.