How can I check if a directory exists in a Bash shell script?

后端 未结 30 2356
猫巷女王i
猫巷女王i 2020-11-22 10:35

What command can be used to check if a directory exists or not, within a Bash shell script?

30条回答
  •  眼角桃花
    2020-11-22 11:28

    This answer wrapped up as a shell script

    Examples

    $ is_dir ~                           
    YES
    
    $ is_dir /tmp                        
    YES
    
    $ is_dir ~/bin                       
    YES
    
    $ mkdir '/tmp/test me'
    
    $ is_dir '/tmp/test me'
    YES
    
    $ is_dir /asdf/asdf                  
    NO
    
    # Example of calling it in another script
    DIR=~/mydata
    if [ $(is_dir $DIR) == "NO" ]
    then
      echo "Folder doesnt exist: $DIR";
      exit;
    fi
    

    is_dir

    function show_help()
    {
      IT=$(CAT <

提交回复
热议问题