33

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 ?

meetar
  • 107
  • 5
Lasall
  • 3,673
  • 4
  • 29
  • 33
  • You need to specify whether you want to ls the files inside each find result, or you want to ls the find results directly. If it is the latter, then u/Dennis's answer is the correct answer. – rmutalik Dec 10 '19 at 19:29

4 Answers4

38

You can use -exec with find command.

find . -name '*foo*' -exec ls -lah {} \;
Prince John Wesley
  • 4,831
  • 1
  • 19
  • 12
  • 1
    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 07:22
  • 8
    A slightly better way: `find . -name '*foo*' -exec ls -lah {} +` – jlliagre May 22 '11 at 03:35
  • 2
    I tried to find out why adding a `+` was better. `man less` and searching for `-exec command {} +` found it. Adding `+` to the end of the command multiple arguments to be appended before executing the command. So much fewer commands are run. – PatS Mar 10 '21 at 21:22
  • According to the man page: "There are unavoidable security problems surrounding use of the -exec action; you should use the -execdir option instead." The option `-execdir` is similar to `-exec` but runs the command from the subdirectory containing the matched file. – Daniel Apr 14 '22 at 10:13
26
find . -name *foo* | xargs -r ls -lah

That should work.

Eric Carvalho
  • 53,609
  • 102
  • 137
  • 162
freethinker
  • 1,160
  • 9
  • 9
  • 1
    Thank you for that solution. But how can I deal with whitespaces? – Lasall May 21 '11 at 06:59
  • 3
    @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
  • So simply brilliant! Thanks! Didn't know the "-r" before. Life saver!! – GTodorov Jun 11 '20 at 03:47
9

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
Peter Kay
  • 91
  • 1
  • 3
9

Try this:

find  . -name *.bak -ls
amc
  • 7,022
  • 7
  • 39
  • 51
Dennis
  • 101
  • 1
  • 1