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

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

    I prefer to do the following one-liner, in POSIX shell compatible format:

    $ [ -f "/$DIR/$FILE" ] || echo "$FILE NOT FOUND"
    
    $ [ -f "/$DIR/$FILE" ] && echo "$FILE FOUND"
    

    For a couple of commands, like I would do in a script:

    $  [ -f "/$DIR/$FILE" ] || { echo "$FILE NOT FOUND" ; exit 1 ;}
    

    Once I started doing this, I rarely use the fully typed syntax anymore!!

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

    The simplest way

    FILE=$1
    [ ! -e "${FILE}" ] && echo "does not exist" || echo "exists"
    
    0 讨论(0)
  • 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.

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

    The test thing may count too. It worked for me (based on Bash Shell: Check File Exists or Not):

    test -e FILENAME && echo "File exists" || echo "File doesn't exist"
    
    0 讨论(0)
  • 2020-11-22 11:35

    sometimes it may be handy to use && and || operators.

    Like in (if you have command "test"):

    test -b $FILE && echo File not there!
    

    or

    test -b $FILE || echo File there!
    
    0 讨论(0)
  • 2020-11-22 11:36

    Bash File Testing

    -b filename - Block special file
    -c filename - Special character file
    -d directoryname - Check for directory Existence
    -e filename - Check for file existence, regardless of type (node, directory, socket, etc.)
    -f filename - Check for regular file existence not a directory
    -G filename - Check if file exists and is owned by effective group ID
    -G filename set-group-id - True if file exists and is set-group-id
    -k filename - Sticky bit
    -L filename - Symbolic link
    -O filename - True if file exists and is owned by the effective user id
    -r filename - Check if file is a readable
    -S filename - Check if file is socket
    -s filename - Check if file is nonzero size
    -u filename - Check if file set-user-id bit is set
    -w filename - Check if file is writable
    -x filename - Check if file is executable

    How to use:

    #!/bin/bash
    file=./file
    if [ -e "$file" ]; then
        echo "File exists"
    else 
        echo "File does not exist"
    fi 
    

    A test expression can be negated by using the ! operator

    #!/bin/bash
    file=./file
    if [ ! -e "$file" ]; then
        echo "File does not exist"
    else 
        echo "File exists"
    fi 
    
    0 讨论(0)
提交回复
热议问题