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

File Test Operators का उपयोग

File test operators एक script को एक file के बारे में yes/no questions पूछने देते हैं, जैसे यह मौजूद है, एक folder है, या writable है।
Syntax
bash
[[ -e path ]]   # exists
[[ -f path ]]   # regular file
[[ -d path ]]   # directory
[[ -r path ]]   # readable

Checking Existence

-e path true है अगर उस path पर बिल्कुल कुछ भी मौजूद हो (file, directory, symlink, आदि)। -f path इसे "मौजूद है और एक regular file है" तक narrow करता है।

उदाहरण: Checking Existence

bash
#!/bin/bash
touch sample.txt
if [[ -e sample.txt ]]; then
    echo "sample.txt exists"
fi
if [[ -f sample.txt ]]; then
    echo "sample.txt is a regular file"
fi
rm -f sample.txt

Checking Directories

-d path सिर्फ तब true है जब path मौजूद हो और एक directory हो। यह cd-ing करने से पहले या इसकी contents list करने से पहले standard guard है।

उदाहरण: Checking Directories

bash
#!/bin/bash
mkdir -p sample_dir
if [[ -d sample_dir ]]; then
    echo "sample_dir is a directory"
fi
if [[ ! -d does_not_exist ]]; then
    echo "does_not_exist is not a directory"
fi
rmdir sample_dir

Checking Permissions

-r, -w, और -x जांचते हैं कि current user के पास respectively एक path पर read, write, या execute permission है या नहीं। ये सिर्फ file के mode bits नहीं, असली OS-level permission checks reflect करते हैं।

उदाहरण: Checking Permissions

bash
#!/bin/bash
touch perm_test.txt
chmod 644 perm_test.txt
if [[ -r perm_test.txt ]]; then
    echo "readable"
fi
if [[ -w perm_test.txt ]]; then
    echo "writable"
fi
if [[ ! -x perm_test.txt ]]; then
    echo "not executable"
fi
rm -f perm_test.txt

Checking File Size

-s path true है जब path मौजूद हो और इसका size zero bytes से ज़्यादा हो, जो उन्हें process करने से पहले empty files detect करने का एक quick तरीका है।

Note: एक साथ कई conditions check करने के लिए tests को && या [[ -f a && -r a ]] के साथ combine करें।

उदाहरण: Checking File Size

bash
#!/bin/bash
touch empty.txt
echo "has content" > full.txt

if [[ ! -s empty.txt ]]; then
    echo "empty.txt is empty"
fi
if [[ -s full.txt ]]; then
    echo "full.txt has content"
fi
rm -f empty.txt full.txt
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. "क्या यह path मौजूद है" check करने के लिए -f उपयोग करना जब इसका असल में सिर्फ मतलब है "मौजूद है और एक regular file है"; एक directory -f में fail होती है।
  2. [ -f $var ] के अंदर variable को quote करना भूल जाना, जो टूट जाता है अगर path में spaces हों या यह empty हो।
  3. यह मान लेना कि -w और -x हर edge case में current user की असली effective permissions perfectly reflect करते हैं (जैसे read-only filesystems), जब वे कभी-कभी misleading हो सकते हैं।
चैप्टर सारांश
  • -e किसी भी तरह की existence जांचता है; -f एक regular file जांचता है; -d एक directory जांचता है।
  • -r, -w, -x current user के लिए read, write, और execute permission जांचते हैं।
  • -s जांचता है कि एक file मौजूद है और इसका size zero से ज़्यादा है।
  • Bash scripts में [ ... ] के बजाय [[ ... ]] को prefer करें; यह unquoted variables और empty strings को ज़्यादा safely handle करता है।

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.