Skip to content
geeksforgeeks
  • Tutorials
    • Python
    • Java
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
    • Practice Coding Problems
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Shell Scripting
  • Kali Linux
  • Ubuntu
  • Red Hat
  • CentOS
  • Docker in Linux
  • Kubernetes in Linux
  • Linux interview question
  • Python
  • R
  • Java
  • C
  • C++
  • JavaScript
  • DSA
Open In App
Next Article:
How to Exclude Certain Paths With the find Command in Linux
Next article icon

How to Exclude Certain Paths With the find Command in Linux

Last Updated : 01 Dec, 2022
Comments
Improve
Suggest changes
Like Article
Like
Report

The find command in Linux is a very useful command line utility that helps us in finding files and directories. In our daily life, there are many tasks in which we require files that may be located deep inside the system, finding them manually would be a tedious task. Thus, the find command comes in handy.

We can improve the performance of this command by excluding certain files and directories so that it doesn't look into them.

Ways to exclude paths with the find command:

Method 1: The -prune flag

Syntax:

find <path_of_the_file> -prune

For the sake of this article, we will be creating sample files and directories which will be used for demonstration purposes.

mkdir dir1 dir2 dir3  touch dir1/cat dir1/dog dir1/ant  touch dir2/apple dir2/cherry dir2/grape  touch dir3/car dir3/bus dir3/bike
How to Exclude Certain Paths With the find Command in Linux
 

Let us look at the tree of this directory system: (install the tree command by sudo apt install tree)

 

Now we will use the -prune option to exclude a certain path while searching:

find . -path ./dir3 -prune -o -print

This will enable the system to look for all the directories except the dir3 directory.

How to Exclude Certain Paths With the find Command in Linux
 

For simplicity we have put all three directories within a sample directory so that all the files in the system are not searched, otherwise, the list would be too long. The result from the output is clear that dir3 was excluded and all the remaining directories were printed.

Excluding multiple directories:

We can also exclude multiple paths if we want to :

find . \( -path ./dir1 -prune -o -path ./dir3 -prune \) -o -print
 

Both directories have been excluded from the search.

Method 2: The -not flag

The -not flag can also be used to exclude directories from the scope of search:

Syntax:

find . -type f -not -path '*/directory_name/*'

Let us exclude dir1 from the search scope:

find . -type f -not -path '*/dir1/*'
 

The not flag also excluded the dir1 directory, although its syntax is a bit complex it performs the same action.

Method 3: The "!" operator

The ! flag like the prune and not flags can be used to exclude the path:

Syntax:

find . -type f  !  -path '*/directory_name/*'

Let us exclude the dir2 directory now:

find . -type f  !  -path '*/dir2/*'
 

With the help of ! operator the second directory(dir2) was not searched.

So these were the three ways in which you can exclude certain paths from the scope of the find command.


Next Article
How to Exclude Certain Paths With the find Command in Linux

M

mukulojha
Improve
Article Tags :
  • Technical Scripter
  • Linux-Unix
  • Technical Scripter 2022

Similar Reads

    How to Find a File in Linux | Find Command
    The find command in Linux is used to search for files and directories based on name, type, size, date, or other conditions. It scans the specified directory and its sub directories to locate files matching the given criteria.find command uses are:Search based on modification time (e.g., files edited
    9 min read
    Difference between locate, which and find Command in Linux
    In Linux, there are multiple utilities available to locate files efficiently within a system. Three of the most commonly used commands for this purpose are:findlocatewhichEach command serves a similar function but differs in the way they search and the data they return. In this article, we'll explor
    5 min read
    locate command in Linux with Examples
    locate command in Linux is used to find the files by name. There are two most widely used file-searching utilities accessible to users called to find and locate. The locate utility works better and faster than the find command counterpart because instead of searching the file system when a file sear
    6 min read
    How to Get the Full Path of a File in Linux
    While dealing with files on Linux, especially shell scripts, one may require determining the full path of a file at times. Now, let's consider several methods of getting the full path of a file in Linux. In this article, we will be discussing several different solutions to a popular problem.Before w
    3 min read
    Linux - Installing locate Command to Find Files
    In this article, we will see how to install locate command to find files in Linux. locate is a command-line interface tool used for finding files by name in Linux systems. This works more efficiently than other commands. It uses one or more databases by updatedb. To check whether locate utility is a
    2 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences