How do I tell if a regular file does not exist in Bash?

后端 未结 20 2465
深忆病人
深忆病人 2020-11-22 10:57

I\'ve used the following script to see if a file exists:

#!/bin/bash

FILE=$1     
if [ -f $FILE ]; then
   echo \"File $FILE exists.\"
else
   echo \"File $         


        
相关标签:
20条回答
  • 2020-11-22 11:17

    You can negate an expression with "!":

    #!/bin/bash
    FILE=$1
    
    if [ ! -f "$FILE" ]
    then
        echo "File $FILE does not exist"
    fi
    

    The relevant man page is man test or, equivalently, man [ -- or help test or help [ for the built-in bash command.

    0 讨论(0)
  • 2020-11-22 11:19

    This code also working .

    #!/bin/bash
    FILE=$1
    if [ -f $FILE ]; then
     echo "File '$FILE' Exists"
    else
     echo "The File '$FILE' Does Not Exist"
    fi
    
    0 讨论(0)
  • 2020-11-22 11:20

    There are three distinct ways to do this:

    1. Negate the exit status with bash (no other answer has said this):

      if ! [ -e "$file" ]; then
          echo "file does not exist"
      fi
      

      Or:

      ! [ -e "$file" ] && echo "file does not exist"
      
    2. Negate the test inside the test command [ (that is the way most answers before have presented):

      if [ ! -e "$file" ]; then
          echo "file does not exist"
      fi
      

      Or:

      [ ! -e "$file" ] && echo "file does not exist"
      
    3. Act on the result of the test being negative (|| instead of &&):

      Only:

      [ -e "$file" ] || echo "file does not exist"
      

      This looks silly (IMO), don't use it unless your code has to be portable to the Bourne shell (like the /bin/sh of Solaris 10 or earlier) that lacked the pipeline negation operator (!):

      if [ -e "$file" ]; then
          :
      else
          echo "file does not exist"
      fi
      
    0 讨论(0)
  • You can also group multiple commands in the one liner

    [ -f "filename" ] || ( echo test1 && echo test2 && echo test3 )

    or

    [ -f "filename" ] || { echo test1 && echo test2 && echo test3 ;}

    If filename doesn't exit, the output will be

    test1
    test2
    test3
    

    Note: ( ... ) runs in a subshell, { ... ;} runs in the same shell. The curly bracket notation works in bash only.

    0 讨论(0)
  • 2020-11-22 11:21

    To test file existence, the parameter can be any one of the following:

    -e: Returns true if file exists (regular file, directory, or symlink)
    -f: Returns true if file exists and is a regular file
    -d: Returns true if file exists and is a directory
    -h: Returns true if file exists and is a symlink
    

    All the tests below apply to regular files, directories, and symlinks:

    -r: Returns true if file exists and is readable
    -w: Returns true if file exists and is writable
    -x: Returns true if file exists and is executable
    -s: Returns true if file exists and has a size > 0
    

    Example script:

    #!/bin/bash
    FILE=$1
    
    if [ -f "$FILE" ]; then
       echo "File $FILE exists"
    else
       echo "File $FILE does not exist"
    fi
    
    0 讨论(0)
  • 2020-11-22 11:23
    [[ -f $FILE ]] || printf '%s does not exist!\n' "$FILE"
    

    Also, it's possible that the file is a broken symbolic link, or a non-regular file, like e.g. a socket, device or fifo. For example, to add a check for broken symlinks:

    if [[ ! -f $FILE ]]; then
        if [[ -L $FILE ]]; then
            printf '%s is a broken symlink!\n' "$FILE"
        else
            printf '%s does not exist!\n' "$FILE"
        fi
    fi
    
    0 讨论(0)
提交回复
热议问题