How to find a file in the filesystem from the command line?
up vote
10
down vote
favorite
I'd like to find where a file (with a partially-known filename) is in the file system. I'd like to know how to do this from the command line, rather than using a GUI utility.
In Windows, I'd run the following:
cd /d C:
dir *filename* /s
What's the Linux equivalent?
ubuntu find ls locate
add a comment |
up vote
10
down vote
favorite
I'd like to find where a file (with a partially-known filename) is in the file system. I'd like to know how to do this from the command line, rather than using a GUI utility.
In Windows, I'd run the following:
cd /d C:
dir *filename* /s
What's the Linux equivalent?
ubuntu find ls locate
Well, there's quite obviously more than one way to skin this cat. I'm still a little curious to know if there's a way to do it withls
?
– Iszi
Nov 16 '11 at 6:32
add a comment |
up vote
10
down vote
favorite
up vote
10
down vote
favorite
I'd like to find where a file (with a partially-known filename) is in the file system. I'd like to know how to do this from the command line, rather than using a GUI utility.
In Windows, I'd run the following:
cd /d C:
dir *filename* /s
What's the Linux equivalent?
ubuntu find ls locate
I'd like to find where a file (with a partially-known filename) is in the file system. I'd like to know how to do this from the command line, rather than using a GUI utility.
In Windows, I'd run the following:
cd /d C:
dir *filename* /s
What's the Linux equivalent?
ubuntu find ls locate
ubuntu find ls locate
edited Nov 24 at 20:11
Rui F Ribeiro
38.3k1476127
38.3k1476127
asked Nov 16 '11 at 2:15
Iszi
3152314
3152314
Well, there's quite obviously more than one way to skin this cat. I'm still a little curious to know if there's a way to do it withls
?
– Iszi
Nov 16 '11 at 6:32
add a comment |
Well, there's quite obviously more than one way to skin this cat. I'm still a little curious to know if there's a way to do it withls
?
– Iszi
Nov 16 '11 at 6:32
Well, there's quite obviously more than one way to skin this cat. I'm still a little curious to know if there's a way to do it with
ls
?– Iszi
Nov 16 '11 at 6:32
Well, there's quite obviously more than one way to skin this cat. I'm still a little curious to know if there's a way to do it with
ls
?– Iszi
Nov 16 '11 at 6:32
add a comment |
4 Answers
4
active
oldest
votes
up vote
12
down vote
accepted
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.
Wish I could up-vote this again. Just had to look this up for another project tonight. Thanks again, @Gilles!
– Iszi
Jan 12 '12 at 6:02
add a comment |
up vote
6
down vote
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.
add a comment |
up vote
5
down vote
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.
add a comment |
up vote
2
down vote
find [path] -name [filename]
For example, if I want to search the /home directory for a filename containing foo, I would use the command:
find /home -name *foo*
use the command man find
for more info on the find command and arguments,
3
You need to quote or escape the*
s to prevent the shell from interpreting them.
– Shawn J. Goff
Nov 16 '11 at 2:43
Is that distro/shell specific? I ran it in Fedora 16 without escapes and it worked fine.
– Joshc1107
Nov 16 '11 at 2:47
1
There are options in some shells that pass the stars along to the program if they don't happen to match anything. Try creating a file in your current directory that is matched by that pattern, and try the command again.
– Shawn J. Goff
Nov 16 '11 at 2:51
add a comment |
4 Answers
4
active
oldest
votes
4 Answers
4
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
12
down vote
accepted
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.
Wish I could up-vote this again. Just had to look this up for another project tonight. Thanks again, @Gilles!
– Iszi
Jan 12 '12 at 6:02
add a comment |
up vote
12
down vote
accepted
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.
Wish I could up-vote this again. Just had to look this up for another project tonight. Thanks again, @Gilles!
– Iszi
Jan 12 '12 at 6:02
add a comment |
up vote
12
down vote
accepted
up vote
12
down vote
accepted
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.
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.
answered Nov 16 '11 at 22:11
Gilles
523k12610431576
523k12610431576
Wish I could up-vote this again. Just had to look this up for another project tonight. Thanks again, @Gilles!
– Iszi
Jan 12 '12 at 6:02
add a comment |
Wish I could up-vote this again. Just had to look this up for another project tonight. Thanks again, @Gilles!
– Iszi
Jan 12 '12 at 6:02
Wish I could up-vote this again. Just had to look this up for another project tonight. Thanks again, @Gilles!
– Iszi
Jan 12 '12 at 6:02
Wish I could up-vote this again. Just had to look this up for another project tonight. Thanks again, @Gilles!
– Iszi
Jan 12 '12 at 6:02
add a comment |
up vote
6
down vote
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.
add a comment |
up vote
6
down vote
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.
add a comment |
up vote
6
down vote
up vote
6
down vote
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.
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.
answered Nov 16 '11 at 2:36
Tim Kennedy
14.1k22950
14.1k22950
add a comment |
add a comment |
up vote
5
down vote
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.
add a comment |
up vote
5
down vote
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.
add a comment |
up vote
5
down vote
up vote
5
down vote
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.
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.
edited Nov 16 '11 at 5:13
answered Nov 16 '11 at 4:55
Peter.O
18.7k1791143
18.7k1791143
add a comment |
add a comment |
up vote
2
down vote
find [path] -name [filename]
For example, if I want to search the /home directory for a filename containing foo, I would use the command:
find /home -name *foo*
use the command man find
for more info on the find command and arguments,
3
You need to quote or escape the*
s to prevent the shell from interpreting them.
– Shawn J. Goff
Nov 16 '11 at 2:43
Is that distro/shell specific? I ran it in Fedora 16 without escapes and it worked fine.
– Joshc1107
Nov 16 '11 at 2:47
1
There are options in some shells that pass the stars along to the program if they don't happen to match anything. Try creating a file in your current directory that is matched by that pattern, and try the command again.
– Shawn J. Goff
Nov 16 '11 at 2:51
add a comment |
up vote
2
down vote
find [path] -name [filename]
For example, if I want to search the /home directory for a filename containing foo, I would use the command:
find /home -name *foo*
use the command man find
for more info on the find command and arguments,
3
You need to quote or escape the*
s to prevent the shell from interpreting them.
– Shawn J. Goff
Nov 16 '11 at 2:43
Is that distro/shell specific? I ran it in Fedora 16 without escapes and it worked fine.
– Joshc1107
Nov 16 '11 at 2:47
1
There are options in some shells that pass the stars along to the program if they don't happen to match anything. Try creating a file in your current directory that is matched by that pattern, and try the command again.
– Shawn J. Goff
Nov 16 '11 at 2:51
add a comment |
up vote
2
down vote
up vote
2
down vote
find [path] -name [filename]
For example, if I want to search the /home directory for a filename containing foo, I would use the command:
find /home -name *foo*
use the command man find
for more info on the find command and arguments,
find [path] -name [filename]
For example, if I want to search the /home directory for a filename containing foo, I would use the command:
find /home -name *foo*
use the command man find
for more info on the find command and arguments,
answered Nov 16 '11 at 2:36
Joshc1107
5961613
5961613
3
You need to quote or escape the*
s to prevent the shell from interpreting them.
– Shawn J. Goff
Nov 16 '11 at 2:43
Is that distro/shell specific? I ran it in Fedora 16 without escapes and it worked fine.
– Joshc1107
Nov 16 '11 at 2:47
1
There are options in some shells that pass the stars along to the program if they don't happen to match anything. Try creating a file in your current directory that is matched by that pattern, and try the command again.
– Shawn J. Goff
Nov 16 '11 at 2:51
add a comment |
3
You need to quote or escape the*
s to prevent the shell from interpreting them.
– Shawn J. Goff
Nov 16 '11 at 2:43
Is that distro/shell specific? I ran it in Fedora 16 without escapes and it worked fine.
– Joshc1107
Nov 16 '11 at 2:47
1
There are options in some shells that pass the stars along to the program if they don't happen to match anything. Try creating a file in your current directory that is matched by that pattern, and try the command again.
– Shawn J. Goff
Nov 16 '11 at 2:51
3
3
You need to quote or escape the
*
s to prevent the shell from interpreting them.– Shawn J. Goff
Nov 16 '11 at 2:43
You need to quote or escape the
*
s to prevent the shell from interpreting them.– Shawn J. Goff
Nov 16 '11 at 2:43
Is that distro/shell specific? I ran it in Fedora 16 without escapes and it worked fine.
– Joshc1107
Nov 16 '11 at 2:47
Is that distro/shell specific? I ran it in Fedora 16 without escapes and it worked fine.
– Joshc1107
Nov 16 '11 at 2:47
1
1
There are options in some shells that pass the stars along to the program if they don't happen to match anything. Try creating a file in your current directory that is matched by that pattern, and try the command again.
– Shawn J. Goff
Nov 16 '11 at 2:51
There are options in some shells that pass the stars along to the program if they don't happen to match anything. Try creating a file in your current directory that is matched by that pattern, and try the command again.
– Shawn J. Goff
Nov 16 '11 at 2:51
add a comment |
Thanks for contributing an answer to Unix & Linux Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f24683%2fhow-to-find-a-file-in-the-filesystem-from-the-command-line%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Well, there's quite obviously more than one way to skin this cat. I'm still a little curious to know if there's a way to do it with
ls
?– Iszi
Nov 16 '11 at 6:32