Directory के Operations
In this page:
mkdir -p directory/path
cd directory || exit 1
pushd directory
popd
Directories को Safely बनाना
mkdir -p एक call में कोई भी missing parent directories बनाता है और fail नहीं होता अगर target पहले से मौजूद हो, इसे scripts के लिए safe default बनाते हुए।
उदाहरण: Creating Directories Safely
#!/bin/bash
mkdir -p project/src/utils
if [[ -d project/src/utils ]]; then
echo "nested directories created"
fi
rm -rf project
Login to try C/C++/Java/PHP code in the editor
Directories को Safely बदलना
cd fail हो सकता है अगर target मौजूद न हो या accessible न हो। इसे || exit 1 (या एक function के अंदर || return 1) के साथ chain करना script को गलत जगह operate करना जारी रखने से रोकता है।
उदाहरण: Changing Directories Safely
#!/bin/bash
mkdir -p work_area
cd work_area || exit 1
echo "Now in: $(pwd | xargs basename)"
cd ..
rmdir work_area
Login to try C/C++/Java/PHP code in the editor
pushd और popd उपयोग करना
pushd dir directory change करता है जबकि आप कहां से आए एक stack पर याद रखता है, और popd उस remembered location पर वापस जाता है। यह उन scripts के लिए convenient है जिन्हें temporarily कहीं और काम करके वापस आना हो।
pushd/popd output को /dev/null में redirect करें क्योंकि उनका default directory-stack printout वहां शायद ही कभी useful होता है।उदाहरण: Using pushd and popd
#!/bin/bash
mkdir -p temp_place
pushd temp_place > /dev/null
echo "working here: $(pwd | xargs basename)"
popd > /dev/null
echo "back to: $(pwd | xargs basename)"
rmdir temp_place
Login to try C/C++/Java/PHP code in the editor
एक Script में Directory Contents List करना
जबकि ls एक human के glance के लिए fine है, scripts को generally glob patterns या find को enumerate files के लिए prefer करना चाहिए, क्योंकि unusual filenames के साथ ls output ambiguous हो सकता है। Plain ls अब भी एक user को listing display करने के लिए fine है।
ls output parse न करें (जैसे for f in $(ls)); इसके बजाय globs (for f in *) या find उपयोग करें।उदाहरण: Listing Directory Contents in a Script
#!/bin/bash
mkdir -p listing_demo
touch listing_demo/a.txt listing_demo/b.txt
ls listing_demo
rm -rf listing_demo
Login to try C/C++/Java/PHP code in the editor
- यह मान लेना कि
cdहमेशा succeed होता है; scripts को इसका exit status जांचना चाहिए या गलत directory में silently continue करने से बचने के लिएcd dir || exit 1उपयोग करना चाहिए। - यह भूल जाना कि nested directories बनाने या directory पहले से मौजूद होने पर error से बचने के लिए
mkdir -pचाहिए। - यह realize न करना कि
pushd/popdएक stack maintain करते हैं, इसलिए उन्हें गलत तरीके से nest करना आपको एक unexpected directory में छोड़ सकता है।
mkdir -pज़रूरत पड़ने पर parent directories बनाता है और error नहीं देता अगर directory पहले से मौजूद हो।cd dir || exit 1एक safe pattern है जो directory change fail होने पर script को रोक देता है।pushd/popdएक stack पर directories push और pop करते हैं, आपको एक previous location पर easily वापस जाने देते हुए।- Scripts के अंदर parsing के लिए
lsसे best बचा जाए; reliable filename handling के लिए globs याfindprefer करें।
Chapter Quiz — Complete all 13 topics to unlock
0/13 topics done
Complete these topics first: