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:
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:
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.
find . -type d
Similarly, to find all regular files, you can use the following command:
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:
find . -size -<file-size-MB>
Similarly, if you want to search for the files whose size is more than 1 MB then use the below given code:
find . -size +<file-size-MB>
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:
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:
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.