Find Command examples

Find is one of the most important and frequently used utilities in Unix-like operating systems. It is used to search the files on the Linux system. You can find the file according to size, date, name, etc. Let’s explore some find Command examples

Find Command examples
This image has nothing to do with Find command examples, but it does represent a Map that has something to do with the search.

1. Find the file with the name.

If you want to search for the file using the name. It is useful when you know the exact name of the file.

$ find -name justgeek.txt

2. Find the file with a wildcard search

Find all files whose name starts with justgeek. When you don’t know the name of the file, but you just know the part of it then you can use a wildcard in this case.

$ find -name justgeek.*

3. Ignore the case while finding the file

The file names you provide are case sensitive, which means if your file name is Justgeek and if you search for justgeek then you won’t get any result. In this case, you will need to pass iname instead of name in the command.

$ find -iname justgeek

4. Find directories

If you want to search directories then you will have to use -d. Here we are searching for the directory whose name is justgeek

$ find -type d -iname justgeek

5. FInd file in a particular location

If you want to search for the file in a specific location, then you can mention that in the command. Below command will look for the directory named in /home.

$ find /home -type d -name justgeek

6. Find with an extension.

If you want to find file ending .html in /home folder

$ find /home -type f -iname "*.html"

7. Find and delete

If you want to find the files ending with .tmp and delete them, you can use the following command.

$ find /home -type f "*.tmp" -exec rm -fv {} \;

The above command will find all files ending with .tmp in /home folder and delete them.

8. Find Empty files

$ find /tmp -type f -empty

9. Find empty directories

Same as the empty files you will be able to find empty directories as well, instead of f you will need to pass d

$ find /tmp -type d -empty

10. Find the file with a size.

Find all files whose size is more than 500 MB

$ find /home/justgeek -type f -size +500M

11. Find and delete using size.

If you want to find the files with more than 100 MB and delete them.

$ find / -type f -name message.logs -size +100M -exec rm -fv {} \;
12. Find and delete old files
$ find /tmp/logs -type f -mmin +1440 -exec rm -fv {} \;

13. Find and move the files.

If you want to find the files which are 60 min old and move them.

$ find /tmp/logs*tar -mmin +60 -exec mv {} /home/backup \;

14. Find files with user

Find all the files, owned by the user justgeek. Similarly, you can find the files owned by any user.

$ find /home -user justgeek

15. Find files with permissions.

To find the files with 777 permission.

$ find . -type f -perm 0777 -print

16. Find files without permission.

You can also find the files whose permission isn’t 777.

$ find / -type f ! -perm 777

These were some normal find Command examples that you are definitely going to use, I will update this post with more examples in a few days. Meanwhile, you can check Linux commands Archives which have more examples of other commands.

Leave a Comment