In the question, you state:
My goal is to print the biggest 20 files in the directory, not using ls.
This implies that an acceptable solution should not use ls
.
Another issue is the use of xargs
. A construction like find . -type f | xargs ls
will not work with subdirectory or file names containing white space, since it will split the string from find
before giving it to ls
. You can protect the string or work around this using null terminated strings, such as find . -type f -print0 | xargs -0 ls
. In general, there are security considerations for using xargs
. Rather than verifying if your xargs
construction is safe, it's easier to avoid using it altogether (especially if you don't need it).
Try printing the 20 biggest files without ls
and without xargs
:
find . -type f -printf '%s %p\n' | sort -rn | head -20