The find
command in Unix is a powerful utility used for searching files and directories based on various criteria such as name, type, size, permissions, modification time, and more. It recursively descends into directory trees and matches conditions specified by the user. The basic syntax is:
find [path] [expression]
[path]: The directory where the search begins. If omitted, it defaults to the current directory (
.
).[expression]: Criteria for searching (e.g., name, type, size, etc.).
Hands-on Exercise Overview
This hands-on exercise shows how to use find
command and its different options to search for files and directories on Ubuntu 22.04. It focuses on basic file searching, advanced searching techniques, and executing commands on found files.
Hands-on Exercise
To search for a file with a specific name, use the
-name
option:sudo find / -name "*fish*"
This command searches the entire filesystem (
/
) for files and directories whose names contain the substring "fish".sudo
provides root access, ensuring no permission issues, and produces a clean output stream of matching file names.head
receives a continuous and uninterrupted stream of matching filenames correctly displaying the first 5 lines from this stream.๐กThe search with-name
option is case-sensitive. To make the search case-insensitive, use the-iname
option instead.To search for files of a specific type, use the
-type
option:sudo find /snap name "cat" -type f
To search for both files and directories simultaneously, use
-o
operator which stands for OR operator:sudo find / -name "vagrant" \( -type f -o -type d\) -exec ls -ld {} \;
To find files of a specific size, use the
-size
option:sudo find /var/log -size 100c sudo find /var/log -size -1K sudo find /var/log -size +100M sudo find /var/log -size 2G
To search for files based on their modification, access, and change times use
-mtime
,-atime
, and-ctime
options:find /path/to/search -mtime +n find /path/to/search -atime -n find /path/to/search -ctime n
-mtime
- Modification time, when the file content was last modified;-atime
- Access time, when the file was last accessed;-ctime
- Change time, when the file's metadata or content was last changed;+n
- more thann
days ago;-n
- in the lastn
days;n
- exactlyn
days ago;
To find all
.conf
files in/etc
and copy them to~/backup/etc
, use a new option-exec
which executes a command against all files found in the result.sudo find /etc -type f -name "*.conf" -exec cp {} ~/backup/etc/ \;
-exec
executes thecp
command against every item in the result;{}
- a placeholder for the actual item itself. So every time afind
command finds something, it puts the result here;\;
terminates thefind
command. You can also use+
instead of\;
sign as a terminator. You should terminate thefind
command anytime you use the-exec
option.
To change the permissions of several files to be readable and writable simultaneously, type:
find ~ -type f -name "*.txt" -exec chmod 600 {} \; find ~ -name "*.txt" -exec ls -ld {} +
Best Practices
Use Quotes for Patterns: Always use quotes around patterns to avoid shell interpretation.
Combine Expressions: Use multiple expressions to narrow down your search.
Test Before Executing: Use
-print
to test yourfind
command before adding-exec
.