Results 1 to 7 of 7

Thread: Help!

  1. #1
    Join Date
    Sep 2009
    Posts
    3

    Red face Unanswered: Help!

    Hello All,

    I'm trying to create a shell scrip that will search through user directory and find any files the execed 5mb and then prints information about the file (user, size and location). I do know I will need to use the "find -size" and the ">" command in the script. I have very little knowledge of shell scripting and any help would be much appreciated.

    Thank for your time!

  2. #2
    Join Date
    Sep 2009
    Location
    Ontario
    Posts
    1,057
    Provided Answers: 1
    find / -size +5000000c -exec ls -l {} >filelist
    or if you want in blocks
    find / -size +10000 -exec ls -l {} >filelist

  3. #3
    Join Date
    Sep 2009
    Posts
    3
    Thanks kitamna.

    What the difference between size in bytes and block? Also what doe the "{}" expresion do?

    Thanks for the help.

  4. #4
    Join Date
    Oct 2002
    Location
    Baghdad, Iraq
    Posts
    697
    Aside from the fact that / isn't exactly a user directory, that -exec is missing the trailing semicolon, and you have to escape those curlies on popular shells like bash. And -exec will run a subshell and the program for *every* file. That gets slooooow.

    This may be all you need:

    Code:
    find ~ -size +5M -ls
    That will list out all 5 megabyte files under your home directory. But if you want the information that the actual ls command provides (or stat), I 'd recommend piping find to xargs. This is a great technique:

    Code:
    find ~ -size +5M -print | xargs ls -l
    To explain: the -print option prints out each path found on a separate line. xargs will then grab a batch of lines and pass them to the program listed as arguments. So you're executing your program 1/1000th as many times as if you had used -exec.

    It is possible that a path can contain the newline character. While this is very rare in practice, if you're concerned, you can do this instead:

    Code:
    find ~ -size +5M -print0 | xargs -0 stat -x
    Now rather than separating each path with a newline character, a null is used, and xargs will split on nulls. Aside from that, it works exactly the same. This is the preferred way because it's bulletproof.

  5. #5
    Join Date
    Sep 2009
    Posts
    3
    Thanks sco08y,

    Thanks for taking the time to help me out, what is the perpose of the "~" symbol, and does the "+" in front of the "5M" mean 5mb and up? I hope I'm not sounding ungreatful, I just would like to learn what these special symbols do.

    Thanks for your time.

  6. #6
    Join Date
    Sep 2009
    Location
    Ontario
    Posts
    1,057
    Provided Answers: 1
    The ~ symbol is shorthand for "my home directory", and the + sign does add "greater than" to the meaning of what follows for the size option on the find command.

    "find ~ " will search your home directory only, the problem with only looking there is that all users have write access to /tmp and possibly /u or /public, and also unread mail (/var/spool/mail)

    The following script will list files for each regular (those with uid >199) user.
    It must be run by root to be effective.

    !#/bin/sh
    #$1 is size of file to search for input should look like 5000000c or 5M,
    # use "man find" to determine the possible values for -size
    IFS=":"
    while read user x uid filler
    do
    if [ $uid -gt 199 ]
    then
    echo Files owned by $user greater than $1
    find / -size +$1 -user $user -exec ls -l {} \;
    echo "\n"
    fi
    done </etc/passwd

  7. #7
    Join Date
    Sep 2009
    Location
    Ontario
    Posts
    1,057
    Provided Answers: 1
    And -exec will run a subshell and the program for *every* file. That gets slooooow.
    I wrote the following script:

    echo using exec
    timex find / -size +100000000c -exec ls -l {} \; >t1.file1
    echo using xargs
    timex find / -size +100000000c -print |xargs ls -l >t1.file2

    and ran it on an idle system as root using sh

    # ./t1
    using exec

    real 2:04.41
    user 0.59
    sys 8.05

    using xargs

    real 2:05.82
    user 0.64
    sys 7.97

    I ran this on a 2.5ghz P4 with a single 80gb SATA disk running SCO 6.0.0

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •