You can use test -e option to check if a file exists in a shell script.
1 2 3 4 5 6 7 8 9 |
## Check if a file exists echo "hello world" > file.txt if [ -e file.txt ] then echo "file exists" else echo "file does not exists" fi |
If you want to check if a directory exists then use test -id option
1 2 3 4 5 6 7 8 9 |
## Check if a directory exists mkdir mydir if [ -d mydir ] then echo "dir exists" else echo "dir does not exists" fi |
Get all the test option using test manual (man test)