The find command is one of the most widely used in Linux OS.

It would be just difficult to administer a Linux environment without knowing find commands.

It helps you to search files, folders on the Linux server, and extremely useful for file system housekeeping.

In this article, I have listed some of the most common find commands syntaxes to help you with productivity.

Find files on a specific file system

If you know the file name and file system but not sure the exact folder path, then you can use this syntax.

In the below example, I am searching for messages file in /var file system.

[[email protected] ~]# find /var -name messages
/var/log/messages
[[email protected] ~]#

Tips: if you don’t know the file system name, you can search on / level, but keep in mind it may take time if you have a large number of file systems.

[[email protected] ~]# find / -name messages
/var/log/messages
[[email protected] ~]#

If you don’t know the exact file name, you can also use a wildcard pattern to search.

Ex – to search error_log you may try

[[email protected] ~]# find / -name error_*
/var/log/httpd/error_log
[[email protected] ~]#

How about searching file name with a lower or upper case, in other words, ignoring case sensitive?

Well, you can use –iname instead of –name.

Ex:

[[email protected] var]# find / -iname MESSAGES
/var/log/messages
[[email protected] var]#

Let’s take a look at one more real-time scenario. If you know the file type and want to search all of them.

For ex – if you are working on WebSphere, you may want to search all files ending with .out then you can try

# find / -name *.out

Find files based on ownership and permissions

Having files with 777 permission is dangerous as anyone can edit or delete, so as a System Administrator, you may want to put a scan in place to find any files with 777 permissions.

For an ex – to show any files having 777 permission under /opt file system.

[[email protected] ~]# find /opt/ -type f -perm 777
/opt/testing
/opt/SystemOut.log
[[email protected] ~]#

Tips: how about printing file ownership, the time stamp in the same line command?

[[email protected] ~]# find /opt/ -type f -perm 777 -exec ls -ltr {} + ;
-rwxrwxrwx 1 root root 0 Jul 19 03:35 /opt/testing
-rwxrwxrwx 1 root root 0 Jul 19 03:36 /opt/SystemOut.log
[[email protected] ~]#

You may also change permission from 777 to 755 in a single find command syntax.

# find /opt/ -type f -perm 777 -exec chmod 755 {} + ;

Obviously, you can adjust permission from 755 to any other you may like.

How about finding files, which is owned by root or different user?

This is very helpful if you are having issues while starting the services due to the previous start was done by root.

For ex – if tomcat is owned by a user called “tomcatapp” and for some reason, you have started with root.

Guess what will happen when you restart next time with “tomcatapp”?

It won’t because some of the ownership of the file is changed to root, and now “tomcatapp” can’t modify/delete those files. So this becomes very handy in that situation.

Here is how you can search any file owned by root in a specific file system.

# find /opt/ -user root

Note: performing this find syntax on / level will results in so many files/folders, so you may want to control by doing this in a specific file system.

Find files older than particular days

File System housekeeping is essential for production support, and often you have to deal with this syntax to find logs that are older than (let’s say) 60 days.

The below example is to find access.log file older than 60 days in /opt file system.

# find /opt/ -name access.log -mtime +60

Tips: if you decide to find and delete in the same command line, you can do like below. This will find access.log older than 60 days in /opt file system and delete it.

# find /opt/ -name access.log -mtime +60 -exec rm {} + ;

While this is very handy, you may want to list the files before you delete them. To do so

# find /opt/ -name access.log -mtime +60 -exec ls -ltr {} + ;

Find large file size

Sometime you may have to deal with frequent file system cleanup due to a large number of logs are being written by the application due to a code issue, etc.

Let’s take an example of searching file greater than 1 GB in /opt file system.

# find /opt/ -size +1G

Tips: If you know all files in /opt/ with more than 1 GB can be deleted then you can just have find and delete in the same line.

# find /opt/ -size +1G -exec rm {} + ;

I hope above find commands are handy and help you in real-time.

To learn more about Linux commands, check out this online course.