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

后端 未结 20 2701
深忆病人
深忆病人 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条回答
  •  旧时难觅i
    2020-11-22 11:33

    You can do this:

    [[ ! -f "$FILE" ]] && echo "File doesn't exist"
    

    or

    if [[ ! -f "$FILE" ]]; then
        echo "File doesn't exist"
    fi
    

    If you want to check for file and folder both, then use -e option instead of -f. -e returns true for regular files, directories, socket, character special files, block special files etc.

提交回复
热议问题