← Back to Bash Course | Chapter 9: File Testing & Filesystem | Lesson 12 of 13

find कमांड की Basics

find command directories के through search करता है आपके दिए criteria से match करने वाली files और folders locate करने के लिए, जैसे name या type।
Syntax
bash
find directory -name "pattern"
find directory -type f
find directory -name "pattern" -exec command {} \;

Name से Files ढूंढना

find dir -name "pattern" dir को recursively उन entries के लिए search करता है जिनका name quoted glob pattern से match करे। Quoting shell को pattern को खुद expand करने से रोकता है इससे पहले कि find चले।

उदाहरण: Finding Files by Name

bash
#!/bin/bash
mkdir -p search_demo/sub
touch search_demo/a.txt search_demo/sub/b.txt search_demo/c.log
find search_demo -name "*.txt"
rm -rf search_demo

Type से Filter करना

-type f सिर्फ regular files को match करता है और -type d सिर्फ directories को match करता है, आपको एक search narrow करने देते हुए जब सिर्फ names specific न हों।

उदाहरण: Filtering by Type

bash
#!/bin/bash
mkdir -p type_demo/inner
touch type_demo/file.txt
find type_demo -type d
echo "---"
find type_demo -type f
rm -rf type_demo

हर Match पर एक Command चलाना

-exec command {} \; हर matched file के लिए एक बार command चलाता है, {} को file के path से substitute करते हुए। \; के बजाय + से end करना ज़्यादा से ज़्यादा matches को कम commands invocations में batch करता है, जो large result sets के लिए faster है।

उदाहरण: Running a Command on Each Match

bash
#!/bin/bash
mkdir -p exec_demo
touch exec_demo/one.txt exec_demo/two.txt
find exec_demo -type f -name "*.txt" -exec echo "found: {}" \;
rm -rf exec_demo

Search Depth सीमित करना

-maxdepth N यह सीमित करता है कि find कितने directory levels में descend करे, जो useful है जब आपको सिर्फ current directory का immediate content चाहिए एक full recursive search के बजाय।

उदाहरण: Limiting Search Depth

bash
#!/bin/bash
mkdir -p depth_demo/nested
touch depth_demo/top.txt depth_demo/nested/deep.txt
echo "maxdepth 1:"
find depth_demo -maxdepth 1 -type f
echo "no maxdepth limit:"
find depth_demo -type f
rm -rf depth_demo
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. यह भूल जाना कि find default रूप से subdirectories में recursively search करता है, जो large trees पर expected से slower या broader हो सकता है।
  2. -name pattern को quote न करना, shell को इसे glob के रूप में expand करने देना इससे पहले कि find इसे देखे भी।
  3. find ... -exec cmd {} \; यह realize किए बिना उपयोग करना कि यह cmd को per file एक बार चलाता है, जो large result sets के लिए -exec cmd {} + से slower है।
चैप्टर सारांश
  • find dir -name pattern quoted glob pattern से match करने वाले names के लिए recursively search करता है।
  • -type f results को regular files तक restrict करता है, -type d directories तक।
  • find ... -exec cmd {} \; हर matched file के लिए एक बार command चलाता है; {} + matches को कम invocations में batch करता है।
  • हमेशा -name patterns को quote करें ताकि shell find चलने से पहले उन्हें expand न करे।

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.