Solved

how to use find to replace text in files of subdirectories but not hidden subdirectories?

Posted on 2007-12-03
13
1,015 Views
Last Modified: 2010-04-21
Using unix.

I am using this command to change word1 to word2 for all subdirectories and files

find . -name "*.*" | xargs perl -pi -e 's/word1/word2/g'

I got this command from one of the great experts on this site earlier.

however, now I would like to modify the command to exclude hidden subdirectories.

Your help would be very much appreciated
0
Comment
Question by:rark
  • 5
  • 3
  • 2
  • +2
13 Comments
 
LVL 48

Accepted Solution

by:
Tintin earned 500 total points
ID: 20397915
find . -name "*.*" | xargs perl -pi -e 's/word1/word2/g'

is much better written as

find . -type f | xargs perl -pi -e 's/word1/word2/g'

The former will attempt to replace strings in directory names and other non-files.

The following will ignore any hidden files or directories.

find . -type f | grep -v "/\." | xargs perl -pi -e 's/word1/word2/g'

0
 
LVL 40

Expert Comment

by:evilrix
ID: 20399718
I'm just wondering if sed might be a little faster?

Something like this should do: -

find . -type f | grep -v "/\." | xargs sed -i 's/word1/word2/g'

You might also want to consider placing a word boundary (\b) around word1 to ensure it is only modified if it is actually a full word and not part of a longer word: -

find . -type f | grep -v "/\." | xargs sed -i 's/\bword1\b/word2/g'

NB. All credit to Tintin for his original solution
0
 
LVL 48

Expert Comment

by:Tintin
ID: 20400687
evilrix.

rark, didn't specify what Unix/Linux flavour they were using, so they may not have GNU sed available.
0
 
LVL 40

Expert Comment

by:evilrix
ID: 20401418
Sure, but clearly the same could be argued for grep too; however, both are normally available on Unix so I think it is reasonable to make the assumption in each case :)

I just though it was worth pointing out that in the scheme of things, if available, sed might be quicker. I do; however, recognize you have already answered his question correctly and hence I called that out to ensure the OP was aware of this.

Thanks Tintin.
0
 
LVL 48

Expert Comment

by:Tintin
ID: 20401461
Sure grep and sed are available on all Unix systems, but not many Unix flavours come standard with GNU grep and sed.
0
 
LVL 40

Expert Comment

by:omarfarid
ID: 20401696
Hi,

Tintin:

Do you think the modified command below will get rid of the hidden directories only?


find . -type f | grep -v "/\..*/" | xargs perl -pi -e 's/word1/word2/g'
0
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 
LVL 48

Expert Comment

by:Tintin
ID: 20401736
omarfarid.

Will still display hidden files in subdirectories.
0
 
LVL 40

Expert Comment

by:omarfarid
ID: 20401748
Yes,

The original requirement is to exclude hidden subdirectories not hidden files.
0
 
LVL 48

Expert Comment

by:Tintin
ID: 20401780
Ahh, very true.

'bout time we heard from rark.
0
 
LVL 39

Expert Comment

by:Adam314
ID: 20404961
You could also use the -exec option instead of piping to xargs:
    find . -type f -exec perl -pi -e 's/word1/word2/g' \{\} \;
Functionally, this will do nothing different
0
 

Author Closing Comment

by:rark
ID: 31412406
Hi Tintin,
Thank you and the others very much for this quick and helpful solution.  sorry for the delay in testing.  I was booked all day yesterday.
0
 

Author Comment

by:rark
ID: 20421316
Many thanks to everyone!  I really appreciate your help.  I do have sed but have not used that command before.
0
 
LVL 40

Expert Comment

by:evilrix
ID: 20421357
>> I do have sed but have not used that command before
Sed: Stream Editor (geddit?). For manipulating text data files it is very powerful. It's worth putting in the little effort to learn how to use it (or at least the basics).

http://www.grymoire.com/Unix/Sed.html

-Rx.
0

Featured Post

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

This Windows batch file is useful for organizing image files from a digital camera or other source, but can have many other uses.  It simply renames the file(s) to match their create date.  For example, if you took a picture today at 1:40pm and the …
On Microsoft Windows, if  when you click or type the name of a .pl file, you get an error "is not recognized as an internal or external command, operable program or batch file", then this means you do not have the .pl file extension associated with …
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

760 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now