DEV Community

Cover image for 4 Quick Commands to Find Things in Linux
Zachary Lee
Zachary Lee

Posted on • Originally published at webdeveloper.beehiiv.com on

4 Quick Commands to Find Things in Linux

1. The which command

The which command and the whereis command below both search for executable files, which are usually binary files.

Commands can be divided into built-in commands and external commands. Built-in commands are part of the shell feature, and their executable code is stored in /bin/bash. External commands have their own corresponding executable program files. We can use type to differentiate:

The which command looks for commands from the $PATH environment variable:

→ echo $PATH | tr : '\n'
/usr/local/bin
/usr/bin
/usr/local/sbin
/usr/sbin
Enter fullscreen mode Exit fullscreen mode

Let’s try it out:

2. The whereis command

whereis can search not only executable files, but also related documentation, configuration files, source files, etc. So the scope of its search is not only $PATH, but also the directory where the man page is located, and the directory where src is located. We can use whereis -l to view:

→ whereis -l
bin: /usr/bin
bin: /usr/sbin
bin: /usr/lib
man: /usr/share/man/man7
man: /usr/share/man/man0p
man: /usr/share/man/man1
src: /usr/src/debug
src: /usr/src/kernels
...
Enter fullscreen mode Exit fullscreen mode

Its common options are:

-b Search for binaries.
-m Search for manuals.
-s Search for sources.
-u Only show the command names that have unusual entries.
Enter fullscreen mode Exit fullscreen mode

Let’s try it out:

3. The locate command

The locate command is mainly used to quickly find files or directories. It is based on the data file to search, because the scope is small, so the speed is faster. We can see which databases it uses with locate -S:

→ locate -S
Database /var/lib/mlocate/mlocate.db:
        16,847 directories
        160,905 files
        8,598,623 bytes in file names
        4,038,841 bytes used to store database
Enter fullscreen mode Exit fullscreen mode

But be aware that this database file is usually updated once a day (update frequency may vary by Linux distribution), so if you create a new file, it may not be searched. But we can use the updatedb command to actively update the database file and then use locate:

To be more specific, when calling updatedb, it will search for files in the file system according to the configuration in /etc/updatedb.conf, and update the results to the database file.

Check /etc/updatedb.conf, where PRUNE_BIND_MOUNTS is whether to enable restrictions, and yes indicates that the following configuration takes effect. The following PRUNEFS indicates file systems that do not need to be searched, PRUNENAMES indicates file names that do not need to be searched, and PRUNEPATHS indicates directories that do not need to be searched.

Let’s try it out:

→ locate passwd
/etc/passwd
/etc/passwd-
/etc/pam.d/passwd
/etc/security/opasswd
/usr/bin/gpasswd
...
Enter fullscreen mode Exit fullscreen mode

It will list all files that contain passwd in their filenames. Here are also some commonly used options:

-l Exit successfully after finding LIMIT entries.

-i Ignore case distinctions when matching patterns.

-c Instead of writing file names on standard output, write the number of matching entries only.

-r Search for a basic regexp REGEXP.
Enter fullscreen mode Exit fullscreen mode

4. The find command

The find command is the most powerful search command, it searches the file system directly, and with so many options supported, you're sure to find what you're looking for. But correspondingly, if your disk performance is poor, it may take more time.

The usual syntax for find is find [directory] [options] [expression]. Here are a few simple examples:

List all directories, files, and subfiles in the /home directory:

find /home
Enter fullscreen mode Exit fullscreen mode

Search the /home directory for all files larger than 10MB:

find /home -size +10M
Enter fullscreen mode Exit fullscreen mode

List all files in the /home directory that has been modified in the last 7 days:

find /home -mtime +7
Enter fullscreen mode Exit fullscreen mode

Search the system for all files ending with .txt:

sudo find / -name *.txt
Enter fullscreen mode Exit fullscreen mode

Search the system for all files ending in .mp3 and delete all found files:

sudo find / -name "*.mp3" -exec rm -rf {} \;
Enter fullscreen mode Exit fullscreen mode

List all empty files in the current directory:

find . -empty
Enter fullscreen mode Exit fullscreen mode

If you found this helpful, please consider subscribing to my newsletter for more useful articles and tools about web development. Thanks for reading!

Top comments (0)