9

I'm using CentOS 6.8

I'd like to know if I can I find all files with the .log extension and order by file size and display the file size next to the filename?

I'm currently using this command to find all files with the .log extension:

find .  -name \*.log
1
  • If any of the existing answers solves your problem, please consider accepting it via the checkmark. Thank you!
    – Jeff Schaller
    Apr 23 '17 at 13:04
9

This seems to work for me:

find .  -name \*.log -ls | sort -r -n -k7
2
2

Here are two options; one bash-centric and one just for fun.


( shopt -s globstar dotglob; stat --format "%s %n" -- **/*.log | sort -rn )

This:

  • runs in a sub-shell, so that the shopt statements don't affect the current/running shell.
  • sets the globstar and dotglob shell options; globstar enables the use of the ** syntax to match files in subdirectories; dotglob enables the shell globbing to match directories that start with a .
  • stat is how we gather the file sizes with their names; it's installed by default on CentOS systems -- it is not POSIX-specified.
  • the real work here is done by the globstar **/*.log, which gathers matching filenames (*.log) in the current directory and any subdirectories.
  • we then reverse-numerically sort the file sizes & names to get the largest files first (use -n without the r to sort them in ascending-size order).

Another bash-centric option, but one that also exercises an ls flag to sort its arguments by size:

shopt -s globstar dotglob
ls -lS **/*.log

# or, in reverse:

ls -lrS **/*.log

To exercise your system and your patience, you could search for files of a specific size in a certain order:

for((i=9223372036854775807;i>=0;i--)); do find . -name \*.log -size ${i}c -exec stat --format "%s %n" {} + ; done

This runs 9,223,372,036,854,775,807 (over 9 quintillion) find commands, looking for *.log files of every possible size, again calling stat to display just the file sizes and names. In case there are multiple files of the same size, I included find's {} + syntax to pass as many filenames to stat as will fit in the environment. You may have to adjust the for loop range based on your shell's integer size, or if you know how large the largest log file might be. This "option" has the "benefit" of being able to be POSIX-compliant if you replace the stat call with a simple ls.

1
  • Thank you, Don! I developed the answer on a BSD system with a different stat command, and I didn't look closely enough at the online manpage for GNU stat. Always test!
    – Jeff Schaller
    Feb 24 '17 at 2:00
0

Kindly run below command for listing all folders named log along with size.

du -csh $(find / -type d -iname log)
-1

Try this command:

 find .  -name \*.log |ls -lSh
1
  • Sorry, that didn't work. Feb 20 '17 at 20:01
-1

Using this you’ll search the entire system. I recommend redirecting the standard error stream to the "null device” to avoid output that is irrelevant :

find / -name *.log 2> /dev/null | sort -r -n -k7
1
  • Welcome to the site, and thank you for your contribution. However, please note that your post seems to mostly reiterate what this other answer by @bigmike7801 already stated. If you want to add more details or context, please consider submitting an edit suggestion to that answer instead.
    – AdminBee
    Nov 16 '21 at 10:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.