find कमांड की Basics
In this page:
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
#!/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
Login to try C/C++/Java/PHP code in the editor
Type से Filter करना
-type f सिर्फ regular files को match करता है और -type d सिर्फ directories को match करता है, आपको एक search narrow करने देते हुए जब सिर्फ names specific न हों।
उदाहरण: Filtering by Type
#!/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
Login to try C/C++/Java/PHP code in the editor
हर 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
#!/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
Login to try C/C++/Java/PHP code in the editor
Search Depth सीमित करना
-maxdepth N यह सीमित करता है कि find कितने directory levels में descend करे, जो useful है जब आपको सिर्फ current directory का immediate content चाहिए एक full recursive search के बजाय।
उदाहरण: Limiting Search Depth
#!/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
Login to try C/C++/Java/PHP code in the editor
- यह भूल जाना कि
finddefault रूप से subdirectories में recursively search करता है, जो large trees पर expected से slower या broader हो सकता है। -namepattern को quote न करना, shell को इसे glob के रूप में expand करने देना इससे पहले किfindइसे देखे भी।find ... -exec cmd {} \;यह realize किए बिना उपयोग करना कि यहcmdको per file एक बार चलाता है, जो large result sets के लिए-exec cmd {} +से slower है।
find dir -name patternquoted glob pattern से match करने वाले names के लिए recursively search करता है।-type fresults को regular files तक restrict करता है,-type ddirectories तक।find ... -exec cmd {} \;हर matched file के लिए एक बार command चलाता है;{} +matches को कम invocations में batch करता है।- हमेशा
-namepatterns को quote करें ताकि shellfindचलने से पहले उन्हें expand न करे।
Chapter Quiz — Complete all 13 topics to unlock
0/13 topics done
Complete these topics first: