Linux Find Command

Introduction The find command in Linux is an incredibly powerful tool that allows you to search for files and directories based on various criteria such as name, size, type, and modification date. It’s an essential command for users and system administrators alike, helping to locate files and perform batch operations…

Introduction

The find command in Linux is an incredibly powerful tool that allows you to search for files and directories based on various criteria such as name, size, type, and modification date. It’s an essential command for users and system administrators alike, helping to locate files and perform batch operations efficiently.

Learning how to use the find command will save you time and streamline your file management tasks, especially in large file systems. This blog post will teach you the various uses of the find command with practical examples and tips.

For those looking to further enhance their Linux skills, Server Academy offers comprehensive courses covering everything from basic command-line operations to advanced system administration. Our training equips you with the practical knowledge needed to manage Linux systems effectively.

Free Course: Linux Fundamentals

This free course will teach you the fundamentals of Linux. You’ll learn about Linux distributions, t…

11 Lessons
1 Quizzes
1 Labs
1.5 Hr

Basic Usage of the Find Command

The find command is versatile and powerful, making it essential to understand its basic usage. Knowing the syntax and simple examples will help you get started quickly and effectively.

Syntax of the Find Command

The basic syntax of the find command is:

find [path] [expression]
  • [path]: The directory path where you want to start the search. Use . to represent the current directory.
  • [expression]: Criteria for finding files, such as name, type, size, etc.

The table below is a quick reference to common find command arguments and examples of how to use them:

ArgumentDescriptionExampleCommand
-nameFind files by nameFind files named example.txtfind /path/to/search -name "example.txt"
-typeFind files by type (f, d, l)Find directoriesfind /path/to/search -type d
-sizeFind files by sizeFind files larger than 100MBfind /path/to/search -size +100M
-mtimeFind files by modification time (days)Find files modified in the last 7 daysfind /path/to/search -mtime -7
-atimeFind files by access time (days)Find files accessed in the last 7 daysfind /path/to/search -atime -7
-ctimeFind files by change time (days)Find files changed in the last 7 daysfind /path/to/search -ctime -7
-execExecute a command on found filesDelete files named example.txtfind /path/to/search -name "example.txt" -exec rm {} \;
-deleteDelete found filesDelete files larger than 100MBfind /path/to/search -size +100M -delete
-userFind files by userFind files owned by user johnfind /path/to/search -user john
-groupFind files by groupFind files owned by group adminfind /path/to/search -group admin
-permFind files by permissionsFind files with 755 permissionsfind /path/to/search -perm 755
-mindepthMinimum search depthStart search at least 2 directories deepfind /path/to/search -mindepth 2
-maxdepthMaximum search depthSearch up to 3 directories deepfind /path/to/search -maxdepth 3
-emptyFind empty files or directoriesFind empty directoriesfind /path/to/search -type d -empty
-pruneExclude directories from searchExclude dir_to_exclude directoryfind /path/to/search -path "dir_to_exclude" -prune -o -print
Linux find Command Quick Reference

Simple Examples

Finding Files by Name

To find a file named example.txt in the current directory and its subdirectories:

find . -name "example.txt"

Finding Directories by Name

To find a directory named exampledir in the current directory and its subdirectories:

find . -type d -name "exampledir"

Finding Files by Type

To find all files (not directories) in the /home/user directory:

find /home/user -type f

Using Find to Locate Files and Directories

The find command can locate files and directories based on various criteria, making it a powerful tool for file management. Here are some common examples:

Finding Files Modified Within the Last 7 Days

To find files in the current directory modified within the last 7 days:

find . -type f -mtime -7

Finding Files Larger Than 100MB

To find files larger than 100MB in the /var/log directory:

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

Practical Example

Finding and Listing All JPEG Files

To find and list all JPEG files in the /home/user directory:

find /home/user -type f -name "*.jpg"

Common Options and Their Usage

The find command becomes even more powerful when you utilize its various options to refine your searches. Below are some of the most commonly used options and how to apply them.

Finding Files by Name

To search for files by name, use the -name option. This is useful when you know the name of the file you’re looking for.

find /path/to/search -name "filename"

Example:

find /home/user -name "example.txt"

Finding Files by Type

Use the -type option to filter search results by file type. Common file types include:

  • f for regular files
  • d for directories
  • l for symbolic links
find /path/to/search -type [f|d|l]

Example:

find /home/user -type d

Finding Files by Size

The -size option allows you to find files based on their size. Size specifications can include:

  • + to find files larger than a specified size
  • - to find files smaller than a specified size
  • Exact size with no prefix

Size units can be:

  • c for bytes
  • k for kilobytes
  • M for megabytes
  • G for gigabytes
find /path/to/search -size [+|-]size[ckMG]

Example:

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

Finding Files by Time

You can use the find command to locate files based on their modification, access, or change times. Options include:

  • -mtime for modification time (in days)
  • -atime for access time (in days)
  • -ctime for change time (in days)
find /path/to/search -[m|a|c]time [+-]n

Example:
To find files modified within the last 7 days:

find . -type f -mtime -7

Combining Multiple Criteria

The find command allows you to combine multiple search criteria to refine your results. Use logical operators like -and, -or, and ! (not) to create complex queries.

Example:
To find all .txt files larger than 1MB:

find . -type f -name "*.txt" -and -size +1M

Example:
To find files either named example.txt or modified in the last 2 days:

find . -name "example.txt" -or -mtime -2

Practical Example

Finding Large Log Files Older Than 30 Days

To find log files in /var/log larger than 50MB and older than 30 days:

find /var/log -type f -name "*.log" -size +50M -mtime +30

Advanced Usage and Examples

The find command in Linux offers advanced options that allow for more refined searches. By understanding and utilizing these advanced features, you can perform complex queries and manage files more effectively.

Excluding Directories

There are times when you want to exclude certain directories from your search. The -prune option allows you to skip specified directories, making your search more efficient and focused.

Example:
To search for all .log files in the /var directory but exclude the /var/log directory:

find /var -path "/var/log" -prune -o -type f -name "*.log" -print

Excluding directories is particularly useful when you need to narrow down your search to specific areas of the filesystem without being bogged down by irrelevant directories.

Combining Multiple Criteria

The find command supports combining multiple criteria to refine your searches. By using logical operators like -and, -or, and ! (not), you can create complex queries that meet specific conditions.

Example:
To find all .txt files larger than 1MB and modified within the last 30 days:

find . -type f -name "*.txt" -size +1M -mtime -30

Combining multiple criteria allows for highly specific searches, making it easier to locate exactly what you need.

Finding Files by Permissions

You can search for files based on their permissions using the -perm option. This is particularly useful for identifying files that may have insecure permissions.

Example:
To find all files with 777 permissions (read, write, and execute for all users) in the /home/user directory:

find /home/user -type f -perm 777

Finding files by permissions helps ensure that your system remains secure by identifying potentially vulnerable files.

Practical Example

Finding and Moving Large Media Files

To find all media files larger than 500MB and move them to the /backup directory:

find /path/to/search -type f \( -name "*.mp4" -o -name "*.mp3" -o -name "*.mkv" \) -size +500M -exec mv {} /backup \;

This example shows how you can use the find command to manage large files efficiently by moving them to a designated backup directory.

Finding Files Owned by a Specific User

Using the -user option, you can search for files owned by a specific user. This is helpful for managing user files and ensuring compliance with storage policies.

Example:
To find all files owned by the user john in the /home directory:

find /home -user john

Searching by user ownership is particularly useful in multi-user environments to manage and audit user files.

Practical Example

Cleaning Up Temporary Files

To find and delete all temporary files (ending in .tmp) older than 7 days in the /tmp directory:

find /tmp -type f -name "*.tmp" -mtime +7 -exec rm {} \;

This example demonstrates how to use the find command to automate the cleanup of temporary files, helping to maintain a tidy and efficient filesystem.

Executing Commands on Found Files

One of the powerful features of the find command is its ability to execute other commands on the files and directories it finds. This can be incredibly useful for automating tasks and managing files more efficiently.

Using -exec to Execute Commands

The -exec option allows you to execute any command on the files or directories that find locates. The command is terminated with \;, and the {} placeholder is used within the command to represent the current file being processed.

Example: Moving files
To move all .log files from /var/log to /backup/logs:

find /var/log -type f -name "*.log" -exec mv {} /backup/logs \;

Example: Changing file permissions
To change the permissions of all .sh files to be executable:

find /path/to/search -type f -name "*.sh" -exec chmod +x {} \;

Example: Renaming files
To rename all .txt files by adding a .bak extension:

find /path/to/search -type f -name "*.txt" -exec mv {} {}.bak \;

Using -ok for Interactive Mode

The -ok option works similarly to -exec, but it prompts the user for confirmation before executing the command on each file. This is useful when you want to ensure that you are performing actions on the correct files.

Example: Deleting files with confirmation
To delete .log files but ask for confirmation first:

find /var/log -type f -name "*.log" -ok rm {} \;

Example: Moving files with confirmation
To move .log files and confirm each move:

find /var/log -type f -name "*.log" -ok mv {} /backup/logs \;

Practical Example

Compressing Large Files

To compress all .log files larger than 100MB in the /var/log directory:

find /var/log -type f -name "*.log" -size +100M -exec gzip {} \;

Using the -exec and -ok options with the find command greatly expands its capabilities, allowing you to automate a wide range of tasks. By understanding how to use these options, you can streamline your workflow and efficiently manage files in your Linux environment.

Conclusion

The find command in Linux is an incredibly versatile tool that allows users to locate and manage files efficiently. By mastering its various options and advanced features, you can perform complex searches, execute commands on found files, and automate numerous file management tasks.

Summary of Key Points

  • Basic Usage: Understanding the basic syntax and using simple examples to locate files by name, type, size, and modification time.
  • Common Options: Utilizing options such as -name, -type, -size, and -mtime to refine your searches.
  • Advanced Usage: Combining multiple criteria, excluding directories, and finding files based on permissions.
  • Executing Commands: Using -exec and -ok to execute commands on found files, allowing for automation of tasks like moving, renaming, and compressing files.

Final Recommendations

To get the most out of the find command, practice using it in different scenarios. Experiment with combining options and using -exec to automate repetitive tasks. By doing so, you’ll become more efficient in managing your Linux file system.

Take your Linux Skills to the Next Level!

If you want to deepen your understanding of Linux commands and become proficient in system administration, check out our free Linux Fundamentals course at Server Academy. Our training programs cover everything from basic command-line operations to advanced server management, equipping you with the skills needed to excel in the IT field.

Feel free to explore our blog for more tutorials and tips on using various Linux commands and tools. If you have any questions or would like to share your experiences with the find command, leave a comment below! Thank you for reading, and happy file searching!

CREATE YOUR FREE ACCOUNT & GET OUR

FREE IT LABS

profile avatar

Paul Hill

Paul Hill is the founder of ServerAcademy.com and IT instructor to over 500,000 students online!