freebsd find

October 6, 2010

How to find files, programs, etc using freebsd

The locate command is good for finding files and directories. Enter something similar to:

locate someprogram | more

If that doesn’t work, perhaps try the whereis or find commands:

whereis someprogram | more

find / -name "someprogram*"

For variations on these commands, you can type:

man locate

man whereis

man find

(from http://www.us-webmasters.com/FreeBSD/Tips-Hints-Tricks/How-to-Find-Files-and-Directories-on-FreeBSD/)

To find the largest ten files on the disk:

find / -type f | xargs ls -s | sort -rn | awk '{size=$1/1024; printf("%dMb %s\n", size,$2);}' | head

To find the largest ten files in a directory:

for i in G M K; do du -ah | grep [0-9]$i | sort -nr -k 1; done | head -n 11

To search for a string recursively amongst a bunch of files/subdirectories install gnugrep (it replaces regular grep) and then run this (replace yoursearchstring):

find . -type f -print | xargs grep --mmap -l yoursearchstring

To replace all instances of a string in a directory with a backup of original file as .bak (subdirectories included):

perl -e "s/FIND/REPLACE/g;" -pi.bak $(find path/to/DIRECTORY -type f)

To replace all instances of a string in a directory without a backup of original file (subdirectories included):

Code:

perl -e "s/FIND/REPLACE/g;" -pi $(find path/to/DIRECTORY -type f)
(the last two were from http://forums.devshed.com/unix-help-35/unix-find-and-replace-text-within-all-files-within-a-146179.html)