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

Directory के Operations

Scripts folders बना सकती हैं, उनके बीच move हो सकती हैं, और याद रख सकती हैं कि वे कहां से आई थीं, बिल्कुल हाथ से cd और ls typing करने जैसा।
Syntax
bash
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

bash
#!/bin/bash
mkdir -p project/src/utils
if [[ -d project/src/utils ]]; then
    echo "nested directories created"
fi
rm -rf project

Directories को Safely बदलना

cd fail हो सकता है अगर target मौजूद न हो या accessible न हो। इसे || exit 1 (या एक function के अंदर || return 1) के साथ chain करना script को गलत जगह operate करना जारी रखने से रोकता है।

उदाहरण: Changing Directories Safely

bash
#!/bin/bash
mkdir -p work_area
cd work_area || exit 1
echo "Now in: $(pwd | xargs basename)"
cd ..
rmdir work_area

pushd और popd उपयोग करना

pushd dir directory change करता है जबकि आप कहां से आए एक stack पर याद रखता है, और popd उस remembered location पर वापस जाता है। यह उन scripts के लिए convenient है जिन्हें temporarily कहीं और काम करके वापस आना हो।

Note: Scripts में pushd/popd output को /dev/null में redirect करें क्योंकि उनका default directory-stack printout वहां शायद ही कभी useful होता है।

उदाहरण: Using pushd and popd

bash
#!/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

एक 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 है।

Warning: Scripts में कभी ls output parse न करें (जैसे for f in $(ls)); इसके बजाय globs (for f in *) या find उपयोग करें।

उदाहरण: Listing Directory Contents in a Script

bash
#!/bin/bash
mkdir -p listing_demo
touch listing_demo/a.txt listing_demo/b.txt
ls listing_demo
rm -rf listing_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. यह मान लेना कि cd हमेशा succeed होता है; scripts को इसका exit status जांचना चाहिए या गलत directory में silently continue करने से बचने के लिए cd dir || exit 1 उपयोग करना चाहिए।
  2. यह भूल जाना कि nested directories बनाने या directory पहले से मौजूद होने पर error से बचने के लिए mkdir -p चाहिए।
  3. यह 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 या find prefer करें।

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.