Using Find Command in combination with other ones

root's picture

Most commands should work on all UNIX/LINUX systems. If there are difference, the OS name is put in front of the commands. Nevertheless, please run them first on a test environment.

Find 50 largest files on /fs:
aix: #find /fs -xdev -type f -ls | sort +6n | tail -n 50
linux: #find /fs -xdev -type f -ls | sort -k 7n | tail -n 50
solaris: #find /fs -xdev -type f -ls | sort -k 7n | tail -50
hp-ux: #find /fs -xdev -type f -exec ls -la {} \; | sort -k 5n | tail -50

Find core files on /fs:
#find /fs -name "core*" -xdev -type f -exec file {} \;

Find AIX core files on /fs:
#find /fs -name "*core*" -xdev -type f -exec file {} \; | grep "AIX"

Find newest files on /fs:
#find /fs -xdev -mtime -1 -type f -exec ls -l {} \;

Sort all files in /fs by time:
#find . -type f -exec ls -lrt {} \;

Find newest 50 largest files on /fs:
aix: #find /fs -xdev -mtime -1 -type f -ls | sort +6n | tail -n 50
linux: #find /fs -xdev -mtime 1 -type f -ls | sort -k 7n | tail -n 50
solaris: #find /fs -xdev -mtime 1 -type f -ls | sort -k 7n | tail -50
hp-ux: #find /fs -xdev -mtime 1 -type f -exec ls -la {} \; | sort -k 5n | tail -50

Find files larger than 100 bytes (example):
aix: #find /fs -type f -size +100c -xdev -exec ls -l {} \;
linux: #find /fs -type f -size +100b -xdev -exec ls -l {} \;

Finding files containing a string:
#find /fs -type f -print | xargs -i grep -il "searchstring" {}

Finding all files in the current directory older than 180 days:
#find /fs -mtime +180 -exec ls -l {} \;

Finding all files belonging to a specific user:
#find /fs -user searchuser -type f -exec du -k {} \;

Display the total space used by a specific user in /fs:
#find /fs -user searchuser -type f -exec du -m {} \;|awk '{ s = s+$1 } END { printf "Total used: %d\n",s }'

Display the total space used by a specific user in directory /fs but not outside the filesystem:
#find /fs -xdev -user searchuser -type f -exec du -m {} \;|awk '{ s = s+$1 } END { printf "Total used: %d\n",s }'

Find all executable files in the current directory:
#find /fs -type f \( -perm -u=x -o -perm -g=x -o -perm -o=x \)

Search by column (e.g. search for "2005" in column 10 can give you all files from 2005):
#find /fs -ls | awk '{if($10=="2005") print $0}'

Display the total space used by files from, eg. 2006:
#find /fs -ls | awk '$10 == "2006" { sum += $7 } END { printf "Total used: %d\n",sum }'
To get the value in human readable format:
#find /fs -ls | awk '$10 == "2006" { sum += $7 } END { hum[1024**3]="Gb";hum[1024**2]="Mb";hum[1024]="Kb"; for (x=1024**3; x>=1024; x/=1024){ if (sum>=x) { printf "Total used: %.2f %s\n",sum/x,hum[x];break } }}'

Display the total space used by whatever files are you searching for:

fmbp:~ florian$ find . -type f -name "*.jpg" -ls | awk '{ sum += $7 } END { hum[1024**3]="Gb";hum[1024**2]="Mb";hum[1024]="Kb"; for (x=1024**3; x>=1024; x/=1024){ if (sum>=x) { printf "Total used: %.2f %s\n",sum/x,hum[x];break } }}'
Total used: 26.10 Gb

An example of how to search and delete all file from a directory from September 9:
#for i in $(find /fs -type f -ls | awk '{if($9=="9" && $8=="Sep") print $11}'); do rm -f $i; done

To get prompt before running a desired command on each file found. To execute the command, enter Y or y. If you want to skip one file, just press enter alone:
#find $HOME/. -name *.txt -ok ls {} \;

Copy a directory's content:
#find /fs | cpio -pdumv /some/dir/;

How to list zero length files:
#find . -type f -empty -exec ls {} \;

Find links that point to nothing with help of perl:
#find / -type l -print | perl -nle '-e || print';

Find unprintable directories and move them to some directory:
#find . -inum 211028 -exec mv {} /some/dir/ \;

Find a file after the inode number:
#find /usr -xdev -inum INODE -print

How to sort and display the files by number/year, except the current one:
#find . -type f -exec ls -la {} \;| awk '{print $8}' | grep -v ":" | sort | uniq -c

Thou shalt not steal!

If you want to use this information on your own website, please remember: by doing copy/paste entirely it is always stealing and you should be ashamed of yourself! Have at least the decency to create your own text and comments and run the commands on your own servers and provide your output, not what I did!

Or at least link back to this website.

Recent content

root
root
root
root
root
root
root
root
root
root
root
root
root
root
root
root
root
root
root
root
root
root
root
root
root
root
root
root
root
root