Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

203
Views
How can I print out only the name and file size using the find command?

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.

over 4 years ago · Santiago Trujillo
3 answers
Answer question

0

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.

over 4 years ago · Santiago Trujillo Report

0

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
over 4 years ago · Santiago Trujillo Report

0

Use following commmand to get size of the fiule in linux.

du -h <<FileName>>

Or

du -h <<FilePath>>
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!