5 Command Line Tools for Quickly Finding Files in Linux

5 Command Line Tools for Quickly Finding Files in Linux

For beginners, searching or locating files in the Linux terminal can be somewhat challenging. However, the system provides several efficient command line tools (or utilities) to help users quickly locate files.

This article will detail five of the most commonly used command line tools that can easily find, locate, and search for files in the Linux system.

1. <span><span>find</span></span> command: The most powerful recursive search tool

<span>find</span> is a powerful and widely used command line tool for recursively searching and locating files that match specific patterns within a directory hierarchy, and its usage is very flexible.

1.1 Basic Usage

We simply need to provide a starting point for the search (i.e., the top of the directory hierarchy, such as the current directory <span>.</span> or another directory where we want to find files), and then specify an **expression to describe how to match files and what actions to perform on the matched files.

1.2 Core Advantages

<span>find</span> supports fine filtering through various file attributes, including:

  • Name Pattern (<span>-name</span>, <span>-iname</span> case insensitive)
  • File Type (<span>-type f</span> for files, <span>-d</span> for directories)
  • File Size (<span>-size</span>)
  • Modification/Access Time (<span>-mtime</span>, <span>-atime</span>)
  • Permissions (<span>-perm</span>)
  • Owner/Group (<span>-user</span>, <span>-group</span>)
1.3 Common Examples
# Find all files named "filename.txt" in the current directory and subdirectories
find . -name "filename.txt"

# Find all files with the extension ".conf" in the /home directory
find /home -name "*.conf"

# Find files modified in the last 7 days
find /var/log -type f -mtime -7

# Find files larger than 100MB and list their details
find / -type f -size +100M -exec ls -lh {} \;

2. <span><span>locate</span></span> command: Fast search based on a database

<span>locate</span> is another commonly used tool for quickly finding files by name. Unlike <span>find</span>, which traverses the file system in real-time, <span>locate</span> is extremely fast because it queries a pre-built database of file paths (usually located at <span>/var/lib/mlocate/mlocate.db</span>).

2.1 How It Works and Considerations

This database is created and updated by the <span>updatedb</span> command (typically run automatically via a daily cron job). This means:

  • Advantages: Unmatched speed, results are returned almost instantaneously.
  • Disadvantages: Cannot find files created after the last database update, and may search for deleted files that have not yet been cleaned from the database. If search results are inaccurate, you can manually run <span>sudo updatedb</span> to update the database.
2.2 Common Examples
# Find all files with "filename" in their path
locate filename

# Use wildcards for pattern matching
locate "*.pdf"

# Show the number of found entries (using the `-c` option)
locate -c "*.conf"

3. <span><span>grep</span></span> command: Search by file content

Strictly speaking, <span>grep</span> is not directly used to find files, but rather to search for lines that match specific patterns (strings, regular expressions) within file contents. However, we can cleverly use it to “locate” files that contain specific content.

3.1 Core Uses

When we do not remember the file name but recall a specific word or code snippet within the file, <span>grep</span> is the best choice.

3.2 Common Examples
# Recursively search for files containing "target_string" in the current directory and display matching lines
grep -r "target_string" .

# Recursively search (case insensitive) and only display file names containing matches (using the `-l` option)
grep -ril "example_pattern" /path/to/search/

# Perform advanced search with regular expressions
grep -r "^#include" ~/projects/
  • <span>-r</span> or <span>-R</span>: Recursively search subdirectories.
  • <span>-i</span>: Case insensitive.
  • <span>-l</span>: Only list file names containing matches, without showing specific matching lines.
  • <span>-n</span>: Show the line numbers of matching lines.

4. <span><span>which</span></span> command: Locate the path of executable commands

<span>which</span> is a small and straightforward command used to locate the absolute path of an executable file (binary file) for a given shell command. When we enter a command in the terminal, <span>which</span> can tell us which file the shell will actually call.

# Common Examples
$ which python
/usr/bin/python

$ which ls
/usr/bin/ls

Note: <span>which</span> only searches in the directories specified by the current user’s <span>PATH</span> environment variable.

5. <span><span>whereis</span></span> command: Locate the binary, source, and manual pages of commands

<span>whereis</span> command is used to locate related files of a command, providing more comprehensive information than <span>which</span>. It can not only find the location of binary files but also simultaneously locate its source code (if installed) and the path of the manual page (man page).

# Common Examples
$ whereis find
find: /usr/bin/find /usr/share/man/man1/find.1.gz

$ whereis python
python: /usr/bin/python3.8 /usr/bin/python /usr/lib/python3.8 /usr/include/python3.8 /usr/share/man/man1/python.1.gz

From the output, it is clear that the <span>find</span> command’s binary file is located at <span>/usr/bin/find</span>, while its manual page is at <span>/usr/share/man/man1/find.1.gz</span>.

6. Summary and Recommendations

Tool Main Use Features Applicable Scenarios
<span><span>find</span></span> Recursive search by name, attributes Most powerful, real-time accuracy, relatively slow Need precise search based on various conditions (time, size, etc.)
<span><span>locate</span></span> Fast search by name Fastest speed, database-based, results may not be up-to-date Quickly search by file name without requiring absolute real-time results
<span><span>grep</span></span> Search by file content Search internal content of files, supports regular expressions Do not remember the file name but recall specific content within the file
<span><span>which</span></span> Locate the binary file of a command Simple, only checks <span>PATH</span> variable Want to know which program is actually called by the command entered
<span><span>whereis</span></span> Locate binary, source, and manual pages of commands Provides more comprehensive command-related information Want to understand all related file locations of a command at once

5 Command Line Tools for Quickly Finding Files in Linux

Leave a Comment