1

I am trying to use the find and sed commands in Linux to do the following:

  1. When command is issued in current directory it will edit all ".cbf" files in all directories and sub-directories.

I have been using: this, this and this as research references.

My current command that is not working is:

find . -name "*.cbf" -print0 | xargs -0 sed -i '' -e 's/# change the header/# change the header to something/g'

The error I get is: sed: can't read : No such file or directory

I have tried the command both above the directory with the .cbf files and actually in the directory.

Can someone please help me with what I am doing wrong. I simply wish to edit a line in all .cbf files in subdirectories from where I am sitting.

Thanks in advance

1

2 Answers 2

2

Try this find/sed command:

find . -name "*.cbf" -print0 | xargs -0 -I {} sed -i.bak 's/# change the header/# change the header to something/g' {}
Sign up to request clarification or add additional context in comments.

2 Comments

This seems to work too, but same error with : File name too long. Again the files I have are as such "cal_50_01_0001.cbf"
That is not really very long. Try quoting '{}' as in: find . -name "*.cbf" -print0 | xargs -0 -I {} sed -i.bak 's/# change the header/# change the header to something/g' '{}'
2

Your command actually works. The error you see is related to -i '' part which seems wrong. The option to -i should be used to provide a suffix for backups when doing an in-place edit, and should be given without any space: -i.bak.

If you don't need backups at all, just don't give any extra option after -i. In your case sed is thinking that extra '' is a filename and is actually trying to open it (quote from strace output):

4000  open("", O_RDONLY)                = -1 ENOENT (No such file or directory)

So, the correct command should not have '' after -i.

3 Comments

So I tried this and I no longer get an error, however once the command is issued is hangs up. It seems like it is working, however I do not see any .cbf files edited.
If there are a lot of files there, find will take time. You could run just the find part, without -print0, to see how long it takes. It goes through all the files from the current directory, recursively.
when I issue without print0 it shows all my files very quick then at the end has : File name too long. Any ideas? my file name is cal_51_09_0627.cbf. Should I have a flag to allow this length?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.