Operating System - HP-UX
1754413 Members
2872 Online
108813 Solutions
New Discussion юеВ

files greater than size in a filesystem sorted by size

 
SOLVED
Go to solution
Billa-User
Regular Advisor

files greater than size in a filesystem sorted by size

hello,

what is the best and fastest solution to list file(s) greater than size (in MB) in a filesystem sorted by size.

i saw a solution in thread "How to find the greatest size file in certain mount point?".

is it a good solution solution to use "find ... -exec ll " or should it better to use perl ?

here my way :

# start of script:
mul()
{
# multiply of digit > 32 bit
bc<scale=0
$1*$2
EOF

}

# File_size in MB
CHECK_FILE_SIZE=20
typeset -E check_file_size_bytes
check_file_size_bytes=`mul ${CHECK_FILE_SIZE} 1048576`

find /tmp -xdev -size +${check_file_size_bytes}c -print

# now i have to use "find | ls | sort" ?
find /tmp -xdev -size +${check_file_size_bytes}c
-exec ls -ls {} \; | sort -n -r

regards
10 REPLIES 10
Jose Mosquera
Honored Contributor

Re: files greater than size in a filesystem sorted by size

Hi,

Using your logic, I would improve the syntax as follows:

CHECK_FILE_SIZE=20
typeset -E CHECK_FILE_SIZE_BYTES
CHECK_FILE_SIZE_BYTES=`echo "scale=0;${CHECK_FILE_SIZE}*1048576"|bc`
find /tmp -xdev -size +${CHECK_FILE_SIZE_BYTES}c -exec -ls -ld \;|sort -nr

Have not you thought of changing the value set in the script CHECK_FILE_SIZE by a variable dynamic value "$n" parsed from prompt? The same suggestion apply for the path value (/tmp) used by find command. i.e:
#./your_scriptname /tmp 20

Rgds
Jose Mosquera
Honored Contributor

Re: files greater than size in a filesystem sorted by size

Ooops, my previous post have a syntax error into "find" command.

Please find attached a new version. This uses parsing values from prompt ($1=MountPoint and $2=FileSizeMb). Then try:
#./script_name /tmp 20

Rgds.
Billa-User
Regular Advisor

Re: files greater than size in a filesystem sorted by size

hello,

also a little bug :-)
old:
find $1 -xdev -size +${FILE_SIZE_BYTES}c -exec ls -ld \;|sort -nr

new:
find $1 -xdev -size +${FILE_SIZE_BYTES}c -exec ls -ld {} \;|sort -nr

Thx
Jose Mosquera
Honored Contributor
Solution

Re: files greater than size in a filesystem sorted by size

Yeap! Find attached a new version that includes $2 field type validation also.

And remember... ...please do not forget assign points!

Best regards.
Billa-User
Regular Advisor

Re: files greater than size in a filesystem sorted by size

hello,

i improve sort to "sort -k5nr"

regards
Jose Mosquera
Honored Contributor

Re: files greater than size in a filesystem sorted by size

Cool! that really is the key, sort by size. I apologize, it's Friday and I'm thinking about the after-hour! :)

Rgds.
James R. Ferguson
Acclaimed Contributor

Re: files greater than size in a filesystem sorted by size

Hi:

You should optimize by allowing 'find' not to spawn an 'ls' for every file it sees! Instead of using the '\;' terminator to '-exec' use '+'. Do something like:

# find . -xdev -type f -size +1000c -exec ls -ld {} +|sort -nrk5,5

Notice the addition of '-type f' to confine the output to only files.

Instead of coding the value 1048576 use (1024*1024). It's much clearer. Now you could write:

# check_file_size_bytes=$(mul ${CHECK_FILE_SIZE} 1024*1024))

...and in doing so also avoid the archaic backticks and use the $(...) POSIX syntax.

Regards!

...JRF...

Billa-User
Regular Advisor

Re: files greater than size in a filesystem sorted by size

thx for the great information's !!!

# find . -xdev -type f -size +1000c -exec ls -ld {} +|sort -nrk5,5

i hear it first time to use "+"

# check_file_size_bytes=$(mul ${CHECK_FILE_SIZE} 1024*1024))

i tested it and the right syntax is
check_file_size_bytes=$(mul ${CHECK_FILE_SIZE} 1024*1024 )
also it isn't possible
check_file_size_bytes=$(mul ${CHECK_FILE_SIZE} (1024*1024) )

regards
James R. Ferguson
Acclaimed Contributor

Re: files greater than size in a filesystem sorted by size

Hi (again):

> i hear it first time to use "+"

Yes, this acts (and may in fact be better) than using 'xargs' to collect multiple arguments and present them as a bundle to a process.

# find . -type f -exec ls -l {} \;

# find . -type f | xargs ls -l

# find . -type f -exec ls -l {} +

One caveat: If you were passing your output to a script that *expected* only one argument you would need to use the '\;' form so that only one argument would be received for every script invocation.

Regards!

...JRF...