Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Following on from my previous question:

Remove slashes/parent paths from filenames inside CSS and Javascript content

I arrived at the following command which replaces any filenames in these files with just the basename:

grep -o -h -E '[A-Za-z0-9:./_-]+\.(png|jpg|gif|tif|css)' |\
  sed "s:[a-zA-z0-9]*/::" `find . -name '*.css' -or -name '*.js'` 

How do I save the output of this command back to the source file for each file processed?

share|improve this question

migrated from stackoverflow.com Jul 12 '11 at 18:01

This question came from our site for professional and enthusiast programmers.

The -i option in sed will make the changes to your files. Can you try modifying your bash line to use this? – Manny Jul 12 '11 at 16:38
@Manny - I get sed: 1: "./example.com-saved/example ...": invalid command code . – keith Jul 12 '11 at 17:08
possible duplicate of Why does sed act differently depending on the output file? – Gilles Jul 12 '11 at 21:27

Late answer to an old question, but I think this is the answer the OP was looking for:

SEDCMD="/[A-Za-z0-9:./_-]+\.(png|jpg|gif|tif|css)/ s:[a-zA-z0-9]*/::"
find . -name '*.css' -or -name '*.js' -exec sed -i "$SEDCMD" '{}' ';'
share|improve this answer

Try a loop, like:

find . -name '*.css' -or -name '*.js' | while read filename; do
  : $filename is the original file name
share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.