Unix and Linux Guide: How to Use the Find Command

ยท

3 min read

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.).

๐Ÿ’ก
The default path of the find command is the current directory. The default expression is the print

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

  1. 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.
  2. 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 {} \;
    

  3. 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
    

  4. 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 than n days ago;

    • -n - in the last n days;

    • n - exactly n days ago;

  1. 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 the cp command against every item in the result;

  • {} - a placeholder for the actual item itself. So every time a find command finds something, it puts the result here;

  • \; terminates the find command. You can also use + instead of \; sign as a terminator. You should terminate the find command anytime you use the -exec option.

  1. 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

  1. Use Quotes for Patterns: Always use quotes around patterns to avoid shell interpretation.

  2. Combine Expressions: Use multiple expressions to narrow down your search.

  3. Test Before Executing: Use -print to test your find command before adding -exec.

References:

  1. How to use FIND in Linux

  2. find(1) โ€” Linux manual page

  3. 10 ways to use the Linux find command

  4. Linux Crash Course - The find command-

ย