Unix and Linux Guide: How to Use the Find Command

Postgraduate in Communications Engineering with working experience in the Support Desk and self-study in software development.
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
-nameoption:sudo find / -name "*fish*"
This command searches the entire filesystem (
/) for files and directories whose names contain the substring "fish".sudoprovides root access, ensuring no permission issues, and produces a clean output stream of matching file names.headreceives a continuous and uninterrupted stream of matching filenames correctly displaying the first 5 lines from this stream.💡The search with-nameoption is case-sensitive. To make the search case-insensitive, use the-inameoption instead.To search for files of a specific type, use the
-typeoption:sudo find /snap name "cat" -type f
To search for both files and directories simultaneously, use
-ooperator 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
-sizeoption: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-ctimeoptions: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 thanndays ago;-n- in the lastndays;n- exactlyndays ago;

To find all
.conffiles in/etcand copy them to~/backup/etc, use a new option-execwhich executes a command against all files found in the result.sudo find /etc -type f -name "*.conf" -exec cp {} ~/backup/etc/ \;
-execexecutes thecpcommand against every item in the result;{}- a placeholder for the actual item itself. So every time afindcommand finds something, it puts the result here;\;terminates thefindcommand. You can also use+instead of\;sign as a terminator. You should terminate thefindcommand anytime you use the-execoption.
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
-printto test yourfindcommand before adding-exec.




