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

JJcarter
  • 43
  • 7

2 Answers2

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' {}
anubhava
  • 761,203
  • 64
  • 569
  • 643
  • 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" – JJcarter Oct 05 '14 at 07:41
  • 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' '{}'` – anubhava Oct 05 '14 at 07:44
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.

afenster
  • 3,468
  • 19
  • 26
  • 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. – JJcarter Oct 05 '14 at 07:28
  • 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. – afenster Oct 05 '14 at 07:31
  • 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? – JJcarter Oct 05 '14 at 07:34