execute all files under a directory with sed and find

I'm trying to use find to execute all the files under a directory. I have a sed script that will replace all instances of # with //.

Assume my directory is /path/to/directory and it contains file1, file2, file3.

Using find and sed:

find /path/to/directory -type f -exec sed -f file.sed {} \;

How do I make it to run ALL files (file1, file2, file3) under path/to/directory?

3

1 Answer

Your find command looks fine and should work, provided that the sed command in file.sed is correct. Since you don't show us your file.sed, we can't be sure that it is correct, so here is what its contents should look like for replacing all instances of # with //:

's|#|//|g'

Notice that | is used as a delimiter instead of / for readability purposes (we dont' have to escape each of the / for the replacement).

What you also need to do is to modify the files in place using the -i sed flag.

So your command should be:

find /path/to/directory -type f -exec sed -i -f file.sed {} \;

For such a simple replacement, however, using a sed file seems like an overkill. In this case I would run the find command as:

find /path/to/directory -type f -exec sed -i -e 's|#|//|g' {} \;

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

You Might Also Like