SnapShooter Learning Center

Ultimate guide to linux find command

Learn how to use find in linux to find files and directories

Simon Bennett]
Simon Bennett
Last Updated: Jan 12, 2021
Table of Contents

The find command is a command-line tool used to find files and directories in Linux operating system. With the find command, you can find a file by name, pattern, modification time, size, owner and permission. By default, it is available in all Linux operating systems.

If you are a system administrator then the find command is very useful for you to save your lot of time in day to day operations. In this tutorial, we will show you how to use the find command to find files and directories in Linux with examples.


# Requirements

  • A system running Linux operating system.
  • A root password is setup in your system.

# Basic Syntax

The basic syntax of the find command is shown below:

find [location] [option] [search term]

A brief explanation of each option is shown below:

  • -name : Used to specify the name of the file you want to find.
  • -type : Used to specify the type of the files and directories you want to find.
  • -perm : Used to specify the permission of the file that you want to find.
  • -user : Used to find files that belong to the specific user.
  • -group : Used to find files that belong to the specific group.
  • -mtime : Used to specify the modification time of the file that you want to find.
  • -atime : Used to specify the access time of the file that you want to find.
  • -size : Used to specify the size of the file that you want to find.

# List all files in current directory

To list all files in your current directory, run the following command:

find .

This command will list all files inside your current directory and sub-directories.


# List all Files in Specific Directory

You can also list all files in the specified directory. Run the following command to list all files located inside /opt directory:

find /opt

# Find files using the filename

You can use the find command to find a file using the name of the file.

For example, find the file with name apache2.conf within the /etc directory, run the following command:

find /etc -name apache2.conf

You should see the following output:

/etc/apache2/apache2.conf

If you don't know the exact name of the file then you can use a pattern to find your file:

find /etc -name apache*

This command will find all files that start with the letter apache as shown below:

/etc/logrotate.d/apache2
/etc/php5/apache2
/etc/init.d/apache2
/etc/apache2
/etc/apache2/apache2.conf
/etc/ufw/applications.d/apache2
/etc/ufw/applications.d/apache2-utils.ufw.profile
/etc/default/apache2
/etc/apparmor.d/snap/abstractions/apache2-common
/etc/apparmor.d/abstractions/apache2-common
/etc/bash_completion.d/apache2
/etc/cron.daily/apache2

# Find all files using a file extension

In some cases, you may need to find all files with a specific extensions. In this case, you can use the following syntax to perform the operation.

find /path -name "*.extension"

For example, find all files with extension .conf located inside /etc/apache2 directory, run the following command:

find /etc/apache2 -name "*.conf"

You should see all files with extension .conf in the following output:

/etc/apache2/conf-enabled/localized-error-pages.conf
/etc/apache2/conf-enabled/security.conf
/etc/apache2/apache2.conf
/etc/apache2/mods-available/dav_fs.conf
/etc/apache2/mods-available/autoindex.conf
/etc/apache2/ports.conf
/etc/apache2/sites-available/000-default.conf
/etc/apache2/sites-available/default-ssl.conf

You can also specify multiple file extension using the OR operater with the find command. For example, find all files with extension .conf and .html located inside /etc/apache2 directory, run the following command:

find /etc/apache2 -name "*.conf" -o -name "*.html"

You should see all files in the following output:

/etc/apache2/conf-enabled/localized-error-pages.conf
/etc/apache2/conf-enabled/security.conf
/etc/apache2/apache2.conf
/etc/apache2/mods-available/dav_fs.conf
/etc/apache2/mods-available/autoindex.conf
/etc/apache2/ports.conf
/etc/apache2/sites-available/000-default.conf
/etc/apache2/sites-available/default-ssl.conf
/etc/apache2/index.html

# Find Only File or Directory

The find command also allows you to find only file or directory using the f and d option.

For example, run the following command to find only file named nginx located inside /etc directory:

find /etc -type f -name "nginx"

You should see all files with name nginx as shown below:

/etc/logrotate.d/nginx
/etc/init.d/nginx
/etc/ufw/applications.d/nginx
/etc/default/nginx

Next, run the following command to find only directory named nginx within the /etc directory:

find /etc -type d -name "nginx"

You should see the following output:

/etc/nginx

# Find files belongs to a specific user

You can use the option -user to find all files belongs to a specific user.

For example, find all files and directories inside /var/www/html directory that blongs to the user www-data, run the following command:

find /var/www/html -user www-data

You should get the following output:

/var/www/html
/var/www/html/index.html

# Find files with specific permissions

You can use the option -perm to find files with specific permissions.

For example, find all files inside /root directory with permissions 775, run the following command:

find /root -type f -perm 775

You should get the following output:

/root/file2.txt
/root/file3.txt
/root/file1.txt

# Find files with specific size

You can use the option -size to find all files with a specific size.

For example, find all files inside /etc directory which are 12k, run the following command:

find /etc -type f -size 12k

You should see all files which are 12kb:

/etc/openal/alsoft.conf
/etc/grub.d/10_linux
/etc/grub.d/30_os-prober
/etc/brltty/boxes.tti
/etc/brltty/da-lt.ttb
/etc/brltty/no-oub.ttb
/etc/brltty/en_GB.ttb
/etc/brltty/fr_CA.ttb
/etc/brltty/da.ttb
/etc/brltty/no-generic.ttb
/etc/brltty/hr.ttb
/etc/brltty/fr-cbifs.ttb
/etc/brltty/fr_FR.ttb
/etc/brltty/pl.ttb
/etc/java-7-openjdk/psfontj2d.properties
/etc/tkcvs/tkcvs_def.tcl
/etc/aliases.db
/etc/mono/2.0/web.config

If you want to find all files which are greater than 12kb, run the following command:

find /etc -type f -size +12k

To find all files which are less than 12kb, run the following command:

find /etc -type f -size -12k

You can use the option l to find all symbolic link files.

For example, find all symbolic links files located inside /etc directory, run the following command:

find /etc -type l

This command will find and list all symbolic links files located inside /etc directory.


# Find Advanced Command Line Usage

You can find a file whether its upper case or lower case using the -iname option.

For example, find a file with name MyFile.txt and ignore the case, run the following command:

find /etc -iname MyFile.txt

If you want to find all files inside /etc directory and ignore .txt files from the list, run the following command:

find /etc -not -name "*.txt"

This can not list files which are .txt extensions.

You can also use more than one condition to find files. For example, find all files of .txt, .conf, .html and .php located inside /etc directory, run the following command:

find /etc -regex ".*\.\(txt\|html\|conf\|php\)$"

To find all empty files, run the following command:

find /etc -type f -empty

To find all empty directories, run the following command:

find /etc -type d -empty

To find all files which are larger than 100MB and delete them, run the following command:

find / -type f -size +100M -exec rm -f {} \;

# Conclusion

In the above guide, you learned how to use the find command in Linux to find files and directories with practical examples. I hope this will helps you to perform your day to day tasks and save your lot of time.


Did you find this article helpful?