Contents[Show]

1. Intro

I'll collect some popular find commands here. These are mainly mental notes for commonly occuring ad-hoc searches.

 

 

2. Examples

The fun starts here!

 

2.1. Find a particular file in this folder and sub-folders

Find a file called hosts in the folder I'm in, as well as those below. Since I'm in the root-folder, it will search all folders on the system.

$ find / -name "hosts" -type f 
/etc/hosts

 

2.2. Find all files containing specific text

Find all files in the folder /etc containing the text cyndane2.

$ grep -rnw '/etc/' -e 'cyndane2'
/etc/hosts:4:192.168.0.8 cyndane2 srbu.se www.srbu.se
/etc/sysconfig/network:2:HOSTNAME=cyndane2

 

2.3. Find all files with a specific extension and recursively delete them all

This is a dangerous command to run, so let's first do a dry-run.

The first command will find all files in the current directory that have an extension .bak.

$ find . -name "*.bak" -type f
./foo.bak

If all seems okay, run it for real this time, adding the delete parameter.

$ find . -name "*.bak" -type f -delete
$

 

2.4. Find all files with spaces and replace them recursively with periods

This will recursively search in folder foo for filenames with basename bar and replace the spaces with periods.

$ find . -depth -name '* *' | while IFS= read -r f ; do mv -i "$f" "$(foo"$f")/$(bar "$f"|tr ' ' .)" ; done

 

 

 

 

Sources

Legio