Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

When I type sth. like that

find . -name *foo* | ls -lah

it gives me a ls-output of 'pwd' like I had no file(s) given to 'ls'.

To simply use

ls -lah $( find . -name *foo* )

works well (when the 'find' command has results).

Is it possible to pipe to 'ls' ?

share|improve this question
add comment

2 Answers 2

up vote 5 down vote accepted

You can use -exec with find command.

find . -name '*foo*' -exec ls -lah {} \;
share|improve this answer
    
Thank you for your solution. There is no pipe but it's an elegant method. If there aren't found any results with 'find' it doesn't display anything (what is in fact good). –  Lasall May 21 '11 at 7:22
3  
A slightly better way: find . -name '*foo*' -exec ls -lah {} + –  jlliagre May 22 '11 at 3:35
add comment
find . -name *foo* | xargs -r ls -lah

That should work.

share|improve this answer
    
Thank you for that solution. But how can I deal with whitespaces? –  Lasall May 21 '11 at 6:59
1  
@Lasall the preferred way is to use find's -exec + (or -exec \;). xargs is only safe to use with the -0 option, which means that you have to tell whatever command you pipe to xargs to delimit the items with NULL-bytes (\0). With find you can do that with -print0. xargs's -0 and find's -print0 are not standard, but -exec is, so if portability is ever an issue, use find with -exec. –  geirha May 26 '11 at 23:29
    
@geirha: Thank you for your exact explanation. –  Lasall May 27 '11 at 14:06
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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