Fading Coder

One Final Commit for the Last Sprint

Home > Tech > Content

Essential File Search Commands in Linux: which, whereis, locate, and find

Tech 2

which: Locating Executable Files

which searches for executable files within the directories specified in the PATH environment variable. It returns the full path of the first matching executable.

which ls  # Outputs the path to the ls executable
which -a ls  # Lists all ls executables found in PATH

whereis: Seraching in Standard Directories

whereis looks for files in predefined system directories, such as those for binaries, manuals, and source files.

whereis [options] filename

Common options:

  • -b: Search only for binary files
  • -m: Search only manual pages
  • -s: Search only source files
  • -u: Find files not in the above categories
  • -l: Display the directories where whereis searches

Example:

whereis -b ls  # Finds binary files named ls

locate: Fast File Search Using a Data base

locate performs quick searches by querying a pre-built database of file paths. Since the database updates periodically (often daily), newly created files might not appear until the next update. Use updatedb to manually refresh the data base.

locate [options] keyword

Key options:

  • -i: Ignore case differences
  • -c: Count matching files instead of listing them
  • -l N: Limit output to N lines (e.g., -l 5)
  • -r: Use regular expressions for pattern matching
  • -S: Show database statistics

Example:

sudo updatedb  # Update the database before searching
locate -i config.txt  # Case-insensitive search for config.txt

find: Comprehensive File System Search

find scans the file system in real-time, offering extensive filtering options but potentially slower performence. It is useful for precise searches based on various criteria.

Time-Based Searches

Find files modified within a specific time frame.

find /path -mtime n  # Files changed exactly n days ago
find /path -mtime +n  # Files changed more than n days ago
find /path -mtime -n  # Files changed within the last n days
find /path -newer reference_file  # Files newer than reference_file

Example:

find / -mtime 0  # Lists files modified in the last 24 hours

User and Group Searches

Locate files based on ownership.

find /path -user username  # Files owned by a specific user
find /path -group groupname  # Files owned by a specific group
find /path -nouser  # Files with no valid owner in /etc/passwd
find /path -nogroup  # Files with no valid group in /etc/group

Example:

find /home -user alice  # Find all files owned by alice in /home

Permission and Name Searches

Search by file permissions, names, or types.

find /path -name "filename"  # Exact filename match
find /path -name "*pattern*"  # Pattern match with wildcards
find /path -size +50k  # Files larger than 50 KB
find /path -type f  # Regular files (use d for directories, l for links, etc.)
find /path -perm 644  # Files with exact permissions 644
find /path -perm -644  # Files with permissions including 644
find /path -perm /644  # Files with any of the permissions in 644

Example:

find / -name "*.conf"  # Find all .conf files
find /var/log -type f -size +1M  # Find files larger than 1 MB in /var/log

Related Articles

Understanding Strong and Weak References in Java

Strong References Strong reference are the most prevalent type of object referencing in Java. When an object has a strong reference pointing to it, the garbage collector will not reclaim its memory. F...

Comprehensive Guide to SSTI Explained with Payload Bypass Techniques

Introduction Server-Side Template Injection (SSTI) is a vulnerability in web applications where user input is improper handled within the template engine and executed on the server. This exploit can r...

Implement Image Upload Functionality for Django Integrated TinyMCE Editor

Django’s Admin panel is highly user-friendly, and pairing it with TinyMCE, an effective rich text editor, simplifies content management significantly. Combining the two is particular useful for bloggi...

Leave a Comment

Anonymous

◎Feel free to join the discussion and share your thoughts.