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

File Tests (-f -d -e) का उपयोग

File tests यह देखने के लिए खिड़की से झांकने जैसे हैं कि एक घर है या नहीं, और यह घर है या बगीचा। आप पूछ सकते हैं कि कुछ exist करता है, एक file है, या एक folder है।

In this page:

  1. File Tests (-f -d -e)
Syntax
bash
[[ -f path ]]
[[ -d path ]]
[[ -e path ]]

File Tests (-f -d -e)

[[ -f path ]] जांचता है कि एक path मौजूद है और एक regular file है या नहीं, [[ -d path ]] एक directory के लिए जांचता है, और [[ -e path ]] दोनों में से किसी के भी existence के लिए जांचता है।

ये conditional file tests एक path पर पढ़ने, लिखने, या act करने से पहले essential हैं जिसके बारे में आप निश्चित नहीं कि मौजूद है। Opposite test करने के लिए इन्हें ! के साथ combine करें, जैसे [[ ! -f path ]]।

Note:
  • -e जांचता है "कुछ भी exist करता है"
  • -f और -d इस बारे में ज़्यादा specific हैं कि यह किस तरह की चीज़ है।

उदाहरण: File Tests (-f -d -e)

bash
#!/bin/bash
touch sample.txt
mkdir -p sample_dir

if [[ -f sample.txt ]]; then
    echo "sample.txt is a regular file"
fi

if [[ -d sample_dir ]]; then
    echo "sample_dir is a directory"
fi

if [[ -e nonexistent.txt ]]; then
    echo "This won't print"
else
    echo "nonexistent.txt does not exist"
fi
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. [ -f "$file" ] में spaces भूल जाना।
  2. -f या -d चाहिए होने पर -e उपयोग करना, क्योंकि -e किसी भी type के लिए true है।
  3. Variable को unquoted छोड़ना, जो spaces वाले names के साथ fail होता है।

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.