How do I tell if a file does not exist in Bash?
The test
command (written as [
here) has a "not" logical operator, !
(exclamation mark):
if [ ! -f /tmp/foo.txt ]; then
echo "File not found!"
fi
How to check if a file exists in a shell script
You're missing a required space between the bracket and -e
:
#!/bin/bash
if [ -e x.txt ]
then
echo "ok"
else
echo "nok"
fi
Test if a file exists
What you have here is a misunderstanding of where specific syntax is used. The [ -e ./documents_2019-12-12.tar.gz ]
part of your command is syntax specific to the if
clause in bash. Here's an example
if [ -e ./documents_2019-12-12.tar.gz ]
then
echo "File Exists!"
fi
The square brackets [] are used to surround the check being performed by the if statement and the -e
flag is specific to these if
checks. More info here http://tldp.org/LDP/Bash-Beginners-Guide/html/sect_07_01.html
To explain the error you're seeing, the <
operator takes a file to the right and feeds the contents to the command on the left. In your case the <
sees the [
as the thing on the right so we try and read it as a file. Such a file doesn't exist so bash helpfully tells you that there's an error (the bash: [: No such file or directory
bit) and then quits out.
How do I check if file exists in Makefile so I can delete it?
The second top answer mentions ifeq
, however, it fails to mention that this ifeq
must be at the same indentation level in the makefile as the name of the target, e.g., to download a file only if it doesn't currently exist, the following code could be used:
download:
ifeq (,$(wildcard ./glob.c))
curl … -o glob.c
endif
# THIS DOES NOT WORK!
download:
ifeq (,$(wildcard ./glob.c))
curl … -o glob.c
endif
Fastest way to check if a file exists using standard C++/C++11,14,17/C?
Well I threw together a test program that ran each of these methods 100,000 times, half on files that existed and half on files that didn't.
#include <sys/stat.h>
#include <unistd.h>
#include <string>
#include <fstream>
inline bool exists_test0 (const std::string& name) {
ifstream f(name.c_str());
return f.good();
}
inline bool exists_test1 (const std::string& name) {
if (FILE *file = fopen(name.c_str(), "r")) {
fclose(file);
return true;
} else {
return false;
}
}
inline bool exists_test2 (const std::string& name) {
return ( access( name.c_str(), F_OK ) != -1 );
}
inline bool exists_test3 (const std::string& name) {
struct stat buffer;
return (stat (name.c_str(), &buffer) == 0);
}
Results for total time to run the 100,000 calls averaged over 5 runs,
Method | Time |
---|---|
exists_test0 (ifstream) | 0.485s |
exists_test1 (FILE fopen) | 0.302s |
exists_test2 (posix access()) | 0.202s |
exists_test3 (posix stat()) | 0.134s |
What's the best way to check if a file exists in C?
Look up the access()
function, found in unistd.h
. You can replace your function with
if (access(fname, F_OK) == 0) {
// file exists
} else {
// file doesn't exist
}
Under Windows (VC) unistd.h
does not exist. To make it work it is necessary to define:
#ifdef WIN32
#include <io.h>
#define F_OK 0
#define access _access
#endif
You can also use R_OK
, W_OK
, and X_OK
in place of F_OK
to check for read permission, write permission, and execute permission (respectively) rather than existence, and you can OR any of them together (i.e. check for both read and write permission using R_OK|W_OK
)
Update: Note that on Windows, you can't use W_OK
to reliably test for write permission, since the access function does not take DACLs into account. access( fname, W_OK )
may return 0 (success) because the file does not have the read-only attribute set, but you still may not have permission to write to the file.
Take a file name as input and check if it exists
First of all, I want to thank anyone and everyone who tried to help. After 3 hard working days, I found the answer, here it is:
#!/bin/bash
file="$@"
if [ -f $file ]
then
echo "File exists"
else
echo "File does not exist"
fi
Using this table:
Variable Name | Description |
---|---|
$0 | The name of the Bash script |
$1 - $9 | The first 9 arguments to the Bash script |
$# | Number of arguments passed to the Bash script |
$@ | All arguments passed to the Bash script |
$? | The exit status of the most recently run process |
$$ | The process ID of the current script |
$USER | The username of the user running the script |
$HOSTNAME | The hostname of the machine |
$RANDOM | A random number |
$LINENO | The current line number in the script |
How do I tell if a file does not exist in Bash?
The test
command (written as [
here) has a "not" logical operator, !
(exclamation mark):
if [ ! -f /tmp/foo.txt ]; then
echo "File not found!"
fi
Related Topics
Urlconnection Getcontentlength() Is Returning a Negative Value
Deadobjectexception on Android App
How to Print to the Console in Android Studio
Use Startactivityforresult from Non-Activity
How to Override the Cipherlist Sent to the Server by Android When Using Httpsurlconnection
How to Combine One Android Studio Project into Another Android Studio Project
Aes/Cbc/Pkcs5Padding Encrypt in Java Decrypt in Ruby
Wrong Version of Keystore on Android Call
Cannot Resolve Method Getmap()
Libavcodec.So: Has Text Relocations
How to Remove a Specific Element from a JSONarray
What Is the Equivalent to a JavaScript Setinterval/Settimeout in Android/Java