Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

List files over a specific size in current directory and all subdirectories

How can I display all files greater than 10k bytes in my current directory and it's subdirectories.

Tried ls -size +10k but that didn't work.

like image 248
eveo Avatar asked Nov 08 '12 05:11

eveo


People also ask

How do I list files in a directory and its subdirectories?

The ls command is used to list files or directories in Linux and other Unix-based operating systems. Just like you navigate in your File explorer or Finder with a GUI, the ls command allows you to list all files or directories in the current directory by default, and further interact with them via the command line.

How do you list files and folders with respective size?

To list all files and sort them by size, use the -S option. By default, it displays output in descending order (biggest to smallest in size). You can output the file sizes in human-readable format by adding the -h option as shown. And to sort in reverse order, add the -r flag as follows.

Which command is used to list all files in current directory as well as in subdirectories in Unix?

Use the ls command to display the contents of a directory. The ls command writes to standard output the contents of each specified Directory or the name of each specified File, along with any other information you ask for with the flags.

How do I list files in all subdirectories?

By default, ls lists just one directory. If you name one or more directories on the command line, ls will list each one. The -R (uppercase R) option lists all subdirectories, recursively.


2 Answers

find . -size +10k -exec ls -lh {} \+

the first part of this is identical to @sputnicks answer, and sucesffully finds all files in the directory over 10k (don't confuse k with K), my addition, the second part then executes ls -lh or ls that lists(-l) the files by human readable size(-h). negate the h if you prefer. of course the {} is the file itself, and the \+ is simply an alternative to \;

which in practice \; would repeat or:

ls -l found.file; ls -l found.file.2; ls -l found.file.3

where \+ display it as one statement or:

ls -l found.file found.file.2 found.file.3

more on \; vs + with find

Additionaly, you may want the listing ordered by size. Which is relatively easy to accomplish. I would at the -s option to ls, so ls -ls and then pipe it to sort -n to sort numerically

which would become:

find . -size +10k -exec ls -ls {} \+ | sort -n

or in reverse order add an -r :

find . -size +10k -exec ls -ls {} \+ | sort -nr

finally, your title says find biggest file in directory. You can do that by then piping the code to tail

find . -size +10k -exec ls -ls {} \+ | sort -n | tail -1 would find you the largest file in the directory and its sub directories.

note you could also sort files by size by using -S, and negate the need for sort. but to find the largest file you would need to use head so

find . -size +10k -exec ls -lS {} \+ | head -1

the benefit of doing it with -S and not sort is one, you don't have to type sort -n and two you can also use -h the human readable size option. which is one of my favorite to use, but is not available with older versisions of ls, for example we have an old centOs 4 server at work that doesn't have -h

like image 104
matchew Avatar answered Oct 07 '22 16:10

matchew


Try doing this:

find . -size +10k -ls 

And if you want to use the binary ls :

find . -size +10k -exec ls -l {} \; 
like image 21
Gilles Quenot Avatar answered Oct 07 '22 16:10

Gilles Quenot