BASH Programming

How to Use Find Command in Bash Script

Bash is a popular Unix shell, which is widely used for automating repetitive tasks and performing various operations. One of the most useful features of the bash shell is the find command, the find command allows you to search for files or directories that meet specific criteria, such as name, size, or modification time. This article will discuss some of the ways in which you can use the find command in a bash script.

1: How to Find Files Based on Name – Bash

The most common use of the find command is to locate files based on their name, you can use the -name option to specify the filename you are looking for. For instance, the following command can be used to find all files in the current directory with the phrase “bashfile” in its name:

#!/bin/bash

find . -name "*<file-name>*"

The “*” is used as a wildcard character that matches any character, here I have searched for the files named “bashfile” in the current directory:

Text Description automatically generated

2: How to Find Files Based on Type – Bash

Another way to find files using the find command is by Using the -type argument to search for files of a specified kind, for example, to locate all folders in the current directory.

#!bin/bash

find . -type d

Similarly, to find all regular files, you can use the following command:

#!bin/bash

find . -type f

3: How to Find Files Based on Size – Bash

To find files based on their size you can use the -size option, for instance, to get all files in the current directory that have a size less than 1MB, you can use the following script:

#!bin/bash

find . -size -<file-size-MB>

Text Description automatically generated

Similarly, if you want to search for the files whose size is more than 1 MB then use the below given code:

#!bin/bash

find . -size +<file-size-MB>

Text Description automatically generated

4: How to Find Files Based on Modification Time – Bash

Another way to use find command is by finding the files based on their modification time using the -mtime option. For illustration I have search for the file that were modified within last two days and is the shell script that I have used:

#!/bin/bash

find . -mtime -2

The “-2” specifies that the files should have been modified within the last 2 days:

5: How to Find Files Based on Ownership – Bash

You can use the -user option to find files based on their owner, like to find all files in the current directory that are owned by the user, you can use the following command:

#!/bin/bash

find . -user <user-name>

Once you have located the files you are looking for, you may want to perform some actions on them, such as deleting them or copying them to another location:

Conclusion

The find command is a powerful tool that can help you search for files based on various criteria. By combining different options, you can create complex search patterns that will help you locate the files you need. Files can be located using the find command depending on their name, type, users’ group, size, and the date they were updated.

About the author

Aaliyan Javaid

I am an electrical engineer and a technical blogger. My keen interest in embedded systems has led me to write and share my knowledge about them.