How do I find files?
How can I search for files on my system? Ideally, I would like to search by various criteria, like date, name, extension, etc.
1 answer
The following users marked this post as Works for me:
User | Comment | Date |
---|---|---|
matthewsnyder | (no comment) | Sep 8, 2023 at 20:38 |
The ancient utility find
should come installed on the majority of distributions. Technically, find
recursively locates all files and directories under a path, and prints their full paths. It is of course possible to pipe it to other tools like grep
(actually, ls
can also list recursively, which you can pipe to grep
also, but it's a pretty bad way of doing it):
find / | grep '.conf'
But this is somewhat inefficient and inelegant. find
supports an elaborate set of filters (and can even do some other things like running a program on each file found). For example:
-
-name
will filter based on the filename -
-type
can be used to filter things like directories, symbolic links, etc. -
-mtime
filters on last modification date/time
Many others can be found in man find
.
Note that find
is subject to access control, so if you search system directories you may see some errors. The errors actually come on the standard error channel, while the found files come on standard output, so if you pipe the output of find
it won't actually pipe the errors (they'll still be printed out). But the way to prevent the errors is to use sudo find
.
find
can be a bit slow if you are searching through a large number of files (100k+ depending on your hardware). There is also a common tool locate
, which also searches for files, but it maintains a database indexing them rather than directly crawling the file tree. The index must be first constructed and then maintained, but this allows locate
to return results much faster than find
. For details, look at man locate
.
I personally don't think find
is very user friendly, and I find it hard to remember its syntax. I use fd
which is essentially the same thing, but has nicer defaults (like colors). One gotcha with fd
is that by default, it will ignore some paths, like:
- "Hidden" files and folders (those that start with
.
) - Things in
.gitignore
if you are searching from a git repo - Internal git data in
.git
You can prevent this behavior in various ways, like passing the -H
flag. For the full list, see man fd
.
0 comment threads