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

后端 未结 30 1942
猫巷女王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:24

    To check more than one directory use this code:

    if [ -d "$DIRECTORY1" ] && [ -d "$DIRECTORY2" ] then
        # Things to do
    fi
    
    0 讨论(0)
  • 2020-11-22 11:25

    The below find can be used,

    find . -type d -name dirname -prune -print
    
    0 讨论(0)
  • 2020-11-22 11:26

    Shorter form:

    [ -d "$DIR" ] && echo "Yes"
    
    0 讨论(0)
  • 2020-11-22 11:26

    The ls command in conjunction with -l (long listing) option returns attributes information about files and directories.
    In particular the first character of ls -l output it is usually a d or a - (dash). In case of a d the one listed is a directory for sure.

    The following command in just one line will tell you if the given ISDIR variable contains a path to a directory or not:

    [[ $(ls -ld "$ISDIR" | cut -c1) == 'd' ]] &&
        echo "YES, $ISDIR is a directory." || 
        echo "Sorry, $ISDIR is not a directory"
    

    Practical usage:

        [claudio@nowhere ~]$ ISDIR="$HOME/Music" 
        [claudio@nowhere ~]$ ls -ld "$ISDIR"
        drwxr-xr-x. 2 claudio claudio 4096 Aug 23 00:02 /home/claudio/Music
        [claudio@nowhere ~]$ [[ $(ls -ld "$ISDIR" | cut -c1) == 'd' ]] && 
            echo "YES, $ISDIR is a directory." ||
            echo "Sorry, $ISDIR is not a directory"
        YES, /home/claudio/Music is a directory.
    
        [claudio@nowhere ~]$ touch "empty file.txt"
        [claudio@nowhere ~]$ ISDIR="$HOME/empty file.txt" 
        [claudio@nowhere ~]$ [[ $(ls -ld "$ISDIR" | cut -c1) == 'd' ]] && 
            echo "YES, $ISDIR is a directory." || 
            echo "Sorry, $ISDIR is not a directoy"
        Sorry, /home/claudio/empty file.txt is not a directory
    
    0 讨论(0)
  • 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 <<EOF
    
      usage: DIR
      output: YES or NO, depending on whether or not the directory exists.
    
      )
      echo "$IT"
      exit
    }
    
    if [ "$1" == "help" ]
    then
      show_help
    fi
    if [ -z "$1" ]
    then
      show_help
    fi
    
    DIR=$1
    if [ -d $DIR ]; then 
       echo "YES";
       exit;
    fi
    echo "NO";
    
    0 讨论(0)
  • 2020-11-22 11:30

    You can use test -d (see man test).

    -d file True if file exists and is a directory.

    For example:

    test -d "/etc" && echo Exists || echo Does not exist
    

    Note: The test command is same as conditional expression [ (see: man [), so it's portable across shell scripts.

    [ - This is a synonym for the test builtin, but the last argument must, be a literal ], to match the opening [.

    For possible options or further help, check:

    • help [
    • help test
    • man test or man [
    0 讨论(0)
提交回复
热议问题