Post Reply 
 
Thread Rating:
  • 0 Votes - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
BASH search for large files
01-11-2010, 10:06 AM
Post: #1
BASH search for large files
Here is a list of sizes you can search for using Find
-size n[ckMGTP]
True if the file's size, rounded up, in 512-byte blocks is n. If
n is followed by a c, then the primary is true if the file's size
is n bytes (characters). Similarly if n is followed by a scale
indicator then the file's size is compared to n scaled as:

k kilobytes (1024 bytes)
M megabytes (1024 kilobytes)
G gigabytes (1024 megabytes)
T terabytes (1024 gigabytes)
P petabytes (1024 terabytes)

I am running this command as administrator. This print any file name that is 1G or bigger. the + sign means greater than.
Code:
sudo find / -size +1G -print

so find searchers from the root directory which is / I use the -size parameter to set a size to search for then lastly I use the -print parameter to print out the results
Visit this user's website Find all posts by this user
Quote this message in a reply
01-11-2010, 11:59 AM
Post: #2
RE: BASH search for large files
I decided to show you what a script would look like for checking for file size. The sourcecode is attached in a zip.

Code:
#!/bin/bash

########################################################
#            Check for file sizes                      #
#           coded by Jerome Scott II                   #
########################################################

#function print the usage of the program
usage()
{
    echo 'Usages:'
    echo 'Argument1: path of files to search'
    echo 'Argument2: the size of files in either bytes, kilobytes, megabytes, or gigabytes'
    echo '         -b bytes'
    echo '           -k kilobytes'
    echo '           -m megabytes'
    echo '           -g gigabytes'
    echo '         -t terabytes'
    echo 'Example:'
    echo './filesize /etc/ -k 10'
}

PARAM_SIZE=3

#check if there is enough parameters
if [ $PARAM_SIZE -ne 3 ]
then
    usage
    exit 1
fi

#if the directory exist continue
if [ -r $1 ]
then

#check for what type of file they are searching for. The du program in bash checks for disk usage -h if for human readable and "{}" is the file found in find put quotes around it because files might have spaces \; is a terminator for -exec function

case $2 in
     -b)
         echo "Searching for files greater than $3 bytes"
        find $1 -size \+$3c -exec du -h "{}" \;
        ;;
     -k)
        echo "Searching for files greater than $3 kilobytes"
        find $1 -size \+$3k -exec du -h "{}" \;
        ;;
     -m)
         echo "Searching for files greater than $3 megabytes"
        find $1 -size \+$3M -exec du -h "{}" \;
        ;;
     -g)
         echo "Searching for files greater than $3 gigabytes"
        find $1 -size \+$3G -exec du -h "{}" \;
        ;;
     -t)
         echo "Searching for files greater than $3 terabytes"
        find $1 -size \+$3T -exec du -h "{}" \;
        ;;

    *)
        usage
        exit 1
        ;;
esac

#file directory does not exist print error message
else
    echo 'Invalid file path'
    exit 1
fi
echo 'Search has completed'


Attached File(s)
.zip  filesize.sh.zip (Size: 1.18 KB / Downloads: 5)
Visit this user's website Find all posts by this user
Quote this message in a reply
01-11-2010, 08:32 PM
Post: #3
RE: BASH search for large files
Great work. You did a good calculation there !

There's a fine line between genius and insanity. I have erased this line.
Oscar Levant
There's a fine line between an administrator and black hat hacker. I have erased this line.
Dr DEBCOL
Visit this user's website Find all posts by this user
Quote this message in a reply
Post Reply 


Forum Jump:


 Quick Theme: