Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

sed to exclude directories

Tags:

sed

I try to replace many files at once with sed using * as filename. However it tries to process directories too, and gives error and terminates. Is there a simple way to overcome this?

like image 592
paul simmons Avatar asked Nov 04 '10 07:11

paul simmons


People also ask

How to exclude in sed?

If you are doing a substitution with sed but need to exclude a specific line or pattern, that can be accomplished by prefixing an exclusion and using “!”. For example, to substitute “hello” for “goodbye” in the following content, but to skip any line containing “world”, use syntax like the following: $ sed "/world/!

How to exclude some directory in find command?

We can exclude directories by using the help of “path“, “prune“, “o” and “print” switches with find command. The directory “bit” will be excluded from the find search!


1 Answers

I'm not sure exactly how you're using sed here but the normal way to process only regular files in UNIX is with the find command, something like:

find . -type f -exec sed 's/Hello/Goodbye/g' {} ';'

The type restricts you to regular files, not directories or FIFOs or any other sort of filesystem magic.

If you run man find on your system, you will see a plethora of other options you can use.

like image 99
paxdiablo Avatar answered Sep 18 '22 14:09

paxdiablo