CentOS Debian Mint openSUSE Red Hat Ubuntu

How to Search Your Files or Directories With Find Command in Linux

Introduction

Everything is considered a file in the Linux system. Searching through these files is a simple task that every user has to do. Though there are multiple ways to do this task, searching with the Find command is the most efficient and convenient method to find your target file/directory.

In this tutorial, you will learn the Find command, its options, and uses that go beyond the basic syntax.

find [file/directory path] [options] [expression]

Prerequisites

Terminal Access.

Note: The commands in this tutorial are executed on the Linux Mint system. These instructions are valid for any Linux-based system.

Browse Files/Directories by Name

To search for a specific file by name with the ‘-name’ option, run the following command.

find /home -type f -name <filename>.txt

Similarly, You can find the directory by name using the following command.

find /home -type d -name <directory name>

Note: ‘-type’ option in the above commands specifies whether we are searching for a file or directory.

Find command search is case-sensitive by default. To search without case sensitivity, use the ‘-iname’ option.

find /home -type d -iname pictures

Browse Files/Directories by Extension

You can narrow down files using their extensions pattern with the following command.

find /home -type f -name "*.txt"

Browse Files/Directories by File Type and Size

To search for a specific type of file, use the ‘-type’ option in your Find command.

find /home -type d

You can limit the search of the files by providing a size limit with the ‘-size’ option. The ‘+’ and ‘-’ sign is the format used for “more than” and “less than” operators.

find /home -type f -size +5M

Browse Files/Directories Using Timestamps

You can search files by specifying modification, access, and creation time of the file with the ‘-mtime’, ‘-atime’, and ‘-ctime ’ options. Following command searches for files that have modified timestamps of the last two days.

find /home -type f -name "*.txt" -mtime -2

find /home -type f -name "*.txt" -atime +4

find /home -type f -name "*.txt" -ctime -2

Browse Files/Directories With Specific Permissions

To find files with specific permission, use the ‘-perm’ option. Following command finds files with read-only restriction.

find /home -type f -perm /u=r

Similarly, the following command uses the -perm option to find executable files.

find /home -type f -perm /a==x

Browse Files/Directories By User And Groups

To search files/directories by their user and user group, run the find command with ‘-user’ and ‘-group’ options.

find /home -type f -user mintlinuxways

find /home -type f -group mintlinuxways

Browse For Empty or Hidden Files/Directories

You can find all the empty files/directories in your system with the following commands.

find /home -type f -empty

find /home -type d -empty

Search and Delete Files/Directories

You can search and delete files simultaneously using the ‘-delete’ or ‘-exec rm’ options in your Find command.

find /home -type f -name "*.pdf" -delete

find /home -type f -name "*.pdf" -exec rm -f {} \;

Complex commands

Moreover, you can combine the above commands with ‘-and’, ‘-or’ and ‘-not’ options and get more functionality. For Example, the following command searches for a file with two conditions.

find /home -user mintlinuxways -and -size +5M

Conclusion

In this tutorial, you have explored the Find command in a detailed manner. You have learned how you can use the Find command to search and perform different operations efficiently.

Similar Posts