Last modified: November 09, 2018
This article is written in: 🇺🇸
The find, locate, and which commands are commonly used for file search operations. The find command performs a comprehensive search using attributes such as name, size, and type. locate provides a faster, albeit periodically updated, search by filename. which locates the path of a program's executable within the system's PATH.
The find command is used to locate the specific files and directories based on various criteria like file name, size, modification time, etc. It is one of the powerful commands, capable of handling operations such as search, copy, remove, and modify attributes of files/directories.
The general syntax of the find command is as follows:
find [path...] [expression]
[path...] refers to where you want to look for. It can be a single directory or multiple directories.[expression] refers to the search criteria like name, size, file type, etc.The find command includes various options, or "flags," that modify its behavior. Below are some commonly used flags:
| Option | Description |
-name pattern |
Search for files based on their name. |
-type [f\|d\|l] |
Search for files (f), directories (d), or symbolic links (l). |
-user user_name |
Search for files owned by a specific user. |
-size +N |
Search for files larger than N blocks (1 block = 512 bytes). |
-exec command {} \; |
Execute a command on each file that matches the criteria. The {} is replaced by the current file name. |
-delete |
Deletes the files that match the given criteria. |
-ok command {} \; |
Similar to -exec, but asks for affirmation before executing the command. |
To find a specific file named error.log in the /var/log/ directory:
find /var/log -name error.log
Suppose there is a file named error.log in /var/log/app/:
/var/log/app/error.log
To find all files owned by the user admin in the /home directory:
find /home -user admin
Suppose the admin user owns several files in /home/admin/:
/home/admin/file1.txt
/home/admin/file2.log
To find all files in the /home directory not owned by the user guest:
find /home ! -user guest
If guest owns files in /home/guest/, this command excludes those files.
To find files modified more recently than file2:
find -anewer file2
This finds files updated after file2, such as:
file3
file4
To find files newer than file2 and delete them:
find -anewer file2 -exec rm -v {} \;
This will delete files, such as file3 and file4, and print each deleted file's name due to the -v (verbose) option.
find UsageTo find all files larger than 10MB and display them using the ls command:
find / -type f -size +10M -exec ls -lh {} \;
To find and remove all files with the .bak extension in the current directory and its subdirectories:
find . -name "*.bak" -type f -delete
To find all files larger than 2000 blocks (approximately 1MB) and ask the user for permission to remove them:
find $HOME -type f -size +2000 -exec ls -s {} \; -ok rm -f {} \;
🔴 Caution: The find command can be very powerful, but it also poses a risk of unintentional file deletion or modification, especially when combined with -exec or -delete. Always double-check your commands and use -ok instead of -exec when performing critical operations.
The locate command is a quicker alternative to find for searching filenames in the filesystem. It uses a database (updatedb) that stores references to all files in the filesystem. While faster, it may not always have the most up-to-date information as the database is updated periodically (usually through a nightly cron job).
The general syntax of the locate command is as follows:
locate [option] pattern
[option] refers to additional parameters that can be passed to locate.pattern refers to the file or directory name you are searching for.Here are some commonly used options with the locate command:
| Option | Description |
-i |
Ignore case distinctions in both the pattern and the file names. |
-l, --limit, -n |
Limit the number of match results. |
-S, --statistics |
Display statistics about each read database. |
-b, --basename |
Match only the base name against the specified patterns. |
-r, --regexp REGEXP |
Search for a basic regexp REGEXP. |
To find a file called example.txt:
locate example.txt
Suppose example.txt exists in multiple locations:
/home/user/Documents/example.txt
/usr/share/docs/example.txt
To find a file called example.txt and ignore case:
locate -i example.txt
This command will return results like:
/home/user/Documents/Example.txt
/usr/share/docs/example.TXT
To limit the number of returned results to 5:
locate -l 5 example.txt
The output will show only the first 5 matches found:
/home/user/Documents/example.txt
/usr/share/docs/example.txt
/var/log/example.txt
/tmp/example.txt
/etc/example.txt
To match only the base name against the pattern:
locate -b '\example.txt'
This command focuses on the base name, ignoring the directory path:
/home/user/example.txt
/usr/share/example.txt
To search for a regular expression pattern:
locate -r 'ex.*\.txt$'
This command will find files matching the regular expression ex.*\.txt$, such as:
/home/user/Documents/exam.txt
/usr/share/docs/example.txt
The locate command is faster than find but might not always show the most up-to-date information. If the file or directory was recently created or deleted, the database might not reflect the change. The database of filenames is typically stored at /var/lib/mlocate/mlocate.db. To update the database manually, use the updatedb command (requires root privileges).
The which command in Unix/Linux is used to locate the executable file associated with a given command. It searches for the executable in directories specified by the PATH environment variable.
The general syntax of the which command is as follows:
which [option] program_name
[option] refers to additional parameters that can be passed to which.program_name is the name of the executable you want to locate.The which command is used to locate the executable file associated with a given command by searching through the directories listed in the PATH environment variable.
Here are some commonly used options with the which command:
| Option | Description |
-a |
Print all matching pathnames of each argument. |
To find the location of the ls command:
which ls
Output might look like:
/bin/ls
This indicates that the ls executable is located at /bin/ls.
To find all the locations of the python command:
which -a python
Output might include:
/usr/bin/python
/usr/local/bin/python
This indicates that there are multiple python executables located at /usr/bin/python and /usr/local/bin/python.
The which command only searches for executables in directories specified in the PATH variable. If an executable is located elsewhere, which will not be able to find it. This limitation means that if a binary is not in a directory included in PATH, which will not display it, even if it exists on the system.
which command to find the location of executable files for tools like cat, ls, reboot, and chmod.find command to locate all files in your home directory that are larger than 1GB.find or locate to search for all .mp3 files within your home directory. Which method do you find faster?.txt files in your home directory that contain the string "linux". You might need to use a combination of commands to achieve this.find command to search for all symbolic links within the /usr/bin directory./usr/local directory that are owned by the root user./var/log directory that have been modified within the past 24 hours.locate to find all files with the .conf extension. Remember that the database may need to be updated.which to determine the paths of python3 and pip3. Are they in the same directory? What does this tell you about your Python installation?