YouTube Icon

Code Playground.

How to Check if a File or Directory Exists in Bash

CFG

How to Check if a File or Directory Exists in Bash

Commonly when composing Shell contents, you may end up in a circumstance where you have to play out an activity dependent on if a document exists. 

In Bash, you can utilize the test order to check whether a document exists and decide the sort of the record. 

The test order takes one of the accompanying linguistic structure structures: 

test EXPRESSION
[ EXPRESSION ]
[[ EXPRESSION ]]

On the off chance that you need your content to be compact, you ought to lean toward utilizing the old test [ order, which is accessible on all POSIX shells. The new overhauled variant of the test order [[ (twofold sections) is upheld on most current frameworks utilizing Bash, Zsh, and Ksh as a default shell. 

Check if File Exists

While checking if a record exists, the most normally utilized FILE administrators are - e and - f. The first will check whether a document exists paying little mind to the sort, while the subsequent one will restore genuine just if the FILE is a standard record (not a registry or a gadget). 

The most lucid choice while checking if a document exists is to utilize the test order in mix with the if articulation . Any of the pieces underneath will check whether the/and so on/resolv.conf document exists: 

FILE=/etc/resolv.conf
if test -f "$FILE"; then
    echo "$FILE exists."
fi

 

FILE=/etc/resolv.conf
if [ -f "$FILE" ]; then
    echo "$FILE exists."
fi

 

FILE=/etc/resolv.conf
if [[ -f "$FILE" ]]; then
    echo "$FILE exists."
fi

 

On the off chance that you need to play out an alternate activity dependent on if the record exists essentially utilize the on the off chance that/at that point build: 

FILE=/etc/resolv.conf
if [ -f "$FILE" ]; then
    echo "$FILE exists."
else 
    echo "$FILE does not exist."
fi

Continuously utilize twofold statements to keep away from issues when managing records containing whitespace in their names. 

You can likewise utilize the test order without the if articulation. The order after the && administrator might be executed if the leave status of the test order is valid, 

test -f /etc/resolv.conf && echo "$FILE exists."

 

[ -f /etc/resolv.conf ] && echo "$FILE exists."

 

[[ -f /etc/resolv.conf ]] && echo "$FILE exists."

On the off chance that you need to run a progression of order after the && administrator just encase the orders in wavy sections isolated by ; or &&: 

[ -f /etc/resolv.conf ] && { echo "$FILE exist."; cp "$FILE" /tmp/; }

Inverse to &&, the assertion after the || administrator may be executed if the leave status of the test order is bogus. 

[ -f /etc/resolv.conf ] && echo "$FILE exist." || echo "$FILE does not exist."

Check if Directory Exist 

The administrators - d permits you to test if a record is a registry. 

For instance to check whether the/and so forth/docker catalog exist you would utilize: 

FILE=/etc/docker
if [ -d "$FILE" ]; then
    echo "$FILE is a directory."
fi
[ -d /etc/docker ] && echo "$FILE is a directory."

 

You can likewise utilize the twofold sections [[ rather than a solitary one [. 

Check if File does Not Exist

Like numerous different dialects, the test articulation can be invalidated utilizing the ! (outcry mark) intelligent not administrator: 

FILE=/etc/docker
if [ ! -f "$FILE" ]; then
    echo "$FILE does not exist."
fi

 Same as above: 

[ ! -f /etc/docker ] && echo "$FILE does not exist."

Check if Multiple Files Exist 

Rather than utilizing convoluted settled if/else builds you can utilize - a (or && with [[) to test if different documents exist: 

on the off chance that [ - f/and so on/resolv.conf - a - f/and so on/has ]; at that point 

if [ -f /etc/resolv.conf -a -f /etc/hosts ]; then
    echo "Both files exist."
fi

 

if [[ -f /etc/resolv.conf && -f /etc/hosts ]]; then
    echo "Both files exist."
fi

Equal variations without utilizing the IF articulation: 

[ -f /etc/resolv.conf -a -f /etc/hosts ] && echo "Both files exist."

 

[[ -f /etc/resolv.conf && -f /etc/hosts ]] && echo "Both files exist."

File test operators

The test order incorporates the accompanying FILE administrators that permit you to test for specific kinds of records: 

  • - b FILE - True if the FILE exists and is an extraordinary square record. 
  • - c FILE - True if the FILE exists and is an extraordinary character record. 
  • - d FILE - True if the FILE exists and is a registry. 
  • - e FILE - True if the FILE exists and is a record, paying little heed to type (hub, catalog, attachment, and so on) 
  • - f FILE - True if the FILE exists and is a customary record (not a catalog or gadget). 
  • - G FILE - True if the FILE exists and has a similar gathering as the client running the order. 
  • - h FILE - True if the FILE exists and is a representative connection. 
  • - g FILE - True if the FILE exists and has set-bunch id (sgid) banner set. 
  • - k FILE - True if the FILE exists and has a tacky piece banner set. 
  • - L FILE - True if the FILE exists and is an emblematic connection. 
  • - O FILE - True if the FILE exists and is possessed by the client running the order. 
  • - p FILE - True if the FILE exists and is a line. 
  • - r FILE - True if the FILE exists and is meaningful. 
  • - S FILE - True if the FILE exists and is an attachment. 
  • - s FILE - True if the FILE exists and has nonzero size. 
  • - u FILE - True if the FILE exists, and set-client id (suid) banner is set. 
  • - w FILE - True if the FILE exists and is writable. 
  • - x FILE - True if the FILE exists and is executable. 

Conclusion

In this guide, we have told you the best way to check if a document or registry exists in Bash. 

In the event that you have any inquiries or criticism, don't hesitate to leave a remark.




CFG