• Home
  • Linux
  • Ubuntu
  • Debian
  • CentOS
  • Linux Commands
  • About Us
  • Donate
TecNStuff
Menu
  • Home
  • Linux
  • Ubuntu
  • Debian
  • CentOS
  • Linux Commands
  • About Us
  • Donate

How to Find Files in Linux Using the Command Line

Written by Admin, Updated On December 10, 2022
find, terminal
How to Find Files in Linux Using the Command Line

The find command is a useful and frequently used command line utility for Linux administrators. Find command is used to search and locate the list of files and directories based on a user given conditions and match the arguments.

It is also possible to search files and directories based on their type, date, ownership, size, and more. It can also be used in combination with other tools like grep and more.

Linux find Command Syntax#

Following is the common syntax of find command:

find [options] [path...] [expression]
  • options – Treat the symbolic links, debugging options, and optimization method.
  • path… It defines the directories where find will search the files.
  • expression – search patterns, and actions separated by operators.

1. Find Files by Name#

It’s a common requirement to find files by name. To find the file by its name, use the -name option followed by the name of the file you want to search.

For instance, to search file name with index.html in the /var/www directory you would use the following command:

find /var/www -type f -name index.html

To find the file by name and ignoring case, change the -name option with -iname:

find /var/www -type f -iname index.html

2. Find Files by Name in Current Directory#

Find all the files whose name is data.txt in a current working directory.

find . -name data.txt

3. Find Files by Extension#

To find files by extension is the same as search file by name. For instance, to find all files ending with .pdf use:

find /home/tecnstuff -type f -name '*.pdf'

You can also search for reverse expression by adding -not option. For example, to find all files that don’t ending with *.php you can use:

find /home/tecnstuff -type f -not -name '*.pdf'

4. Find Files by Type#

You can search for specific file types like symlinks, directories or normal files. Use -type option to search file by its type. Following are the descriptors to specify the file type:

  • f: a regular file
  • d: directory
  • l: symbolic link
  • c: character devices
  • b: block devices
  • p: named pipe
  • s: socket

For example, to change the file permissions to 644 and directory permissions to 755 using the chmod command, you would type:

find /var/www/example.com -type d -exec chmod 0755 {} \;
find /var/www/example.com -type f -exec chmod 0644 {} \;

Above commands will change the permissions for all files and directories to 644 and 755, respectively.

5. Find Files by Size#

Sometimes you might need to find files by size, pass -size parameter along with the size criteria. Below are the suffixes to specify the file size:

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

Below command will find all files of exactly 2M inside the /home/data directory:

find /home/data -type f -size 2M

In the following example, we are searching for all files which size between 2M and 5M inside current working directory.

find . -type f -size +2M -size -5M

If you want to search file with size less than 2MB, then you need to use the minus - symbol before the size value:

find . -type f -size -2M

For search file with size greater than 5MB, you need to add plus + symbol before size value:

find . -type f -size +5M

6. Find Files by Modification Date#

It is very useful and most common to find files by their last modification, access, or change time. To find .php files which have been modified in the last ten days:

find /var/www/html -name "*.php" -mtime 10

Another example, to get list of files which were modified before 15 or more days ago. You would use the -daystart option:

find /var/www/html -mtime +15 -daystart

7. Find Files by Permissions#

The -perm option is used to search files based on the file permissions. For example, to find files with permissions of 644 inside the /var/www/html directory, you would use:

find /var/www/html -type f -perm 644

To find files which have not 644 permissions, you have to add ! symbol as following:

find /var/www/html -type f ! -perm 644

The above command will list the files which don’t have 644 permissions.

8. Find Files by Owner#

Find files owned by a particular user or group, you would use -user and -group options.

For instance, to search all files and directories owned by user tecnstuff, type:

find / -user tecnstuff

9. Find Specific Files and Delete#

To delete the matched files, you can add -delete option at the end of the match expression. Make sure your are confident that matched result no more needed.

For example to delete all files ending with .tmp from the /var/www/html/temp you would use:

find /var/www/html/temp/ -name '*.tmp' -delete

10. Find Files by Size and Delete#

Find all .log files with more than 5MB and delete them using one single command.

find / -type f -name *.log -size +5M -exec rm {} \;

Conclusion#

This tutorial shown you how to find command with different options and expressions. To learn more about other useful options of find command visit find man page.

If you have any question or feedback, please leave a comment below.

If our content helps you, please consider buying us a coffee

Thank you for your support.

Share On
Share on Facebook
Share on Twitter
Share on Reddit
Share on Tumblr
 Previous Article How to Add Apt Repository In Ubuntu / Debian
Next Article   How to Add Directory to PATH in Linux

Related Posts

  • How to Install SSH Keys on Ubuntu 22.04

    How to Set up SSH Keys on Ubuntu 22.04

    January 7, 2023
  • How to Install Fail2ban on Ubuntu 22.04

    How to Install and Configure Fail2ban on Ubuntu 22.04

    December 5, 2022
  • How to Enable SSH on Ubuntu 22.04

    How to Enable SSH on Ubuntu 22.04

    December 1, 2022

Leave a Reply Cancel reply

DigitalOcean Referral Badge

Popular Posts

  • How to Install SSH Keys on Ubuntu 22.04
    How to Set up SSH Keys on Ubuntu 22.04 January 7, 2023
  • How to Install Mongodb on Debian 11
    How to Install MongoDB on Debian 11 Linux January 11, 2023
  • How to Install Puppet Agent on Ubuntu 22.04
    How to Install Puppet Agent on Ubuntu 22.04 January 22, 2023
  • How to Install Jenkins on Debian 11
    How to Install Jenkins on Debian 11 January 5, 2023
  • How to Change-Hostname Ubuntu 22.04
    How to Change Hostname on Ubuntu 22.04 January 19, 2023
© 2020 TecNStuff All rights reserved. This website is using and storing cookies on your browser. By using this website you agree our Privacy Policy.  Follow us -  Twitter | Facebook