Learn Find Command and usages

Introduction

The Linux find command is used to search for files and directories based on different conditions such as name, type, permissions, ownership, age, and access time.

In this guide, we will cover commonly used find command examples that can help with everyday Linux server administration and file management tasks.

Prerequisites

Before using the commands in this guide:

  • You should have access to a Linux server or terminal.
  • Basic knowledge of Linux commands is recommended.
  • Make sure you have appropriate permissions to access the directories you are searching.

Implementation

Find a File by Name

To find a file named test.txt under /home/pheonixsolutions/:

find /home/pheonixsolutions/ -type f -name test.txt

This searches the directory and its subdirectories for the specified file.

Find Files Using Wildcards

To find files whose names start with test and have a .txt extension, such as test1.txt, test2.txt, and test3.txt:

find /home/pheonixsolutions/ -type f -name test*.txt

Example output:

/home/pheonixsolutions/test1.txt
/home/pheonixsolutions/test2.txt
/home/pheonixsolutions/test3.txt

Find a Directory by Name

To find a directory named dir from the current working directory:

find . -type d -name dir

Example output:

./dir1/dir
./dir

Find Directories Up to a Maximum Depth

To find a directory named dir with a maximum search depth of 2:

find . -type d -name dir -maxdepth 2

Example output:

./dir1/dir
./dir

Find Directories at a Minimum Depth

To find a directory named dir starting from a minimum depth of 2:

find . -type d -name dir -mindepth 2

Example output:

./dir1/dir
./dir1/dir2/dir

Find Files by Permission

To find files that have permission 644:

find . -type f -perm 644

Example output:

./dir1/file1.txt
/dir/dir1/file2.txt
file.txt

Execute a Command on Find Results

The -exec option allows you to run another command on the files returned by find.

For example, to list files with detailed information:

find . -type f -exec ls -la {} \;

Here, {} represents each file found by the find command.

Change File Permissions Using find

The following command changes the permissions of the files found:

find . -type f -exec chmod 644 {} \;

Use this carefully, especially when running it against system or application directories.

Use xargs with find

You can pass the results of find to another command using xargs.

For example:

find . -type f | xargs ls -la

This runs ls -la against the files returned by find.

Use Find Results in Multiple Places

The -I option of xargs allows the result to be substituted in multiple positions.

For example:

find . -type f | xargs -I {} mv {} {}_bak

This renames the matching files by adding _bak to their names.

Find Files Older Than 3 Days

To find files modified more than 3 days ago:

find . -type f -mtime +3

Delete Old Files

To delete files older than 5 days:

find . -type f -mtime +5 -exec rm {} \;

Use deletion commands carefully and verify the search results before removing files.

Find Files Accessed More Than 5 Days Ago

To find files that were accessed more than 5 days ago:

find . -type f -atime +5

Find Files by Owner

To find files owned by the pheonixsolutions user:

find . -type f -user pheonixsolutions

Find Files by Group

To find files belonging to the pheonixsolutions group:

find . -type f -group pheonixsolutions

Conclusion

The Linux find command is a powerful tool for locating and managing files and directories. It can search based on names, types, permissions, ownership, modification time, access time, and other conditions.

Using find together with options such as -exec and xargs makes it useful for everyday server administration, file cleanup, permission management, and automation tasks.

FAQs

1. What is the find command used for?

The find command is used to search for files and directories based on conditions such as name, type, permissions, owner, group, and file age.

2. How do I find a file by name?

Use:

find /path -type f -name filename

For example:

find /home/pheonixsolutions/ -type f -name test.txt

3. What is the difference between -mtime and -atime?

-mtime searches based on when a file was modified, while -atime searches based on when a file was last accessed.

4. How can I execute a command on files found by find?

Use the -exec option:

find . -type f -exec ls -la {} \;

5. Can find be used to delete files?

Yes. For example:

find . -type f -mtime +5 -exec rm {} \;

Always verify the files matched by the search before using a deletion command.

Enable Command Logging on Linux Hosts

Learn how to enable command logging on Linux hosts using Bash and rsyslog to track user commands and server activity.

Overview of Docker

Learn the basics of Docker, including container concepts and how Docker is used to run applications in isolated environments.

Talk to our experts

Looking for the right technology solution for your business? Our team of experts can help you with development, cloud, DevOps, design, and a wide range of other technology needs. Get in touch with our team here.

admin

Writes about Security at Pheonix Solutions.

Leave a Reply

Scroll to Top