CIS120Linux Fundementals
locate and find comands
The locate
and find
commands are powerful tools in Linux for searching files and directories. Each serves a different purpose and has distinct advantages and characteristics.
The locate Command
The locate
command searches for files and directories by name using a prebuilt database. This database is created and updated regularly by the updatedb
command, making locate
extremely fast for searching. However, because it relies on this database, the results might not reflect recent changes to the filesystem unless the database has been updated.
Basic usage of locate
:
locate [options] pattern
Commonly Used locate
Options:
Option | Description |
---|---|
-i |
Perform case-insensitive matching |
-r |
Use regular expressions |
-n |
Limit the number of results |
-e |
Print only existing files |
--existing |
Same as -e , print only existing files |
--regex |
Same as -r , interpret the pattern as a regex |
Examples:
To find files with "report" in their name:
locate report
To find files with "report" in their name, case-insensitively:
locate -i report
To find files using a regular expression:
locate -r 'report[0-9]*\.txt'
To update the locate
database:
sudo updatedb
This command updates the database that locate
uses, ensuring the search results are current.
The find Command
The find
command is used to search for files and directories within the filesystem based on various criteria. Unlike locate
, find
searches the directory tree in real-time, making it more versatile but generally slower.
Basic usage of find
:
find [path] [expression]
Tests
Tests are criteria used by find
to match files and directories.
Test | Description |
---|---|
-name |
Matches files with the specified name |
-iname |
Matches files with the specified name, case-insensitive |
-type |
Matches files of the specified type (f for file, d for directory) |
-size |
Matches files of the specified size |
-mtime |
Matches files modified n days ago |
-user |
Matches files owned by the specified user |
-cmin |
Matches files changed n minutes ago |
-cnewer |
Matches files changed more recently than another file |
-ctime |
Matches files changed n days ago |
-empty |
Matches empty files and directories |
Operators
Operators are used to combine multiple tests in find
.
Operator | Description |
---|---|
-and |
Logical AND |
-or |
Logical OR |
! |
Logical NOT |
Predefined Actions
Actions are operations that find
performs on the matched files and directories.
Action | Description |
---|---|
-print |
Print the matching files |
-delete |
Delete the matching files |
-exec |
Execute a command on matching files |
Examples of Using Operators and Predefined Actions:
To find files named "report.txt" and print their names:
find /path/to/search -name report.txt -print
To find files that are either named "report.txt" or "summary.txt":
find /path/to/search \( -name report.txt -or -name summary.txt \) -print
To find files that are not directories:
find /path/to/search ! -type d -print
To find files named "report.txt" and "summary.txt" and delete them:
find /path/to/search \( -name report.txt -or -name summary.txt \) -delete
To find files owned by "john" and modified in the last 7 days:
find /path/to/search -user john -and -mtime -7 -print
Size Options
Size options specify the size criteria for matching files.
Option | Description |
---|---|
+n |
Greater than n units |
-n |
Less than n units |
n |
Exactly n units |
c |
Bytes (default) |
k |
Kilobytes (1024 bytes) |
M |
Megabytes (1024 kilobytes) |
G |
Gigabytes (1024 megabytes) |
Examples of Size Options:
To find files larger than 1MB:
find /path/to/search -size +1M
To find files between 1MB and 10MB:
find /path/to/search -size +1M -size -10M
To find files smaller than 1MB:
find /path/to/search -size -1M
Using the -exec Option
The -exec
option in the find
command allows you to execute a command on each file that matches the search criteria. The {}
placeholder is used to represent the current file, and \;
is used to terminate the command.
Examples with -exec
:
To delete all .tmp
files:
find /path/to/search -name "*.tmp" -exec rm {} \;
To change permissions of all .sh
files to executable:
find /path/to/search -name "*.sh" -exec chmod +x {} \;
To move all .log
files to another directory:
find /path/to/search -name "*.log" -exec mv {} /path/to/destination/ \;
To copy all .conf
files to a backup directory:
find /path/to/search -name "*.conf" -exec cp {} /path/to/backup/ \;
To list all .txt
files with detailed information:
find /path/to/search -name "*.txt" -exec ls -l {} \;
Understanding xargs
The xargs
command is used to build and execute command lines from standard input. It is often used in conjunction with find
to handle a large number of files.
Examples with xargs
:
To delete all .tmp
files using find
and xargs
:
find /path/to/search -name "*.tmp" -print | xargs rm
To change permissions of all .sh
files to executable using find
and xargs
:
find /path/to/search -name "*.sh" -print | xargs chmod +x
To move all .log
files to another directory using find
and xargs
:
find /path/to/search -name "*.log" -print | xargs -I {} mv {} /path/to/destination/
To copy all .conf
files to a backup directory using find
and xargs
:
find /path/to/search -name "*.conf" -print | xargs -I {} cp {} /path/to/backup/
To list all .txt
files with detailed information using find
and xargs
:
find /path/to/search -name "*.txt" -print | xargs ls -l
Summary
The locate
and find
commands are essential for searching files and directories in Linux. locate
uses a prebuilt database for fast searches and is ideal for quick lookups. However, it requires the database to be updated regularly. find
is more versatile, allowing for real-time searches based on various criteria, but can be slower. The -exec
option in find
allows executing commands on matched files, and xargs
is a powerful tool to handle large numbers of files efficiently. By mastering these commands and understanding their options, you can efficiently locate and manage files in the Linux environment.