-1

find /$HOME/Desktop -name "*.dpx" -exec sed -i "" 's/Exile1/ExileR1/' {} \;

just hangs with no results. I'm unclear what the problem may be.

Buuuut,

find /$HOME/Desktop -name "*.dpx" -exec rename 's/Exile1/ExileR1/' {} \;

works fine.

Why????

I'm on MacOSX.

Bleakley
  • 653
  • 2
  • 9
  • 19
  • 3
    Sed seems to think your command is `/s/Exi...` and not `s/Exil...` - have you copy-pasted the right command? – Benjamin W. Jan 24 '17 at 00:30
  • Ah, fixed that. `s/Exil...` just hangs and nothing happens... – Bleakley Jan 24 '17 at 19:20
  • 1
    These two commands try to do different things. Sed looks at the _content_ of the files and changes the _content_; `rename` looks at the _name_ of the files and changes the _name_. Which one do you want? (See [mklement0's answer](http://stackoverflow.com/a/41837720/3266847).) – Benjamin W. Jan 24 '17 at 19:51
  • To those wondering about `-i ""` vs. just `-i`. `-i ""` is indeed necessary with BSD `sed`, the implementation used on macOS (you can read more about it [here](http://stackoverflow.com/a/40777793/45375)). – mklement0 Jan 25 '17 at 13:55

2 Answers2

0

Given that you contrast your failing sed command with rename, it looks like you're trying to rename files, whereas the sed command would perform a string substitution in the content of the files you pass; the -i option then writes the modified input back to the input file (loosely speaking).

sed cannot rename files directly, but you can use it as an auxiliary command to construct a new filename you then pass to mv:

find "$HOME/Desktop" -name "*.dpx" -print0 | 
 while IFS= read -d '' -r file; do
   echo mv "$file"' "$(sed 's/Exile1/ExileR1/' <<<"$file")"
 done

Remove echo to perform actual renaming.

mklement0
  • 382,024
  • 64
  • 607
  • 775
  • Thank you @mklement0 I noticed that running the sed command did change the checksum on my files. DPX are image files so I'm unclear what sed would have done to change the checksum... it doesn't alter the hex of a file just text content if present, right? – Bleakley Jan 25 '17 at 20:20
  • Yes, _your_ `sed` command changes the file's _content_, even if no actual substitutions take place. At the very least, BSD `sed` will append a trailing `\n` to your content. But it sounds like you just wanted to _rename_ files, not _change their content_, right? – mklement0 Jan 25 '17 at 20:52
  • I did just want to rename and thanks to your advice I did just that with rename. I just didn't understand what sed was actually doing in my command once I saw that the checksums had changed. If the content is simply an image then \n was added to the hex? @mklement0 – Bleakley Jan 25 '17 at 20:56
  • BSD `sed` always adds a trailing `\n` to its output, even if the input didn't have one, so it is fundamentally unsuitable for editing _binary_ files - _GNU_ `sed` doesn't have that problem. – mklement0 Jan 25 '17 at 21:01
-1

I don't have any .dpx files, but try adding a -e to clarify the search script. Like this...

find /$HOME/Desktop -name "*.dpx" -exec sed -i "" -e 's/Exile1/ExileR1/' {} \;
mnr
  • 434
  • 1
  • 8
  • 13
  • Please don't post such quick tips as an answer - use a comment instead. Use of `-e` makes no difference here. – mklement0 Jan 24 '17 at 19:55