WCC logo

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.

NOTE: Locate cannot search specific directories like find can.

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
-maxdepth Limits how deep the search goes into subdirectories
-mindepth Skips the top levels and starts searching at a specified depth
-perm Matches files with specific permission bits
-group Matches files owned by a specific group
-newer Matches files modified more recently than another file
-regex Matches file paths against a regular expression. We will be looking at those in the regular expressions sub-chapter
-inum Matches files with a specific inode number
-links Matches files with a specific number of hard links
-uid Matches files with a specific user ID
-gid Matches files with a specific group ID
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 

To find files that are either named "report.txt" or "summary.txt":

find /path/to/search \( -name report.txt -or -name summary.txt \) 

To find files that are not directories:

find /path/to/search ! -type d 

To find files named "report.txt" or "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 

To find a file named "report.txt" in the /home/user directory:

find /home/user -name report.txt

To find files in /var/log larger than 10MB:

find /var/log -type f -size +10M

To find directories under /etc with names containing "conf":

find /etc -type d -name "*conf*"

To find all files in /tmp owned by the user "john":

find /tmp -user john

To find files with exact permissions 644 in the root directory:

find / -perm 644

To find files in /home/user modified in the last 7 days:

find /home/user -mtime -7

To find all ".jpg" files in /home/user, case-insensitive:

find /home/user -iname "*.jpg"

To find executable files in /usr/bin:

find /usr/bin -executable

To find ".txt" or ".md" files in /home/user using logical OR:

find /home/user -type f \( -name "*.txt" -o -name "*.md" \)

To find and delete all ".log" files in /home/user:

find /home/user -type f -name "*.log" -delete
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

To find files exactly 100 bytes in size:

find /path/to/search -size 100c

To find files larger than 500 kilobytes:

find /path/to/search -size +500k

To find files smaller than 10 kilobytes:

find /path/to/search -size -10k

To find files exactly 2 megabytes in size:

find /path/to/search -size 2M

To find files larger than 1 gigabyte:

find /path/to/search -size +1G

To find files smaller than 1 gigabyte:

find /path/to/search -size -1G

To find files exactly 1 block (512 bytes) in size:

find /path/to/search -size 1

To find files between 10k and 100k in size:

find /path/to/search -size +10k -size -100k

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"  | xargs rm

To change permissions of all .sh files to executable using find and xargs:

find /path/to/search -name "*.sh"  | xargs chmod +x

To move all .log files to another directory using find and xargs:

find /path/to/search -name "*.log"  | 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"  | xargs -I {} cp {} /path/to/backup/

To list all .txt files with detailed information using find and xargs:

find /path/to/search -name "*.txt" | 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.