Monday, June 1, 2009

UNIX Find stuff

Here are some UNIX find command samples I've gathered and used for some time. I've noticed that most "new" UNIX users/admins tend to search for how to use the find UNIX command. Hope this helps.


Look for programs with no executable on disk:

file /proc/[0-9]*/exe | grep '(deleted)'

Files with SUID bit set:

find / -user root -perm -4000 -ls

Files with SUID & SGID bit set:

find / -user root -perm -6000 -ls

Find files older than 5 days and remove them:

find . -mtime +5 -type f -exec rm \{\} \;

Files with suspect UID or GID values and save to a file:

find / ! '(' -fstype proc -o -fstype nfs ')' '(' -nouser -o -nogroup ')' -ls 2>/dev/null >>/some/file/to/save

remove some files:

find . -type f \( -name \*.200??????? -o -name \*200????? \) -exec rm {} \;

Directories using a large amount of disk:

find . -type d -exec du -sh \{\} \; | grep [0-9]G

change do transport to 0 for all files in a dir:

for f in *; do sed 's/DO_TRANSPORT=1/DO_TRANSPORT=0/'< $f >bobit; mv bobit $f; done

tar.gz a file to a remote system:

tar -czf - . | ssh user@host "cat >/export/home/user/baldrick/opt-partition.tar.gz"

find directories that are using a lot of space:

find . -type d -exec du -sh \{\} \; | grep [0-9]G

fix permissions on tx releases:

grep ^cp releaseOP2187.0.sh | grep -v { | awk '{print $4}' | xargs -i chown nobody:nogroup \{\}
grep ^cp releaseOP2187.0.sh | grep -v { | awk '{print $4}' | xargs -i chmod o-r \{\}

add a -p option to the cp command on some lines in a file:

cat releaseOP2112.0.sh.orig | perl -ne '(/^cp.(\".*\})$/) ? (print "cp -p $1\n") : (print $_)' >releaseOP2112.0.sh

fast find - use as >> ff ch09 <<>

alias ff "find . -name '*\!{*}' -ls"

find different types of files and dirs and do something different with each type

find . \( -type d -a -exec chmod 771 {} \; \) -o \
\( -name "*.BAK" -a -exec chmod 600 {} \; \) -o
\ \( -name "*.sh" -a -exec chmod 755 {} \; \) -o
\ \( -name "*.txt" -a -exec chmod 644 {} \; \)

duplicate an empty directory tree from an existing tree

find . -type d -print | sed 's@^@/new/dir/tree/@' | xargs mkdir

alias to find files in the current dir

alias find. 'find . \( -type d ! -name . -prune \) -o \( \!* -print \)'
alias find.ls 'find . \( -type d ! -name . -prune \) -o \( \!* -ls \)'

Readers who read this page, also read:




Bookmark and Share My Zimbio http://www.wikio.com

0 comments: