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?
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 need to use
exec
predicate offind
:This will execute
ls -lh
under the-exec
predicate offind
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 yourls -lh
suggesting that you only want the output in human readable format.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 thels
command.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 offind
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.Here's example with my home folder:
Note: from the discussion in the comments bellow, it seems that
mawk
has issues, butgawk
(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 can print the sizes of files in bytes using the
find
alone with%s
format specifier:Since you want the size in megabytes, let's divide the last field by 1024*1024 using
awk
: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:TL;DR
find … -ls …
is not the same asls
. If you need some parameters forls
you need the commandls
. And-lh
isn't a parameter forfind
, therefore the error.find … -exec …
is a good option. You could use also: