Jump to content

sed on every file in every folder


Recommended Posts

Ok, so I hope you can get what I wanna do from this and tell me how to actually do it:

[urza9814@localhost CDREADY]$ sed s/ssk/..\/ssk/gw < */*.html
bash: */*.html: ambiguous redirect

 

Basically, I have a folder of folders with HTML files, and I need to change wherever it says 'ssk' to '../ssk' for all of the html files in all of the folders. How do I do that? I mean I understand why what I typed doesn't work...but yea. What will?

Edited by Urza9814
Link to comment
Share on other sites

assuming the files are in the current directory...

for file in *html
do
 sed -i "s@ssk@../ssk@g" $file
done

or shorter way with find, (but i havnt tested).

 

this one will recurse to deeper directories

find . -iname \*.html -exec sed -i "s@ssk@../ssk@g" {};

this one is the same as the last, except it won't recurse deeper, only the current directory:

find . -maxdepth 1 -iname \*.html -exec sed -i "s@ssk@../ssk@g" {};

 

Just as a tip, instead of hideously escaping forward slashes in sed statements, use a different delimiter, I use @ because it stands out, but iirc the rule is, if it isnt in the string, you can use it. Though using alphanumeric things isnt encouraged as it'd be pretty confusing.

 

James

Link to comment
Share on other sites

Guest blin

Good relevant script examples, here are a few minor additions and adjustments.

 

adjusted for command line use, -print addition to list files modified

for file in *.html; do  sed -i "s@ssk@../ssk@g" $file; done -print

 

adjustments for escape sequences on ";", execdir vs. exec, missing -iname and html ext typos, -print addition

With changes...

find . -iname \*.html -execdir sed -i "s@ssk@../ssk@g" {} ';' -print

this one is the same as the last, except it won't recurse deeper, only the current directory:

find . -maxdepth 1 -iname \*.html -execdir sed -i "s@ssk@../ssk@g" {} ';' -print

 

If the execdir command you're using permits, you should use + instead of ';' . It executes the command against a list of names rather than once per filename. Sed and most fileutils will work with lists. No need to escape +.

 

find . -iname \*.html -execdir sed -i "s@ssk@../ssk@g" {} + -print

find . -maxdepth 1 -iname \*.html -execdir sed -i "s@ssk@../ssk@g" {} + -print

 

All tested and appear to work on Mandriva 2007.1 x86_64 standard install.

 

You'll want to rewrite the reg expression in sed to be more useful. Possible fixes...

add exclude pattern so you don't clobber ../ssk into ../../ssk, this will allow you to run the script twice if needed.

adjust regex to include the larger pattern, ssk is probably not the fully defined pattern you're trying to find,

"newline" ssk, "whitespace" ssk, html : / /ssk or another more refined regex pattern is needed that won't match sssk, tssk, ../ssk or some other variant of ssk you don't want to change.

add line numbers and counts of modified files to output

 

see http://www.regular-expressions.info/examples.html for more ideas

Edited by blin
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
 Share

×
×
  • Create New...