check if file exists bash

Solutions on MaxInterview for check if file exists bash by the best coders in the world

showing results for - "check if file exists bash"
Clara
12 Nov 2018
1FILE=/etc/resolv.conf
2if [ -f "$FILE" ]; then
3    echo "$FILE exists."
4else 
5    echo "$FILE does not exist."
6fi
7
Breanna
14 May 2019
1#!/bin/bash
2if [ -e x.txt ]
3then
4    echo "ok"
5else
6    echo "nok"
7fi
Francisco
29 May 2018
1# The most readable option when checking whether a file exist or not is
2# to use the test command or the old '[' or the new '[[' in combination
3# with the if statement.
4# Any of the snippets below will check whether the /etc/resolv.conf file
5# exists:
6
7FILE=/etc/resolv.conf
8if test -f "$FILE"; then
9    echo "$FILE exist"
10fi
11
12# or
13
14FILE=/etc/resolv.conf
15if [ -f "$FILE" ]; then
16    echo "$FILE exist"
17fi
18
19# or
20
21FILE=/etc/resolv.conf
22if [[ -f "$FILE" ]]; then
23    echo "$FILE exist"
24fi
25
26
27
Nova
23 Jan 2018
1FILE=/etc/resolv.conf
2if [ -f "$FILE" ]; then
3    echo "$FILE exists."
4fi
5
Lara
25 Sep 2016
1[ -f /etc/resolv.conf ] && echo "$FILE exists."
2
similar questions
queries leading to this page
check if file exists bash