So I have this command line that works.
find . -type f |xargs ls -lS |head -20
The thing is I only want the output to be the file size and the name. I tried:
find . -type f -printf '%s %p\n' |xargs ls -lS |head -20
but this gives me a bunch of 'cannot access [inode], no such file or directory' errors.
My goal is to print the biggest 20 files in the directory, not using ls.
An answer without ls involved:
find . -type f -printf '%k %p\n' |sort -n |tail -n 20
This gives each file, listed with the size (in kB), a space, then the file name, sorted numerically, and then you get the last 20 items (the 20 largest).
Your problem was in piping to ls.
If you've got a really big directory structure, sort will fall over. You'd have to use custom code that stores only the largest 20 items.
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
Use following commmand to get size of the fiule in linux.
du -h <<FileName>>
Or
du -h <<FilePath>>