How to find files and directories in Linux?

How to find files and directories in Linux?

Find and diff command in Linux

Table of contents

Hello Friends,

Today I am going to write a short blog on find and diff command which is very important for DevOps engineers.

Find Command

The find command is used to search files or directories in Linux. We can use the find command in various conditions like finding a file by

  • Permission

  • Size

  • Users

  • Groups etc.,

Syntax: find / <options> <filename>

OptionsUsage
-nameTo search a file with its name
-userTo search a file who is the owner or user of a file
-groupTo search for a file belonging to a particular group

/ => It means it will search in all directories.

. => It means it will search in current directories

Now we will see the find command with some use cases.

  1. How to search a file1 in the home directory?

     [root@ip-172-31-89-38 ~]# find ~ -name file1.txt
     /root/file1.txt
    
  2. How to search a file1 in the current directory?

     [root@ip-172-31-89-38 ~]# find . -name file1.txt
     ./file1.txt
    
  3. How to search all files whose permission is 777?

     [root@ip-172-31-89-38 ~]# find -type f -perm 777
     ./file3.txt
    

  4. How to search all empty files?

     [root@ip-172-31-89-38 ~]# find . -type f -empty
    

  5. How to display the files which are older than 30 days?

    mtime ==> it means modified time.

    Display 30 days old files in the current working directory.

    $find. -mtime +30 print

    Display 30 days old files in a specific directory.

    $find <directory path> -mtime +30 print

  6. How to delete all the files which are older than 30 days?

    $find <directory path> -mtime +30 delete

diff command

diff command is used to see the difference between two files.

Syntax: diff <file1> <file2>

I hope this blog would be informative.

Thank You for reading this blog

Parimal Pradhan

You can follow me on LinkedIn for my daily updates:- linkedin.com/in/parimal-pradhan-b62021168

Did you find this article valuable?

Support Parimal Pradhan by becoming a sponsor. Any amount is appreciated!