Languages
[Edit]
EN

Bash - check if file exists

5 points
Created by:
AnnLen
1813

In this short article we would like to show how to check if file exists using Bash and test command.

Simple solution:

#!/bin/bash

if [ -e "path/to/my/file/or/directory" ]
then
	echo "File exists";
fi

Alternative solution

#!/bin/bash

if test -e "path/to/my/file/or/directory"
then
	echo "File exists";
fi

 

Hey 👋
Would you like to know what we do?
  • Dirask is a friendly IT community for learners, professionals and hobbyists to share their knowledge and help each other in extraordinary easy way.
  • We welcome everyone,
    no matter what the experience,
    no matter how basic the question is,
    this community will help you.