How to look for files on your system very quickly

This guide will work for most Unix-like operating systems. A lot of people probably know about this or similar approaches, but many new users don't.
If you have a Unix-like OS with a Desktop Environment, you probably search for files on your system or in storage by using a file manager's search tool (inputting something into the find field in dolphin/nemo/thunar/caja etc). If you have many files, such a search could take a lot of time to complete and find what you want, dozens of minutes in some cases. For example, I have caja on my GhostBSD and it can take forever to look for something on a multi-terabyte drive.
There is a much more efficient way to look for files by creating a list of contents on your file system with find and then looking for your file in it with grep, grep usually takes mere seconds to complete even on file lists weighting hundreds megabytes (in my personal experience). The only drawback is that the initial list creating using find command takes time, but you need to do this only once or once in a while to keep the file list updated. The initial file creation process shouldn't take longer than a regular search using the file manager and you won't need to bother your drives with find requests anymore.
You can create a list which would include files' and directories' names, modification dates and sizes with the following command:
find path-to-directory-where-you'd-like-to-look-for-files -printf "%t %s %p\n" > contents
To list only files:
find path-to-directory-where-you'd-like-to-look-for-files -type f -printf "%t %s %p\n" > files
Then if you want to find files containing "BSD" in their names, simply run
grep BSD files
It should be noted that grep is a very versatile command and there are many ways to tweak the search querries to get more exact search results.
Please note that the basic grep command is case-sensitive!
find command can also be tweaked in various ways to add the file/directory information you'd expect to look for into contents lists.
You can also exclude paths when using find, for example:
find / -path "/home" -prune -o -path "/media" -prune -o -type f -printf "%t %s %p\n" > system_files
 
Code:
$ uname -rs
FreeBSD 14.4-RELEASE-p9
$ find $HOME -name .profile -printf "%t %s %p\n"
find: -printf: unknown primary or operator
FreeBSD is not Linux...
 
Regarding "How to look for files on your system very quickly", FreeBSD has a locate(1) facility to "locate" files from a created file database instantaneously, basically what you are proposing with your custom aproache.

The database is recreated on a weekly basis (see /etc/crontab), but one can change the frequency or which file systems, directories should be searched.

Besides the manual, see also:

/etc/periodic/weekly/310.locate
/etc/locate.rc
/etc/defaults/periodic.conf
 
Back
Top