FizzBuzz Journal

Not really a blog, just some stuff that a future me might need to remember one day.

  1. Github
  2. Stackoverflow
  3. Twitter

find tips

12 Apr 2015

As part of the ‘remedial Unix’ series lets take a look at command line tools for finding files by name.

find

find is the basic tool for file searches. The basic idea is that you give it a starting directory some kind of search criteria and it will print out all the matching files in that directory and any of its sub-directories.

Find a file when you know it’s full name

To find all files with name engine.rb:

$ find / -name engine.rb

This is going to be slow because we are scanning all files under / so…

Search within a certain directory

To find all files with name engine.rb in the ~/code directory or any of its sub-directories:

$ find ~/code -name engine.rb

Find a file when you know part of it’s name

You can use wildcards:

$ find ~/code -name '*.js'

The * character needs to be quoted or you can escape it with a backslash:

$ find ~/code -name \*.js

Find files in a certain path

Suppose you want to find CoffeeScript files in a models directory:

$ find ~/code -path \*/models/\* -name \*.coffee

Find a directory

If you want to just find directories then the type option lets you specify that:

$ find ~/code -type d -name models

Find files that changed recently

The mtime option is handy for list files that have changed in the past n days, for example to find Ruby files that changed in the past 3 days:

$ find ~/code -name \*.rb -mtime -3

Find large files

Finally if you need to find out what is gobbling up all your storage space there is the size option:

$ find ~/code -size +1MB