10 Linux Find Command Examples

by lifeLinux on September 8, 2011

The Linux find command is very powerful. It can search the entire filesystem to find files or directories according to the search criteria that you specify. In this article, I’ll show 10 examples of Linux find command that will be very useful to both newbies and experts.

1. Find files using name

# find /etc -name passwd

Sample ouputs

/etc/cron.daily/passwd
/etc/passwd
/etc/pam.d/passwd

2. Find files using name and ignoring case

# find /etc -iname Passwd

Sample ouputs

/etc/cron.daily/passwd
/etc/passwd
/etc/pam.d/passwd

3. Find files based on file type

Find only the socket files

# find . -type s

Find all directories

# find . -type d

Find only the normal files

# find . -type f

4. Find files by modification time

# find . -mtime -7

Sample outputs

./.dbus/session-bus/c08a8197eb71b09ea24402430000000a-0
./.macromedia/Flash_Player/macromedia.com/support/flashplayer/sys
...

5. Find Files by Size

Find files bigger than 100MB

# find /var -size +100M

Sample outputs

/var/lib/mysql/ibdata1
...

Find files smaller than 100MB

# find /var -size -100M

Sample outputs

/var/cache/debconf/templates.dat-old
/var/cache/debconf/passwords.dat
/var/cache/debconf/templates.dat
/var/cache/debconf/config.dat
...

6. Find files using permission

# find /var -perm 777

Sample outputs

/var/spool/mail
/var/run/mysqld/mysqld.sock
/var/run/avahi-daemon/socket
/var/run/dbus/system_bus_socket
...

7. Find and delete

Clean PHP files

# find . -iname "*.php" -exec rm -rf {} \;

Or

# find . -iname "*.php" -print | xargs rm –f

8. Find all empty files

# find /var -empty

Sample outputs

/var/opt
/var/cache/pm-utils
/var/cache/pppconfig
/var/cache/jetty
/var/cache/apt/archives/lock
...

9. Finding files only in current directory not finding on sub directories

# find /etc -maxdepth 1 -name passwd

Sample outputs

/etc/passwd

10. Find files with different file extensions

# find / -type f \( -name "*.php" -o -name "*.php5" \)

Sample outputs

/var/cache/dictionaries-common/sqspell.php
/var/cache/dictionaries-common/sqspell.php5
..

Previous post:

Next post: