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.
Loading...
Related Quiz
- You've been tasked to lockdown a specific application on an Ubuntu server. You decide to use a tool that restricts programs to a limited set of resources. Which tool would you primarily utilize?
- You've written a script that processes a list of files. You want to ensure that even if a filename contains spaces, it's treated as a single argument. Which built-in variable should you modify?
- What is the primary distinction between top and htop when monitoring system processes?
- The __________ command in Linux allows mounting of NFS shared directories.
- A colleague informs you that a specific daemon is causing issues and should be temporarily disabled without stopping it. Which command in Linux would allow you to mask the service?