Communities

Writing
Writing
Codidact Meta
Codidact Meta
The Great Outdoors
The Great Outdoors
Photography & Video
Photography & Video
Scientific Speculation
Scientific Speculation
Cooking
Cooking
Electrical Engineering
Electrical Engineering
Judaism
Judaism
Languages & Linguistics
Languages & Linguistics
Software Development
Software Development
Mathematics
Mathematics
Christianity
Christianity
Code Golf
Code Golf
Music
Music
Physics
Physics
Linux Systems
Linux Systems
Power Users
Power Users
Tabletop RPGs
Tabletop RPGs
Community Proposals
Community Proposals
tag:snake search within a tag
answers:0 unanswered questions
user:xxxx search by author id
score:0.5 posts with 0.5+ score
"snake oil" exact phrase
votes:4 posts with 4+ votes
created:<1w created < 1 week ago
post_type:xxxx type of post
Search help
Notifications
Mark all as read See all your notifications »
Q&A

Post History

66%
+2 −0
Q&A How do I find files?

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 ...

posted 1y ago by matthewsnyder‭

Answer
#1: Initial revision by user avatar matthewsnyder‭ · 2023-09-08T20:37:57Z (about 1 year ago)
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):

```sh
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`](https://github.com/sharkdp/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`.