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 below example, I am searching for messages file in /var file system.

[root@Chandan ~]# find /var -name messages
/var/log/messages
[root@Chandan ~]#
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.

[root@Chandan ~]# find / -name messages
/var/log/messages
[root@Chandan ~]#
If you don’t know the exact file name, you can also use wildcard pattern to search.

Ex – to search error_log you may try

[root@Chandan ~]# find / -name error_*
/var/log/httpd/error_log
[root@Chandan ~]#
How about searching file name with lower or upper case in other word ignoring case sensitive?

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

Ex:

[root@Chandan var]# find / -iname MESSAGES
/var/log/messages
[root@Chandan 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.

[root@Chandan ~]# find /opt/ -type f -perm 777
/opt/testing
/opt/SystemOut.log
[root@Chandan ~]#
Tips: how about printing file ownership, the time stamp in same line command?

[root@Chandan ~]# 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
[root@Chandan ~]#
You may also change permission from 777 to 755 in 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 files ownership 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 specific file system.

# find /opt/ -user root
Note: performing this find syntax on / level will results so many files/folders so you may want to control by doing this in 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 which are older than (let’s say) 60 days.

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 {} ;