find

Find Files and Directories in CentOS

The find command helps you locate files and folders on your hard disk. There are additional parameters that can be passed to the command that help you find files and folders in multiple ways. Find by filename, modified time, find files and folders.

Basic syntax

find /path/to/search -option1 -option2

Find a file by name

Search your entire system from the root folder "/" for a file named "filename.sh", which is presumably a shell script.

find / -name filename.sh

Find empty files

Lets find empty files in /tmp/

find /tmp -type f -empty

Find empty directories/folders

Lets find empty directories in /tmp/

find /tmp -type d -empty

Find files not modified for a set duration

Files under /var/log that haven't been written to or otherwise modified in a week or more.

find /var/log -mtime +7

Find files to delete

Now lets find files under /var/log which have not been modified for a week and delete them.

find /var/log -mtime +7 -exec rm -f {} \;
(Comments)

Comments