Wednesday, July 11, 2007

UNIX - find cheet sheet

Find all files named "foo" in directory "/" recursively
find / -name foo

Find all files named "foo" in directory "/"
find / -maxdepth 1 -name foo

Find all files name "foo" in current directory descending recursively
find . -name foo

Find all files starting with "foo" and ending with "bar" in the current directory
find . -name foo*bar
find . -name "foo*bar"

Find all files modified in the last seven days and tar them
find / -type f -mtime -7 | xargs tar -rf weekly_incremental.tar

Find all files named core and delete them
find / -name core | xargs /bin/rm -f
find / -name core -exec '/bin/rm -f {} ;'

Locate files modified less than 10 minutes ago
find / -mmin -10

locate files that are writeable by "others"
find . -perm +o=w

Locate files owned by a user
find . -name core -user ssinghal

Find mtime and mmin options
When specifying time with find options such as -mmin (minutes) or -mtime (24 hour periods, starting from now), you can specify a number "n" to mean exactly n, "-n" to mean less than n, and "+n" to mean more than n.
find . -mtime 0 # find files modified within the past 24 hours
find . -mtime -1 # find files modified within the past 24 hours
find . -mtime 1 # find files modified between 24 and 48 hours ago
find . -mtime +1 # find files modified more than 48 hours ago
find . -mmin +5 -mmin -10 # find files modifed between 6 and 9 minutes ago

5 comments:

Nalini Kannan said...

I think locate is a more handy tool - it is faster. Just that the data is not up to the minute latest. You have to enable the indexing and thats it.. you could search right from / !

sandeep said...

@nalini
locate is a great tool, but for most cases this is an overkill. For e.g. locate is global and will give results from all over places,whereas find can restrict searches to specified directories

Nalini Kannan said...

I agree, but it is much faster. You could always grep on the output to 'find' what you were looking out for.

sandeep said...

ok , another comment in defense of find (even though I love locate)

There are systems where you do not have root priviliges and they are not running the "updatedb" command for locate. Hence you cannot use it on such systems. hence while choosing the tools, it is very important to "find" the tools which are omnipresent

Anonymous said...

Can anyone recommend the robust Network Monitoring utility for a small IT service company like mine? Does anyone use Kaseya.com or GFI.com? How do they compare to these guys I found recently: N-able N-central system network
? What is your best take in cost vs performance among those three? I need a good advice please... Thanks in advance!