How to find a file in the filesystem from the command line?
locate filename
find -name '*filename*'
echo **/*filename*
ls -ld **/*filename*
(Read on for the main terms and conditions. Read the manual for the fine print.)
Listing the contents of a directory is kind of a secondary feature of ls
. The main job of ls
, the one that takes up most of its complexity, is fine-tuning its display. (Look at the manual and compare the number of options related to choosing what files to display vs. the number of options that control what information to display about each file and how the display is formatted. This is true both of GNU ls which you'll find on Linux, and of other systems with fewer options, since the early days.)
The default mode of ls
is that when you pass it a directory, it lists the files in that directory. If you pass it any other type of file (regular file, symbolic link, etc.), it lists just that file. (This applies to each argument separately.) The option -d
tells ls
never to descend into a directory.
ls
does have an option -R
that tells it to list directories recursively. But it's of limited applicability, and doesn't allow much filtering on the output.
The very first tool to perform pattern matching is the shell itself. You don't need any other command: just type your wildcards and you're set. This is known as globbing.
echo *filename*
Traditionally, wildcards were limited to the current directory (or the indicated directory: echo /some/where/*filename*
). A *
matches any file name, or any portion of file name, but *.txt
will not match foo/bar.txt
. Modern shells have added the pattern **/
which means “in this directory, or in any directory below it (recursively)”. With bash, for historical compatibility reasons, this feature needs to be explicitly enabled with shopt -s globstar
(you can put this line in your ~/.bashrc
).
echo **/*filename*
The echo
command just echoes the list of file names generated by the shell back at you. As an exception, if there is no matching file name at all, the wildcard pattern is left unchanged in bash (unless you set shopt -s nullglob
, in which case the pattern expands to an empty list), and zsh signals an error (unless you set setopt nullglob
, or setopt no_no_match
which causes the pattern to be left unchanged).
You may still want to use ls
for its options. For example, ls
can give indications about the nature or permissions of the file (directory, executable, etc.) through colors. You may want to display the file's date, size and ownership with ls -l
. See the manual for many more options.
The traditional command to look for a file in a directory tree is find
. It comes with many options to control which files to display and what to do with them. For example, to look for files whose name matches the pattern *filename*
in the current directory and its subdirectories and print their names:
find /some/dir -name '*filename*' -print
-print
is an action (most other actions consist of executing a command on the file); if you don't put an action, -print
is implied. Also, if you don't specify any directory to traverse (/some/dir
above), the current directory is implied. The condition -name '*filename'
says to list (or act on) only the files whose name matches that pattern; there are many other filters, such as -mtime -1
to match the files modified in the last 24 hours. You can sometimes omit the quotes on -name '*filename*'
, but only if the wildcard would not match any file in the current directory (see above). All in all, the short form is
find -name '*filename*'
Another useful tool when you know (part of) the name of a file is locate
. This tool queries a database of file names. On typical systems, it's refreshed every night. The advantage of locate
over find /
is that it's a lot faster. A downside is that its information may be stale. There are several implementations of locate
which differ in their behavior on multi-user systems: the basic locate
program indexes only publicly-readable files (you may want to run the companion updatedb
to make a second database that indexes all the files in your account); there are other versions (mlocate, slocate) that index all files and have the locate
program filter the database to only return the files you can see.
locate filename
Sometimes you think that a file is provided by a package in your distribution, you know (part of) the name of the file but not the name of your package, and you'd like to install the package. Many distributions provide a tool for that. On Ubuntu, it's apt-file search filename
. For equivalent commands on other systems, check the Pacman Rosetta.
the equivalent to your DOS example would be:
cd /
find . -name \*filename\* -print
On Linux you don't generally need the -print
argument, anymore, though. If you find yourself working on other operating systems, it can be handy to know about.
If you want something "fast", but not in a mission-critical situation and you only want to know if it exists and where it is, you can use locate
. It keeps a database of all files in directories you have told it to gather information on.
In the default install (on Ubuntu), locate
sets up a daily cron
job which scans the file system and updates the database...
If you feel you need to bring the database up to date before the next cron update, it is typically faster than find
or ls
to just run sudo updatedb
and then locate
. It is definitely faster if you need to make more searches ... as its name suggests, updatedb
updates the database which locate
uses...
locate
has built-in regex, which makes it very handy... I'll use find
in a script, but I rarely use find
at the commandline. I even use locate
in (personal) scripts... eg. locate -bir "oo.*datt.*mp4$"
locate
returns the fully-qualified paths of matched files.