How can I direct a pipe input to ls command?
When I type something like:
find . -name *foo* | ls -lah
it returns the same result as a plain ls
command, as though it had no input.
However:
ls -lah $( find . -name *foo* )
works well, but only when the find
command has results.
Is it possible to pipe to ls
?
Solution 1:
You can use -exec
with find
command.
find . -name '*foo*' -exec ls -lah {} \;
Solution 2:
find . -name *foo* | xargs -r ls -lah
That should work.
Solution 3:
Try this:
find . -name *.bak -ls
Solution 4:
This works with filenames with spaces or unusual characters, and ls
can sort all the files:
find . -name *foo* -print0 | xargs -0 ls -lah
-print0
means that filenames such as file foo 1
will get output from find
followed by null. The "-0" argument to xargs
tells it to expect this sort of input, so filenames with spaces get piped to the ls
command correctly.
The xargs
construction is in some ways better than find etc -exec ls {} +
because all the filenames get sent to ls
at once, so if you want to sort them all by timestamp (using ls
), something like this works:
find . -iname *pdf -print0 | xargs -0 ls -ltr
On a NetBSD system, "-printx" is also an option (this seems a useful argument to me, but whatever, we have xargs -0 and it's okay):
find . -name *foo* -printx | xargs ls -lah` # not for Ubuntu