This article introduces how to check if a file exists using an if condition in the Bash command line of Linux computers and servers.
if [ ! -f file.md ]
then
echo "Not existing."
fi
Check if File Exists Using if Condition
if [ -f file.md ]
then
echo "Existing."
fi
Check if Multiple Files Exist Using if Condition
if [ -f file1.md ] && [ -f file2.md ]
then
echo "Existing."
fi
Summary
We introduced how to check if a file exists using an if condition in the command line. There are many scenes where file if checks are performed, so it’s convenient to remember.
For example, you can perform processing like creating a file if it doesn’t exist. Or you could also perform processing like deleting a file if it exists.
That’s all.