7

How can I find a file at the same time view the size of the file? ERROR: I tried this command, but I only got the size and details of other files.

find -name "*.conf" | ls -l
muru
  • 197,895
  • 55
  • 485
  • 740

4 Answers4

14

find has a -ls option. So your command should look like this:

find -name '*.conf' -ls
waltinator
  • 36,399
8

An option is specify your own format with -printf. For example this will output the filesize followed by a space then the filename (with relative path):

find -name "*.conf" -printf "%s %p\n"

You can end your format string with \0 instead of \n if you want to NUL-separate find's output records, in the case you expect funny filenames (e.g. those including newlines).


For the record, the attempt shown in your question doesn't work because you are trying to pipe output from find to input to ls. ls does not read any input from STDIN, so the output from find is simply discarded and then ls lists the content of the current directory, as usual.

fosslinux
  • 3,831
Digital Trauma
  • 2,445
  • 15
  • 24
6

Other answers already have pointed out

  1. how to print the filesize with find only, using -printf
  2. how to get an ls output with find only, using -ls
  3. how to properly pipe find's output to another program with xargs

However, the first and second approach don’t give the exact output of ls -l (the -ls flag equals ls -dils) and the third one needs an additional xargs call. With the help of the -exec flag it’s possible to call ls -l directly from find:

find -name "*.conf" -exec ls -l {} +

This gives the whole list of file names as arguments to ls -l and calls it as just as many times as necessary.


If you just want to match every file ending in .conf in and under the current directory (and ARG_MAX isn’t a problem!), you actually don’t need find at all. Instead you can use bash’s glob matching with the globstar and dotglob shell options as follows – it’s equally wise enough not to break on newlines in file names and matches “hidden” files like find:

$ shopt -s globstar dotglob
$ ls -l **/*.conf
dessert
  • 39,982
4

ls doesn't get its list of files from standard input, it expects them to be arguments. The xargs command is a general facility for turning input into arguments:

find -name '*.conf' | xargs ls -l

However, this will split the input at whitespace, so you can have problems if any of the names have spaces. find and xargs have options that work together to mitigate this:

find -name '*.conf' -print0 | xargs -0 ls -l

The -0 option to xargs tells it to use a null byte as the delimiter between parameters in the input, and -print0 tells find to print the names separated by a null byte instead of newline.

fosslinux
  • 3,831
Barmar
  • 241