1

I am trying to apply two command (echo and haml --check) to result of my find.

haml --check `find . -name "*.haml"`
# return Syntax error on line 2: Illegal nesting: nesting within plain text is illegal.
# but I don't know which file

echo `find . -name "*.haml"`
# returns list of files (space separated) 

Ideally I want to print the file name and then output of haml --check of that file in stdout.

7

Also a way without xargs:

find . -name "*.haml" -ls -exec haml --check {} \;

to print out only file name with path:

find . -name "*.haml" -print -exec haml --check {} \;
  • I don't want to print file permissions and owner name. Just the path – Mohsen May 14 '13 at 18:20
  • That's not a problem. New variant is in the answer. – rush May 14 '13 at 18:23
  • Best answer, but a bit confusing for those who expect a way to execute two distinct commands as the question title says. I'll propose an edit. – Nemo Oct 5 '15 at 10:34
2
find . -name "*.haml" -print0 | xargs -0 -n 1 --no-run-if-empty haml --check 

runs haml on each file found by find

If haml can take multiple files in one invocation, you can leave out the -n 1

  • Thanks! It is now iterating through files. I want to print the file name (argument that we are passing to haml --check) before executing haml --check too. – Mohsen May 14 '13 at 18:17
  • if you add -v to xargs as well, then it will print the command (haml --check filename.haml before execution. Is that ok or do you really need to have only the filename? – Anthon May 14 '13 at 18:21
1

Here is a portable and efficient way to execute multiple commands with find without using the GNU specific "-print0" and "xargs -0" tricks :

find . -name "*.haml" -exec sh -c 'for i; do echo $i;ls -l $i;haml --check $i; done' sh {} +

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.