Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

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

Posted on 2007-12-03
13
Medium Priority
?
1,025 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 2000 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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
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

Enroll in October's Free Course of the Month

Do you work with and analyze data? Enroll in October's Course of the Month for 7+ hours of SQL training, allowing you to quickly and efficiently store or retrieve data. It's free for Premium Members, Team Accounts, and Qualified Experts!

Question has a verified solution.

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

Recently, an awarded photographer, Selina De Maeyer (http://www.selinademaeyer.com/), completed a photo shoot of a beautiful event (http://www.sintjacobantwerpen.be/verslag-en-fotoreportage-van-de-sacramentsprocessie-door-antwerpen#thumbnails) in An…
Checking the Alert Log in AWS RDS Oracle can be a pain through their user interface.  I made a script to download the Alert Log, look for errors, and email me the trace files.  In this article I'll describe what I did and share my script.
Six Sigma Control Plans
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…

636 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