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

后端 未结 20 2819
深忆病人
深忆病人 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: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
    

提交回复
热议问题