find Command in Linux

Learn via video courses
Topics Covered

Overview

The find command in Linux is a powerful tool that assists users in locating files or directories on their system. It allows for a wide range of search criteria and can be used in scripts for complex file management tasks. With the find command in Linux, you can search by name, type, size, and even by the time files were last accessed, modified, or changed.

Transform Your Career

Choose from our industry-leading programs designed for career success

NSDC Certified

Modern Software and AI Engineering Program

Master full-stack development with AI integration

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

Modern Data Science and ML with specialisation in AI

Advanced data science techniques with AI specialization

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

Advanced AIML with Specialisation in Agentic AI

Deep dive into AIML with focus on Agentic systems

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

DevOps, Cloud & AI Platform Engineering

Build and manage AI-powered cloud infrastructure

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

AI Engineering Advanced Certification by IIT-Roorkee

Premier AI engineering certification from IIT-Roorkee

3 MonthsDuration
AI-LedCurriculum
Career SupportSupport
Program highlights
Go to Program

Syntax of find Command in Linux

The basic syntax of the find command in Linux is as follows:

Where:

  • where: This is the location where the find command starts its search. It could be a directory, such as /home/username/, or it could be '.', which represents the current directory.
  • what: These are the search criteria. It can be the name, type, size, or time of the file or directory.

Options in find Command in Linux

  1. -name: Search for a file by its name.

    For example -

    Output:

    This command searches for a file named 'example.txt' in the current directory and its subdirectories.

  2. -type: Search for a file by its type.

    For example -

    Output:

    This command lists all directories in the current directory and its subdirectories.

  3. -size: Search for a file by its size.

    For example -

    Output:

    This command searches for files larger than 1 Megabyte in the current directory and its subdirectories.

Example Usages

  • Find files modified in the last day:

    Output:

    Explanation: This command lists all files that were modified within the last day in the current directory and its subdirectories.

  • Find and delete files with a specific extension:

    Explanation: This command deletes all files with the .tmp extension in the current directory and its subdirectories.

Tips

  • Always be careful when using the -delete option, as it does not ask for confirmation before deleting files.

  • Using -iname instead of -name makes the search case insensitive.

Advanced Use Cases of find Command in Linux

  • Find and compress all .jpg files:

    Explanation: This command finds all .jpg files in the current directory and its subdirectories and adds them to a tar archive named images.tar.

  • Find files not accessed in the last 10 days:

    Output:

    Explanation: This command lists all files that have not been accessed in the last 10 days in the current directory and its subdirectories.

  • Find and change file permissions:

    Explanation: This command finds all files in the current directory and its subdirectories and changes their permissions to 644 (read and write for the owner, and read for the group and others).

Conclusion

  • The find command in Linux is a versatile and powerful tool for locating files and directories based on various criteria.

  • Be careful when using options that can modify or delete files, such as -delete or -exec.

  • The find command in Linux can be combined with other commands using the -exec option for more complex file management tasks.

Hiring Partners:
GoogleGoogleAmazonAmazonMicrosoftMicrosoftFlipkartFlipkartAdobeAdobe1200+ more