Wednesday, August 15, 2007

Find command tips ...

Finding Max space used by Top 10 Files in MB:
============================================

[root@linuxbox Desktop]# find /tmp -type f | xargs ls -msa
| sort -rn | awk '{size=$1/1024;printf("%dMb %s\n", size,$2);}'
| head

10Mb /tmp/openldap-devel-2.3.34-0.fc7.i386.rpm,
6Mb /tmp/newtonslaw.wav
4Mb /tmp/openldap-clients-2.3.34-0.fc7.i386.rpm,
3Mb /tmp/ntfs-3g-1.516-1.fc7.i386.rpm,
2Mb /tmp/fuse-2.6.5-1.fc7.i386.rpm,
1Mb /tmp/orbit-root/bonobo-activation-server-ior,
0Mb /tmp/gconfd-root/lock/ior,
0Mb /tmp/orbit-root/bonobo-activation-register.lock,

Finding files which are more than 10 MB:
========================================

find /tmp -name "*" -size +10000k

Finding files modified 3 days ago but less than 5 days:
========================================================

find /tmp -type f -mtime +2 -mtime -5


Find files that were accessed exactly 3 days a go
==================================================
find /tmp -type f -atime 3


-atime +7: All files that were last accessed more than 7 days ago
-atime 7: All files that were last accessed exactly 7 days ago
-atime -7: All files that were last accessed less than7 days ago

To replace recursive in *.html files, with multiline support:
=============================================================
find ./dir/ -name *.html -exec perl -pi -e 'undef $/;
s/REPLACE-THIS/WITH-THAT/s' {} \;

Update the modification date for all files in the
current directory and below:
=================================================
find . -exec touch {} \;

Remove files in a directory that were created more
than x number of days ago (great for managing Maildir).
======================================================
find ./ -ctime +x | xargs | rm -f -r

This should delete files older than 7 days, and
print the name of each file it deletes.
=================================================
find ./ -ctime +7 -name "*.zip" -exec rm -f {} \; -ls

# If you want to be prompted before it deletes a file:
find ./ -ctime +7 -name "*.zip" -exec rm -i {} \; -ls

This will list the files with names ending in .c
and containing the word hello:
=================================================

find . -type f -name '*.c' -exec grep -l hello {} /dev/null \;

Find all the files recursively that contain foo:
================================================
find . | xargs grep foo
./a:foo
./c:foo
./d:ifoo

a,c,d are the file names that contain foo

No comments: