Solved

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

Posted on 2007-12-03
13
1,023 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

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 …
The following is a collection of cases for strange behaviour when using advanced techniques in DOS batch files. You should have some basic experience in batch "programming", as I'm assuming some knowledge and not further explain the basics. For some…
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…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

726 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