Skip to content

Instantly share code, notes, and snippets.

@nrocy
Created September 1, 2011 15:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nrocy/1186377 to your computer and use it in GitHub Desktop.
Save nrocy/1186377 to your computer and use it in GitHub Desktop.
Why doesn't this work?
# this doesn't work
find . -type f -iname '*rhtml' -exec echo git mv {} `echo {} | sed 's/rhtml$/html.erb/i'` \;
# output
..
git mv ./pictures/_form.rhtml ./pictures/_form.rhtml
..
# but this works
find . -type f -iname '*rhtml' | while read i; do echo git mv $i `echo $i | sed 's/rhtml$/html.erb/i'`; done
# output
..
git mv ./pictures/_form.rhtml ./pictures/_form.html.erb
..
@nrocy
Copy link
Author

nrocy commented Sep 1, 2011

Bizarrely the following works, in so far as the manually added trailing 'rhtml' is replaced properly.

find . -type f -iname '*rhtml' -exec echo git mv {} echo {}rhtml | sed 's/rhtml$/html.erb/i' ;

Makes me suspect that echo {} isn't doing what I think it is..

@nrocy
Copy link
Author

nrocy commented Sep 1, 2011

Here's why it doesn't work..

  1. The backticks are evaluated by the shell beforehand.

echo {} outputs literal {}

  1. "{}" won't match the sed regex "rhtml$" so no substitution takes place

This makes the -exec block: echo git mv {} {}

  1. Find runs and replaces the {}s with filenames

Output: git mv ./pictures/_form.rhtml ./pictures/_form.rhtml

@Svenito
Copy link

Svenito commented Sep 1, 2011

I believe the problem is that the backticked expression is evaluated by the shell before the find command is run.

@Svenito
Copy link

Svenito commented Sep 1, 2011

Why you gotta type so quickly?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment