I use the following script to see if the file exists:
#!/bin/bash FILE=$1 if [ -f $FILE ]; then echo "File $FILE exists." else echo "File $FILE does not exist." fi
If I just want to check that the file does not exist, what is the correct syntax to use?
#!/bin/bash FILE=$1 if [ $FILE does not exist ]; then echo "File $FILE does not exist." fi
#1st floor
test thing may also count.It's for me useful (based on Bash Shell: Check that the file exists ):
test -e FILENAME && echo "File exists" || echo "File doesn't exist"
#2nd floor
To undo the test, use'!'.This is equivalent to the "not" logical operator in other languages.Try this:
if [ ! -f /tmp/foo.txt ]; then echo "File not found!" fi
Or in a slightly different way:
if [ ! -f /tmp/foo.txt ] then echo "File not found!" fi
Or you can use:
if ! [ -f /tmp/foo.txt ] then echo "File not found!" fi
Or, keep it all the same:
if ! [ -f /tmp/foo.txt ]; then echo "File not found!"; fi
This can be written (using the "and" operator: &&):
[ ! -f /tmp/foo.txt ] && echo "File not found!"
Looks shorter:
[ -f /tmp/foo.txt ] || echo "File not found!"
#3rd floor
This Shell script can also be used to find files in the directory:
echo "enter file" read -r a if [ -s /home/trainee02/"$a" ] then echo "yes. file is there." else echo "sorry. file is not there." fi
#4th floor
To test the existence of a file, the parameter can be any 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 of the following tests apply to regular files, directories, and symbolic links:
-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
Sample script:
#!/bin/bash FILE=$1 if [ -f "$FILE" ]; then echo "File $FILE exists" else echo "File $FILE does not exist" fi
#5th floor
Bash File Test
-b filename - block special files
-c filename special character file
-d directoryname - Check if the directory exists
-e filename Checks whether a file exists, regardless of type (node, directory, socket, etc.)
-f filename checks if a regular file exists instead of a directory
-G filename - Check that the file exists and is owned by a valid group ID
-G filename set-group-id-true if the file exists and is set-group-id
-k filename - viscous bit
-L filename - Symbolic link
-O filename True if the file exists and is owned by a valid user ID
-r filename - Check if the file is readable
-S filename - Check if the file is a socket
-s filename checks if the file is non-zero size
-u filename checks if the file set-user-id bit is set
-w filename Check if the file is writable
-x filename Check whether the file is executable
How to use:
#!/bin/bash file=./file if [ -e "$file" ]; then echo "File exists" else echo "File does not exist" fi
You can use! To negate the test expression! Operator
#!/bin/bash file=./file if [ ! -e "$file" ]; then echo "File does not exist" else echo "File exists" fi