1

I am using this command which is working fine:

sudo find / -user web132 -ls

But when I am trying like

sudo find / -user web132 -ls -lh

I am getting a wrong syntax error. How can I get the file sizes belonging to a user in MB?

  • you get syntax error probably because -ls is a flag to find command, not separate instance of ls, so it cannot take -lh flags. – Sergiy Kolodyazhnyy Jun 21 '15 at 21:50
4

You need to use exec predicate of find:

sudo find / -user web132 -exec ls -lh {} +

This will execute ls -lh under the -exec predicate of find and will give you output in the human readable format. If you want to output always in megabytes you need some additional operations, the -h option in your ls -lh suggesting that you only want the output in human readable format.

  • As an aside, you can actually have find run any command on the files it finds. – trognanders Jun 21 '15 at 18:59
  • @heemayl find has -ls flag, which displays files in the ls format – Sergiy Kolodyazhnyy Jun 21 '15 at 21:48
  • @Serg The answer to your statement is in the question itself :) – heemayl Jun 21 '15 at 21:49
  • @heemayl I've comented on that already, check it – Sergiy Kolodyazhnyy Jun 21 '15 at 21:50
  • @Serg the question is to show the output os ls in a human readable format..i don't know what you are getting at.. – heemayl Jun 21 '15 at 21:51
2
find / -size +100k -size -500M -user web132

This means to find a file larger than 100kb and smaller than 500mb. The -ls option is about how the output is formatted, and has little to do with the ls command.

  • Uppercase K in +100K didn't work for me,but lowercase works. – Sergiy Kolodyazhnyy Jun 21 '15 at 21:56
1

Here's find + awk + numfmt version. Because -ls is a flag to find command, not an instance of ls, you cannot use -lh flags as well. However, if you insist on using the -ls flag with find, we can also try format the file size into human readable form. File size in output of find with -ls flag is field #7, thus using awk's two-way io, we just convert that field to a human readable form, and substitute it back.

find / -user web132 -ls | 
 awk '{ command="numfmt --to=iec "; command $7 | getline result; $7=result;print; close(command) }'

Here's example with my home folder:

$ find . -maxdepth 1 -user xieerqi -ls | awk '{ command="numfmt --to=iec "; command $7 |& getline result; $7=result;print; close(command) }'   
3018363 4 drwxr-xr-x 43 xieerqi xieerqi 4.0K Jun 22 00:33 .
3409153 4 drwx------ 3 xieerqi xieerqi 4.0K Jun 22 00:33 ./.gconf
3018459 4 -rw-rw-r-- 1 xieerqi xieerqi 756 Jun 11 00:08 ./record
3018368 16 -rw------- 1 xieerqi xieerqi 13K Jun 21 23:14 ./.bash_history
3018364 12 -rw-r--r-- 1 xieerqi xieerqi 8.8K May 28 03:59 ./examples.desktop
3018535 4 -rw-rw-r-- 1 xieerqi xieerqi 64 Jun 21 16:04 ./testfile.txt
3018473 4 drwxrwxr-x 2 xieerqi xieerqi 64 Jun 16 23:28 ./netmanager

Note: from the discussion in the comments bellow, it seems that mawk has issues, but gawk(GNU awk) works perfectly fine. I've figured out an approach for mawk, which merely makes use of replacing $7 not as an argument, but as part of the command, that later gets expanded. Here's what I mean:

find . -maxdepth 1 -user xieerqi -ls | mawk '{command="numfmt --to=iec "$7; command | getline var; $7=var;print }'

  • You probably should close the numfmt command: gnu.org/software/gawk/manual/html_node/… – muru Jun 21 '15 at 22:24
  • @muru addressed that already. Thank you for the info, didn't know that – Sergiy Kolodyazhnyy Jun 21 '15 at 22:31
  • i am getting unexpected character '&' – heemayl Jun 21 '15 at 22:32
  • @heemayl how about removing & from |& ? does that have an effect ? Do you use gnu awk or something else, like mawk ? run awk --version command. – Sergiy Kolodyazhnyy Jun 21 '15 at 22:35
  • i am using mawk – heemayl Jun 21 '15 at 22:36
1

You can print the sizes of files in bytes using the find alone with %s format specifier:

find / -user web132 -printf "%p %s\n"

Since you want the size in megabytes, let's divide the last field by 1024*1024 using awk:

find / -user web132 -printf "%p %s\n" |  awk '{printf "%sB %fMB\n", $0, $NF/1048576.0}'

This way you'll always have the output in megabytes, unlike with ls -lh option that will use smaller units for smaller files.

There is also %k specifier that will use 1K block, so alternatively you can use:

find / -user web132 -printf "%p %k\n" |  awk '{printf "%sB %fMB\n", $0, $NF/1024.0 }'
0

TL;DR find … -ls … is not the same as ls. If you need some parameters for ls you need the command ls. And -lh isn't a parameter for find, therefore the error.


find … -exec … is a good option. You could use also:

sudo find / -user web132 -print0 | xargs -0 ls -lh {}

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.