How to Find Files in Linux Using the Command Line

How to Find Files in Linux Using the Command Line

Introduction

The Find command is one of the most useful tools in Linux system administration collection. It is a powerful command that enables Linux administrators to recursively filter objects based on a straightforward conditional mechanism. Using Find from the command line makes it easy to locate files by extension, name, or a specific expression. The command applies your specific action and presents results of all files that match the action. Files can be located based on permissions, date, size, type, ownership, and more. This guide will show you how to locate files on a Linux system. Ready? Let’s get started!

Understanding The Find Command Syntax

Before we show how to use the Find command, you must understand its basic syntax. All Find Utility expressions are written in the form:

$ find [options] [path...] [expression]

Here;

  • Options dictate the treatment of the debugging options, symbolic links (symlinks), and optimization methods.
  • Path stipulates the directory(s) where the find command will get the files.
  • Finally, expression, contains options, actions, and search patterns separated by operators.

An example of a Find command featuring these attributes will look like:

$ find  -L /home/projects/ -name "*.js" -exec chmod 644 {} ;

In the command above -L  is the options written to allow the find command to follow symlinks. The attribute makes Find scrutinize the complete directory tree under /home/projects/. The /home/projects/ is the path in the command, it directs Find to search files ending the expression .js. The find command is also instructed to change the permission for the mentioned files to 644.

Finding Files By Type

If you want to search a file by type, you will use the parameter -type followed by the description of the file type. Some of the most common descriptors used to specify a file type include:

  • b: block devices
  • c: character devices
  • f: a regular file
  • l: symbolic link
  • p: named pipe (FIFO)
  • s: socket

For example, to find directories in a specific location, simply execute the command:

$ find . -type d

Alternatively, to find all the character devices in a Linux system, run the command:

$ find / -type c

To change the permissions of all files to 644 and permissions for all directories to 755, execute the commands:

$ find /var/www/my_project -type f -exec chmod 0644 {} ;
$ find /var/www/my_project -type d -exec chmod 0755 {} ;

Finding File By Names

This one of the most popular Linux Find command. This command entails the use of a -name option together with the specific file name. For example, to find a file titled document.pdf in the location /home/hostadvice, you simply execute the command:

$ sudo find /home/hostadvice -type f -name document.pdf

The option -name can be changed to -iname when doing a case-insensitive search. For instance, the above command can be executed as:

$ sudo find /home/hostadvice -type f -iname document.pdf

This will find the document which matches the name DOCUMENT.pdf, Document.pdf, etc

Finding A File By Extension

This is similar to finding a file by name only that the name is replaced with an expression. To find all .log.gz files in the directory /var/log/nginx, execute the command;

$ find /var/log/nginx -type f -name '*.log.gz'

On the other hand, when searching for files that do not match the expression *.log.gz, add a -not parameter just before the -name option.

$ find /var/log/nginx -type f -not -name '*.log.gz'

Note: when using the wildcard character you must escape the asterisk symbol using a backlash or quote the expression. This prevents the Linux shell from interpreting the wildcard character.

Finding A File By Size

Finding a file by its size is straightforward. Simply pass the parameter; -size, together with the size format. Some of the most popular size suffixes include;

  • b: 512-byte blocks (default)
  • c: bytes
  • G: Gigabytes
  • M: Megabytes
  • k: Kilobytes
  • w: two-byte words

For example, to find 1024 bytes file in the ‘/tmp’ directory, execute the command below:

$ find /tmp -type f -size 1024c

The command above will match all the files which are less or greater than 1024 bytes. To find files which are greater than 1024 bytes use + just before the file size. On the other hand, to find files which are less than 1024 bytes use a minus sign before the files size.  For instance, to find files which are less than 1M in your current working directory execute the command:

$ find . -type f -size -1M

The size command allows you to find files that are within a range. For example, to find files which are between 2 and 3 MB, execute the command:

$ find . -type f -size +2M -size3M

Finding A File By Its Modification Date

In addition, the find command allows you to find files based on their last access, modification, or change time. Same as the previous case, we can utilize the minus and plus signs to indicate less than or greater than. For example, if you made changes to a devcot config file in that last five days and can’t remember the specific file name. You can search all files within the directory /etc/dovecot/conf.d/ that was modified during this time. Search for a file ending with .conf, using the command below:

$ find /etc/dovecot/conf.d -name "*.conf" -mtime 5

Better still, you can use the -daystart option to filter files based on the day they were modified. To find all the files in the directory /home, that were modified a month ago or longer, run the command:

 $ find /home -mtime +30 -daystart

Finding A File By Permissions

To find a file by permission we use the parameter -perm. For example, to find files with 775 permission inside the directory /var/www/html.

$ find/var/www/html -perm 644

The numeric mode in the command above can be prefixed with a slash or minus symbol. When prefixed with a slash then one group, user, or other categories must possess at least the bits stipulated for that file to match. For instance:

$ find . -perm /444

This command will deliver results of files that feature read permissions for the group, user, or other categories. When a minus sign is used to prefix the numeric value, the stipulated bits must be specified for any file to match. For instance, the following command, will find files that feature read and write privileges for the corresponding owner, but the other users and the group have read permissions only:

$ find . -perm -664

Finding A File by Its Owner

Here you can use the -group and -user to find files owned by a group or user respectively. For example, to search for directories and files owned by a user called hostadvice, execute the command below:

$ find / -user hostadvice

Alternatively, to search for files owned by www-data and modify the ownership of these files to nginx, execute the command:

$ find / -user www-data -type f  -exec chown nginx {} ;

Finding And Deleting A File

Deleting a file is as easy as adding a parameter as the suffix of the file match expression. It’s always wise to ensure that the results are for the files you want to trash. For instance, to delete files that end with .temp in the directory /var/log/, execute the command below:

$ find /var/log/ -name `*.temp` -delete

Conclusion

That’s it! We’ve highlighted the commonly used Linux find commands based on a number of criteria. You can now use the command to find any a file in your Linux system.

Check out these top 3 Linux hosting services

FastComet
$1.79 /mo
Starting price
Visit FastComet
Rating based on expert review
  • User Friendly
    4.7
  • Support
    5.0
  • Features
    4.8
  • Reliability
    4.5
  • Pricing
    5.0
HostArmada
$2.49 /mo
Starting price
Visit HostArmada
Rating based on expert review
  • User Friendly
    4.5
  • Support
    4.5
  • Features
    4.5
  • Reliability
    4.5
  • Pricing
    4.0
Hostinger
$2.99 /mo
Starting price
Visit Hostinger
Rating based on expert review
  • User Friendly
    4.7
  • Support
    4.7
  • Features
    4.8
  • Reliability
    4.8
  • Pricing
    4.7
  • Want to get top recommendations about best hosting? Just click this link!

How To Files And Folders Using the Command Line In Linux Systems

This guide will help you find files and folders in your Linux system via the com
4 min read
David Malcom
David Malcom
Author

How To Set up a VSFTPD Server on a CentOS 7 VPS or Dedicated Server

Brief Description FTP is usually insecure exposing clear-text passwords, userna
2 min read
Avi Ilinsky
Avi Ilinsky
Hosting Expert

How To Set up a VSFTPD Server on an Ubuntu 16.04 VPS or Dedicated Server

Brief Description FTP data is usually insecure since information (usernames, pa
2 min read
Eliran Ouzan
Eliran Ouzan
Web Designer & Hosting Expert

How To Recover a Forgotten or Lost Root Password on a Linux Server

In this tutorial, we will reset the root password of a Linux web server by booti
2 min read
Avi Ilinsky
Avi Ilinsky
Hosting Expert
HostAdvice.com provides professional web hosting reviews fully independent of any other entity. Our reviews are unbiased, honest, and apply the same evaluation standards to all those reviewed. While monetary compensation is received from a few of the companies listed on this site, compensation of services and products have no influence on the direction or conclusions of our reviews. Nor does the compensation influence our rankings for certain host companies. This compensation covers account purchasing costs, testing costs and royalties paid to reviewers.
Click to go to the top of the page
Go To Top