You are writing a Bash script to automate server maintenance. You want to check if a directory exists before trying to create it. Which command would you use within your script to check for the directory's existence?

  • if [ -d "$directory" ]
  • if [ -e "$directory" ]
  • if [ -f "$directory" ]
  • if [ -s "$directory" ]
The correct command to check if a directory exists in a Bash script is if [ -d "$directory" ]. This condition tests whether the given directory path, represented by $directory, exists as a directory. Option 2 -e checks for the existence of any file or directory, not specifically a directory. Option 3 -f checks for regular files, and Option 4 -s checks if the file has a size greater than zero.
Add your answer
Loading...

Leave a comment

Your email address will not be published. Required fields are marked *