If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Data Access, Manipulation & Batch Languages > Unix Shell Scripts > Help!

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 09-29-09, 21:50
chrlzbrn chrlzbrn is offline
Registered User
 
Join Date: Sep 2009
Posts: 3
Red face 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!
Reply With Quote
  #2 (permalink)  
Old 09-30-09, 12:02
kitaman kitaman is offline
Papabi's friend
 
Join Date: Sep 2009
Location: Ontario
Posts: 626
find / -size +5000000c -exec ls -l {} >filelist
or if you want in blocks
find / -size +10000 -exec ls -l {} >filelist
Reply With Quote
  #3 (permalink)  
Old 09-30-09, 22:03
chrlzbrn chrlzbrn is offline
Registered User
 
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.
Reply With Quote
  #4 (permalink)  
Old 09-30-09, 22:04
sco08y sco08y is offline
Registered User
 
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.
Reply With Quote
  #5 (permalink)  
Old 09-30-09, 22:49
chrlzbrn chrlzbrn is offline
Registered User
 
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.
Reply With Quote
  #6 (permalink)  
Old 10-02-09, 11:09
kitaman kitaman is offline
Papabi's friend
 
Join Date: Sep 2009
Location: Ontario
Posts: 626
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
Reply With Quote
  #7 (permalink)  
Old 10-02-09, 14:08
kitaman kitaman is offline
Papabi's friend
 
Join Date: Sep 2009
Location: Ontario
Posts: 626
Quote:
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
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On