Not really a blog, just some stuff that a future me might need to remember one day.
As part of the ‘remedial Unix’ series lets take a look at command line tools for finding files by name.
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.
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…
To find all files with name engine.rb
in the ~/code
directory or any
of its sub-directories:
$ find ~/code -name engine.rb
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
Suppose you want to find CoffeeScript files in a models directory:
$ find ~/code -path \*/models/\* -name \*.coffee
If you want to just find directories then the type
option lets you
specify that:
$ find ~/code -type d -name models
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
Finally if you need to find out what is gobbling up all your storage
space there is the size
option:
$ find ~/code -size +1MB