user5552734
user5552734

Reputation:

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.

Upvotes: 4

Views: 6833

Answers (5)

Adam Katz
Adam Katz

Reputation: 16146

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. If you already know the ballpark size and that most files are much smaller, you can tell find to rule out smaller sizes with an additional option, e.g. find . -type f -size +2M -printf … which prunes out anything under 2MiB. Otherwise, you'd have to use some custom code that stores only the largest 20 items.

Upvotes: 4

e0k
e0k

Reputation: 7161

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

Upvotes: 3

Anton Drukh
Anton Drukh

Reputation: 917

find . -type f |xargs ls -lS |head -20 | awk '{print $9, $5}'

Since the output of ls is columnar, just print the proper columns and you're done.

Upvotes: 0

Marc B
Marc B

Reputation: 360732

xargs takes each line of output from the previous command and basically slaps at the end of its arguments, so your find is printing something like

123 ./somefile.txt 

which xargs turns into

ls -lS 123 ./somefile.txt

unless you actually have a file named 123 in that directory, you get your "cannot access" error:

marc@panic:~$ touch foo
marc@panic:~$ ls -lS file_that_does_not_exist foo
ls: cannot access file_that_does_not_exist: No such file or directory
-rw-rw-r-- 1 marc marc 0 Feb  3 14:26 foo

Upvotes: 0

Mukrram Rahman
Mukrram Rahman

Reputation: 426

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

du -h <<FileName>>

Or

du -h <<FilePath>>

Upvotes: 2

Related Questions