In this guide let us see some of the very useful real-time required find commands in our daily operations, Most of below examples are very useful for newbies to prepare them to be familiar with find commands. Open a terminal and run below commands manually without copy-paste to get familiar with it.

find command is just a single command but come with hundreds of options and arguments required to run during our requirement.

To list all files which named as test in current working directory. The “.” after the find command represent current directory you are in.

# find . -type f -name "*test*"

To list the directory with name as “audit” in current working directory. The “d” at the end of command represent the type as directory.

# find . -name audit -type d

To list only the files with .log extension. We are finding files inside the directory /var/log and looking only for “-f” files. By following executing the command “ls -lthr” to list all the files inside /var/log/.

# find /var/log/ -name "*.log" -type f -exec ls -lthr {} \;

To list all the .log extension files which modified older than 7 days.
The “-mtime” represent modified days before.

# find /var/log/ -name "*.log" -type f -mtime +7 -exec ls -lthr {} \;

To list only the files which last changed in n*24 hours ago. Changes done before number of days.

# find /var/log/ -name "*.log" -type f -ctime +7 -exec ls -lthr {} \;

To list only the files which last accessed n*24 hours ago. The file which accessed before two days back.

# find /var/log/ -name "*.log" -type f -atime +2 -exec ls -lthr {} \;

To list the files which are owned by mysql user.

# find /var/log/ -type f -user puppet -exec ls -lthr {} \;

To list the all files which are owned by mysql user and group.

# find / -type f -user puppet -group puppet -exec ls -lthr {} \;

To list the files below the levels of directories below the starting-points for user and group puppet.

# find / -maxdepth 4 -type f -user puppet -group puppet -exec ls -lthr {} \;

Check that no large files exist on the file system.

# find / -xdev -type f | cut -d '/' -f 2 | sort | uniq -c | sort -n

To list large file more than below size. You can replace 100000 with 10 Megabyte, MB or 1 G for 1 Gigabyte, GB.

# find . -xdev -size +1000000 -exec ls -lthr {} \;

To list only the deleted open files (Very Important one).

# find /proc/*/fd -ls | grep '(deleted)'

To list the file was last accessed 5 minutes ago

# find /var/log/ -amin +5 -type f

To list the file last changed 5 minutes ago

# find /var/log/ -cmin +5 -type f

To list the file last modified 5 minutes ago

# find /var/log/ -mmin +5 -type f

To list the empty directories

# find /var/log/ -empty -type d

To list the empty files

# find /var/log/ -empty -type f

Find and remove the empty files in current directory.

# find . -empty -type f -delete

Find and remove only the extension with .log which are empty.

# find /var/log/ -empty -type f -name *.log -delete

List files which are with executable permission

# find /tmp/ -executable -type f

List all files which owned by 1000 UID and GID under home directory.

# find /home/ -uid 1000 -gid 1000 -type f

To list only the files with executable permission.

# find /tmp/ -perm 700 -type f

To list all the block devices

# find /dev/ -type b

To list the files of users with user and group write access.

# find /home/ -perm /u+w,g+w

To list file and confirm before removing it.

# find /var/log/ -name *.log -type f -ok rm -rfv {} \;

We will update this topic with more examples soon.

Conclusion:

Every Linux admin should be familiar with find command and it’s options. In all case, during daily operations and troubleshooting this command is mostly very helpful to locate any files and configurations.

Babin Lonston
Overall 14+ Years of experience in IT field, 7+ years of experience in Linux administration with Virtualization & Cloud technologies. Love documentation and being Numismatics for a long time.

Creating ASM disks using UDEV in Linux servers

Previous article

Creating ASM disks using SAN disk through multipath in Oracle Database Linux Servers

Next article

You may also like

Comments

Leave a reply

Your email address will not be published.