In Linux, finding files can be a frequent task, whether you’re working on a system with numerous directories or trying to locate a specific file buried deep within the filesystem. To aid in this process, there are several command-line tools available, each designed to help users quickly search and identify files. Among the most commonly used are the find, fd, and locate commands.
This article will take a closer look at each of these tools, explaining their functionalities, use cases, and some common examples.
find: The Powerhouse of File Search
The find command is one of the oldest and most powerful file search utilities in Unix-like operating systems. It recursively searches through directories to locate files that match specific criteria.
Key Features:
- Search by name: Find files by their name, extension, or pattern.
- Search by file type: Locate directories, regular files, symbolic links, etc.
- Search by time: Find files based on modification or access times.
- Search by size: Locate files that match certain size conditions.
- Search by permissions: Look for files with specific permission settings.
- Search by user/group: Find files owned by a specific user or group.
Common Examples:
# Search for a file by name:
find /path/to/directory -name "filename.txt"
# Search for files modified in the last 7 days:
find /path/to/directory -mtime -7
# Search for empty files:
find /path/to/directory -type f -empty
# Search by file size (greater than 100MB):
find /path/to/directory -size +100M
# Search based on extention/pattern
find /path/to/directory -name *.txt
find /path/to/directory -name "serv*.txt"
# Search and delete
find /path/to/directory -name sample.txt -exec rm -i {} \;
find /path/to/directory -name "sample.txt" -delete
# Search Files with Specific Permissions
find /path/to/directory -perm 666
# Search only dirs
find /path/to/directory -type d
# Search all Files by Location
find /path/to/directory
# Search only files which are empty
find /path/to/directory -type f -empty
# Search by Content
find /path/to/directory -type f -exec grep -l "Sample text in file" {} +
# case-insensitive search
find /path/to/directory -iname "*foo*txt*png"
# Limit listing results (search depth)
find /path/to/directory -maxdepth 1 -type d
fd: The Fast and User-Friendly Search Tool
The fd command is a modern alternative to find, designed to be faster and more user-friendly. It is written in Rust and focuses on speed, simplicity, and ease of use. Unlike find, fd uses regular expressions by default and provides colorized output for easier readability.
Key Features:
- Fast: fd is optimized for speed and can outperform find in many cases.
- Simplified Syntax: fd simplifies many of the more complex aspects of find (e.g., searching by name, extension, or file type).
- Regular Expressions: Built-in support for regular expressions, which makes pattern matching more powerful.
- Colorized Output: Provides color-coded results to improve readability.
- Excludes Hidden Files by Default: fd ignores hidden files and directories unless explicitly told otherwise.
Installing fd command
# on debian/Ubuntu
sudo apt install fd-find
# if your use fd on ubunutu based distro ,
# it is recommende to make alias for fdfind in your bash.rc by
alias fd=fdfind
# on Archlinux
sudo pacman -S fd
Common Examples:
# Search for a file by name:
fd "filename.txt"
# Search for files with a specific extension:
fd --glob "*.txt"
# Search within a specific directory:
fd "*txt" /path/to/directory
# Exclude hidden files (hidden files are excluded by default):
fd "pattern" --no-hidden
# Find files that begin with `foo`:
fd "^foo"
# Find files with a specific extension:
fd --extension txt
fd -e 'ino'
# Show search results from files and dirs that would otherwise be ignored
# .gitignore , .ignore , .fdignore ,
fd -I "myfile"
# Case sensitive search
fd -s "Myfile"
# Search by file type
fd -t f "ino"
# Search by dir type
fd -t d "ino"
fd -t d 'myDirectory'
# Search by executable
fd -t x
# Search by empty files/dir
fd -t e
# Search by symlink
fd -t l
# searching hidden files and directories.
fd -H 'filename'
# Searches for files with specific permissions
fd -p 'rwx' 'filename'
# Limits the directory depth.
fd -d 2 'filename'
# Executes a oommand for each search result.
fd -e zip -x unzip {}
fd -e zip -x unzip {} -d {//}
fd -e png -x rm
fd -e png -X rm -i # delete interactively
# Find and remove file
fd -e ino | xargs rm
# Excluding specific files or directories
fd -H -E .git
locate: The Instant File Lookup Tool
The locate command is a fast way to search for files by name. Unlike find and fd, locate doesn’t search the filesystem in real-time. Instead, it relies on an indexed database that is updated periodically by the updatedb command. This makes locate extremely fast but potentially out of date if the index is not up to date.
Key Features:
- Instant Results: locate provides very quick search results by looking up a pre-built index.
- Search by Name: It only supports searching for files by their name or part of their name.
- Requires Index Update: The search database must be periodically updated with updatedb to ensure it is current.
- Can Search Entire Filesystem: It searches the entire indexed filesystem by default, but this can be restricted with options.
Installing locate command
# on debian/ubuntu
sudo apt install plocate
# on Archlinux
sudo pacman -s mlocate
Common Examples:
# Search for a file by name:
locate filename.txt
# Search for files containing a specific pattern:
locate "*.txt"
# Update the search database (typically done automatically via cron jobs):
sudo updatedb
# Limit Search Queries to a Specific Number
locate "*.html" -n 10
# print number of Matching Entries
locate -c [.png]*
# Case Sensitive search
locate -i *SAMPLE.cpp*
Conclusion
In summary, all three tools—find, fd, and locate—offer powerful ways to search for files in Linux. The best tool to use depends on your specific needs:
- find is a highly flexible tool that can handle complex searches based on various criteria.
- fd is a modern, faster alternative with a simple interface, perfect for regular searches when speed and ease of use are a priority.
- locate is ideal for users who need lightning-fast searches on an indexed database, with the tradeoff being the need to regularly update the index.
By understanding the strengths and weaknesses of each, you can choose the right tool for your task, ensuring that you can quickly and efficiently locate files on your Linux system.