Skip to main content

Using the Find Command in Linux

Search and act on files and directories in Linux using the find command's name, type, size, time, and exec options.

Updated 6 min readMarkdown
Using the Find Command in Linux

The find command in Linux is a powerful and versatile utility for searching or locating files and directories in a file system. It uses file attributes like name, size, time, and permissions to perform the search, and you can combine it with other commands to perform complex searches. This guide walks through the basic syntax, the most common options, and practical and advanced examples using a set of sample files and directories. By the end, you'll have a working set of find commands for searching, filtering, and acting on files by name, type, size, time, owner, and permissions, following Linux file search practices documented in Vultr Docs.


The find Command Syntax

The basic find command syntax is:

find [path] [expression]

[path] specifies the directory to search and defaults to the working directory (.) if you don't specify one. [expression] combines options, tests, and actions to filter and manipulate the search results, such as filename, file type, size, and modification time.

Set Up Example Directories and Files

1. Switch to your user's home directory:

cd

2. Create demo.txt, Demo.txt, and DEMO.txt sample files:

touch demo.txt Demo.txt DEMO.txt

3. Create dir and dir1 directories, then create a sub_dir sub-directory under dir1:

mkdir -p dir dir1/sub_dir

4. Create a new file.txt text file:

touch file.txt

5. Change the file.txt file permissions mode to 755:

chmod 755 file.txt

Most Common find Command Options

The find command supports the following options for refining search results:

Option Description
-name Searches for files and directories with a specific name.
-iname Case-insensitive version of -name.
-type Searches for files of a specific type (f for regular files, d for directories).
-size Searches for files of a specific size (for example, +100M for files larger than 100 MB).
-mtime Searches for files modified within a specific number of days (for example, -7 for files modified in the last 7 days).
-atime Searches for files accessed within a specific number of days.
-user Searches for files owned by a specific user.
-group Searches for files owned by a specific group.
-perm Searches for files with specific permissions (for example, 755).
-exec Executes a command on each matched file.
-delete Deletes the matched files.
-ls Lists the matched files in ls -dils format.
-print Prints the full filename on the standard output.

Practical find Command Examples

1. Find a file named demo.txt in your working directory and subdirectories:

find . -name "demo.txt"

2. Perform a case-insensitive search for all files that match demo.txt:

find . -iname "demo.txt"

3. Find all directories in the working directory:

find . -type d

4. Find files by size, for example files larger than 100MB:

find . -size +100M

5. Find files by modification time, for example files modified in the last 7 days:

find . -mtime -7

6. Find files by access time, for example files accessed in the last 7 days:

find . -atime -7

7. Find all files owned by a specific user, such as demo_user:

find . -user demo_user

8. Find all files owned by a specific group, such as demo_user:

find . -group demo_user

9. Find all files with 755 permissions:

find . -perm 755

The 755 permission means the owner can read, write, and execute the file, while others can only read and execute it.

Advanced find Command Options

1. Execute a command on each matching file using -exec:

find . -name "demo.txt" -exec ls -l {} \;

This finds all files named demo.txt and runs ls -l on each one to list them in long format. {} is a placeholder for the matched filename, and \; marks the end of the command to execute.

2. Delete all files named demo.txt:

find . -name "demo.txt" -delete

3. Find all .txt files and copy them to another directory:

find . -name "*.txt" -exec cp {} /path/to/directory/ \;

This finds all .txt files and copies them to /path/to/directory/, with the filename replacing {} during execution. Replace /path/to/directory with your actual path.

4. Find all .txt files and move them to another directory:

find . -name "*.txt" -exec mv {} /path/to/directory/ \;

5. Find all .txt files owned by a specific user, such as demo_user, and modified in the last 7 days:

find . -name "*.txt" -user demo_user -mtime -7

This combines -name, -user, and -mtime to create a more specific search.

Interactive find Command Examples

1. Find all files named Demo.txt and list them with detailed information:

find . -name "Demo.txt" -exec ls -l {} \;

This uses ls -l to display detailed information about each matched file, such as permissions, owner, size, and modification date.

2. Combine find with wc to locate all files named Demo.txt and count them:

find . -name "Demo.txt" | wc -l

This pipes the results to wc -l, which counts the number of matching lines.

3. Find all files with 644 permissions and change them to 600:

sudo find . -perm 644 -exec chmod 600 {} \;

This finds all files with 644 permissions and changes the mode to 600 using chmod. {} represents the filename, and \; marks the end of the command.

Next Steps

  • Combine -mtime, -size, and -user filters to build a single targeted cleanup command for your own file system.
  • Swap -exec ls -l {} \; for -exec rm {} \; or -delete once you've confirmed a search matches exactly the files you expect — always dry-run with -exec ls first.
  • Pair find with xargs for better performance on very large result sets instead of -exec.
  • Look into find's -newer test for finding files modified more recently than a reference file.

For the full guide with additional tips, visit the original article on Vultr Docs.

About the authors

  • A Developer Advocate with a focus on improving the developer experience through clear communication, technical enablement, and community engagement.
  • DevOps Engineer with experience in Kubernetes, automation, cloud infrastructure, and observability. I work in Developer Relations, contribute to technical documentation, and collaborate on engineering-focused projects.

No comments yet. Be the first to comment.