How to Use find Command
The find
command is a command-line tool used to locate files and directories in the Linux operating system. With this command, you can search for files by name, pattern, modification time, size, owner, and permission. It is available by default in all Linux operating systems.
If you are a system administrator, the find
command can be very useful in saving time during day-to-day operations. This tutorial will show you how to use the find
command to locate files and directories in Linux, with examples.
Basic Syntax
The basic syntax of the find command is shown below:
find [location] [option] [search term]
Below is a brief explanation of each option:
- name: Specifies the name of the file you want to find.
- type: Specifies the type of files and directories you want to find.
- perm: Specifies the permission of the file you want to find.
- user: Finds files that belong to a specific user.
- group: Finds files that belong to a specific group.
- mtime: Specifies the modification time of the file you want to find.
- atime: Specifies the access time of the file you want to find.
- size: Specifies the size of the file you want to find.
How to List All Files in the Current Directory
To list all files in your current directory, enter the following command:
find .
This command will list all files inside your current directory and its sub-directories.
List all Files in a Specific Directory
You can list all files in a specified directory by running the following command:
find /opt
Find Files by Name
To find a file by its name, use the following command:
find /etc -name apache2.conf
This command finds the file with the name apache2.conf in the /etc directory and returns the following output:
/etc/apache2/apache2.conf
If you don’t know the exact name of the file, you can use a pattern to find it:
find /etc -name apache*
This command finds all files that start with the letters “apache” in the /etc directory and returns the following output:
/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
Finding Files by File Extension
In some cases, it may be necessary to find all files with a specific extension. To do this, use the following syntax:
find /path -name "*.extension"
For example, to find all files with the extension “.conf” located inside the directory “/etc/apache2”, run the following command:
find /etc/apache2 -name "*.conf"
You should see all the files with the “.conf” extension 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 extensions using the OR operator with the “find” command. For example, to find all files with the extensions “.conf” and “.html” located inside the directory “/etc/apache2”, run the following command:
find /etc/apache2 -name "*.conf" -o -name "*.html"
This should output all files with both the “.conf” and “.html” extensions:
/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
Finding Only Files or Directories
The find
command allows you to find only files or directories using the -f
and -d
options, respectively.
For example, to find only files named nginx
located inside the /etc
directory, run the following command:
find /etc -type f -name "nginx"
You should see all files with the 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 directories named nginx
within the /etc
directory:
find /etc -type d -name "nginx"
You should see the following output:
/etc/nginx
Finding Files Belonging to a Specific User
You can use the -user
option to find all files belonging to a specific user.
For example, to find all files and directories inside the /var/www/html
directory that belong 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
Finding Files with Specific Permissions
You can use the -perm
option to find files with specific permissions.
For example, to find all files inside the /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
Finding Files with Specific Size
You can use the -size
option to find all files with a specific size.
For example, to find all files inside the /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
Finding All Symbolic Link Files
You can use the -l
option to find all symbolic link files.
For example, to find all symbolic link files located inside the /etc
directory, run the following command:
find /etc -type l
This command will find and list all symbolic link files located inside the /etc
directory.
Advanced Command Line Usage
You can find a file whether it’s uppercase or lowercase using the -iname
option.
For example, to find a file with the name MyFile.txt
and ignore the case, run the following command:
find /etc -iname MyFile.txt
If you want to find all files inside the /etc
directory and ignore .txt
files from the list, run the following command:
find /etc -not -name "*.txt"
This cannot list files which have the .txt
extension.
You can also use more than one condition to find files. For example, to find all files of .txt
, .conf
, .html
, and .php
located inside the /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 this guide, you learned how to use the find
command in Linux to find files and directories with practical examples. I hope this will help you perform your day-to-day tasks and save you a lot of time.
Small world. Big idea!
- Welcome to visit the knowledge base of SRE and DevOps!
- License under CC BY-NC 4.0
- No personal information is collected
- Made with Material for MkDocs and generative AI tools
- Copyright issue feedback me#imzye.com, replace # with @
- Get latest SRE news and discuss on Discord Channel