• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1029
  • Last Modified:

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

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
rark
Asked:
rark
  • 5
  • 3
  • 2
  • +2
1 Solution
 
TintinCommented:
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
 
evilrixSenior Software Engineer (Avast)Commented:
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
 
TintinCommented:
evilrix.

rark, didn't specify what Unix/Linux flavour they were using, so they may not have GNU sed available.
0
Cloud Class® Course: Microsoft Azure 2017

Azure has a changed a lot since it was originally introduce by adding new services and features. Do you know everything you need to about Azure? This course will teach you about the Azure App Service, monitoring and application insights, DevOps, and Team Services.

 
evilrixSenior Software Engineer (Avast)Commented:
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
 
TintinCommented:
Sure grep and sed are available on all Unix systems, but not many Unix flavours come standard with GNU grep and sed.
0
 
omarfaridCommented:
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
 
TintinCommented:
omarfarid.

Will still display hidden files in subdirectories.
0
 
omarfaridCommented:
Yes,

The original requirement is to exclude hidden subdirectories not hidden files.
0
 
TintinCommented:
Ahh, very true.

'bout time we heard from rark.
0
 
Adam314Commented:
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
 
rarkAuthor Commented:
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
 
rarkAuthor Commented:
Many thanks to everyone!  I really appreciate your help.  I do have sed but have not used that command before.
0
 
evilrixSenior Software Engineer (Avast)Commented:
>> 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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: SQL Server Core 2016

This course will introduce you to SQL Server Core 2016, as well as teach you about SSMS, data tools, installation, server configuration, using Management Studio, and writing and executing queries.

  • 5
  • 3
  • 2
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now