How can I check a file exists and execute a command if not?
[ -f /tmp/filename.pid ] || python daemon.py restart
-f
checks if the given path exists and is a regular file (just -e
checks if the path exists)
the []
perform the test and returns 0
on success, 1
otherwise
the ||
is a C-like or
, so if the command on the left fails, execute the command on the right.
So the final statement says, if /tmp/filename.pid
does NOT exist then start the daemon.
How to check if a file exists from inside a batch file
if exist <insert file name here> (
rem file exists
) else (
rem file doesn't exist
)
Or on a single line (if only a single action needs to occur):
if exist <insert file name here> <action>
for example, this opens notepad on autoexec.bat, if the file exists:
if exist c:\autoexec.bat notepad c:\autoexec.bat
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
How to verify if a file exists in a batch file?
You can use IF EXIST to check for a file:
IF EXIST "filename" (
REM Do one thing
) ELSE (
REM Do another thing
)
If you do not need an "else", you can do something like this:
set __myVariable=
IF EXIST "C:\folder with space\myfile.txt" set __myVariable=C:\folder with space\myfile.txt
IF EXIST "C:\some other folder with space\myfile.txt" set __myVariable=C:\some other folder with space\myfile.txt
set __myVariable=
Here's a working example of searching for a file or a folder:
REM setup
echo "some text" > filename
mkdir "foldername"
REM finds file
IF EXIST "filename" (
ECHO file filename exists
) ELSE (
ECHO file filename does not exist
)
REM does not find file
IF EXIST "filename2.txt" (
ECHO file filename2.txt exists
) ELSE (
ECHO file filename2.txt does not exist
)
REM folders must have a trailing backslash
REM finds folder
IF EXIST "foldername\" (
ECHO folder foldername exists
) ELSE (
ECHO folder foldername does not exist
)
REM does not find folder
IF EXIST "filename\" (
ECHO folder filename exists
) ELSE (
ECHO folder filename does not exist
)
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
short way to check (one liner) if a file exists or not
Try this:
[ -f file ] && echo 1 || echo 0
How to execute certain commands if a file does NOT exist?
You can use this:
if [ ! -e "$file" ]; then
touch file
else
...
fi
Related Topics
The Meaning of Real, User, and Sys in Output of Linux Time Command
Ansible Conditional Based on Stdout of Result
X11: Run a Gnome App as Another User
Bash: Get List of Commands Starting with a Given String
Linux - Write Commands from One Terminal to Another
How to Prevent Out of Memory (Oom) Freezes on Linux
Is There Any Significant Difference Between Tcp_Cork and Tcp_Nodelay in This Use-Case
Svg to PDF on a Shared Linux Server
Can't Build 32Bit Wine on 64Bit Linux
Run Command 'At ' 5 Seconds from Now
Why Should I Recompile an Entire Program Just for a Library Update
Is It Safe to Issue Blocking Write() Calls on the Same Tcp Socket from Multiple Threads
Trying to Find All the Kernel Modules Needed for My MAChine Using Shell Script
Ubuntu Equivalent of Yum's Whatprovides, to Find Which Package Provides a File
Use Wc on All Subdirectories to Count the Sum of Lines
Shifting from Windows to *Nix Programming Platform