Bash Program to Check if A Directory Exists
Q. How do I check if a directory exists or not?
Add the following code to a shell script to verify if defined directory exists. Then execute a set of statement if directory exists:
- 
       
        Check if a directory exists:
       
       
Shell123if [ - d "$DIR" ] ; then# Script statements if $DIR exists.fi - 
       
        Check if a directory doesn’t exist:
       
       Shell123if [ ! - d "$DIR" ] ; then# script statements if $DIR doesn't exist.fi
 
Example
A sample shell script to navigate to a specific directory only if exists.
| 
                   
                    1
                    
                   
                    2
                    
                   
                    3
                    
                   
                    4
                    
                   
                    5
                    
                   | 
                 
                   
                    
                     DIR
                    
                    =
                    
                     "/var/backup"
                    
                    
                   
                    
                     if
                    
                    
                    
                    
                     [
                    
                    
                    
                    -
                    
                     d
                    
                    
                    
                    
                     "$DIR"
                    
                    
                    
                    
                     ]
                    
                    
                     ;
                    
                    
                    
                    
                     then
                    
                    
                   
                    
                    
                    
                     cd
                    
                    
                    
                    
                     $
                    
                    
                     DIR
                    
                    
                   
                    
                     fi
                    
                    
                   | 
                
In most cases, where you are switching to directory which don’t exists. In that case, the script will exit with an error. So its good to create directory first and then switch to directory.
| 
                          
                           1
                           
                          
                           2
                           
                          
                           3
                           
                          
                           4
                           
                          
                           5
                           
                          | 
                        
                          
                           
                            DIR
                           
                           =
                           
                            "/var/backup"
                           
                           
                          
                           
                            if
                           
                           
                           
                           
                            [
                           
                           
                           
                           
                            !
                           
                           
                           
                           -
                           
                            d
                           
                           
                           
                           
                            "$DIR"
                           
                           
                           
                           
                            ]
                           
                           
                            ;
                           
                           
                           
                           
                            then
                           
                           
                          
                           
                           
                           
                            mkdir
                           
                           
                           
                           -
                           
                            p
                           
                           
                           
                           
                            $
                           
                           
                            DIR
                           
                           
                           
                           &&
                           
                           
                           
                            cd
                           
                           
                           
                           
                            $
                           
                           
                            DIR
                           
                           
                          
                           
                            fi
                           
                           
                          | 
                       
                   Was this article helpful to you?
Advertisement