100 Days of DevOps — Day 91-How to check if the file exists (Bash/Python)

Prashant Lakhera
4 min readMay 12, 2019

--

To view the updated DevOps course(101DaysofDevOps)

Course Registration link: https://www.101daysofdevops.com/register/

Course Link: https://www.101daysofdevops.com/courses/101-days-of-devops/

YouTube link: https://www.youtube.com/user/laprashant/videos

Welcome to Day 91 of 100 Days of DevOps, Focus for today is How to check if the file exists (Bash/Python)

This is one of the common tasks everyone needs to deal with, to check if the file exists. So the way I usually check is by using ls command

# ls /etc/resolv.conf/etc/resolv.conf

OR via the long listing

# ls -l /etc/resolv.conf-rw-r--r--. 1 root root 76 Apr  6 18:45 /etc/resolv.conf

The other way we can check via cat command

# cat /etc/resolv.conf# Generated by NetworkManagernameserver 192.168.0.1

OR sometimes we use pager like less

# less /etc/resolv.conf

Except for that, we can create a file using the touch command

# touch myfile
# ls -l myfile
-rw-r--r-- 1 root root 0 Apr 6 16:42 myfile

The other use of touch is to change the modification time

# touch myfile# ls -l myfile-rw-r--r-- 1 root root 0 Apr  6 16:43 myfile

To remove a file

# rm myfile

Now let’s look at the bash inbuilt command test

As per test man page

test - check file types and compare values

Now let's look at some of the test parameters

-e: Returns true value if file exists.-f: Return true value if file exists and regular file.-r: Return true value if file exists and is readable.-w: Return true value if file exists and is writable.-x: Return true value if file exists and is executable.-d: Return true value if exists and is a directory.

To check the file via test

# test -f /etc/resolv.conf
# echo $?
0

If the file doesn’t exist it will return exit code 1

# test -f /etc/nonexistfile# echo $?1

We can do much better than this

# test -f /etc/resolv.conf && echo "file exist" || echo "File doesn't exist"file exist# test -f /etc/nonexistfile && echo “file exist” || echo “File doesn’t exist”File doesn’t exist
  • &&(AND_IF) The intent is to execute the command that follows the && only if the first command is successful.
  • ||(AND_OR) Second command is executed only if the first command returns a non-zero exit status

For more info http://www.gnu.org/software/bash/manual/bashref.html#Lists

Let’s put together everything in a shell script

#!/bin/bashFILE="$1"if [ -f "$FILE" ];thenecho "$FILE exist"elseecho "$FILE doesn't exist"fi

Let make it executable

chmod +x filecheck.sh

and test it

# bash filecheck.sh /etc/resolv.conf/etc/resolv.conf exist

OR

# bash filecheck.sh /etc/notexist/etc/notexist doesn't exist
  • $1 is for positional parameters

Positional parameters are a series of special variables ($0 through $9) that contain the contents of the command line

Now what will happen if I am not supplying any file name

# bash filecheck.shdoesn't exist

So clearly there is a bug in this script

The simplest way to fix it is to ask the user to supply the required number of arguments(there is a much better way to deal with it but for the time being let stick to basic one)

#!/bin/bashFILE="$1"if [ "$#" -ne 1 ]thenecho "Please enter the file name"exit 1fiif [ -f "$FILE" ];thenecho "$FILE exist"elseecho "$FILE doesn't exist"fi
  • ($#) Expands to the number of positional parameters in decimal.

Now if I execute the same code

# bash filecheck.shPlease enter the file name

and after passing the filename

# bash filecheck.sh  /etc/resolv.conf/etc/resolv.conf exist

and with a filename that doesn’t exist

# bash filecheck.sh  /etc/dontexistfile/etc/dontexistfile doesn't exist

Same check we can do it with directory using -d

-d: Return true value if exists and is a directory.

Now let’s do the same thing via Python way(I am using Python3 for this tutorial but I believe this works for Python2)

So let’s start with the OS module, we can use function exists to check if the file exists

>>> import os>>> os.path.exists("/etc/resolv.conf")True

It will return False if the file doesn’t exist

>>> os.path.exists("/etc/notexist")False

Similar way we can check if it’s a file using isfile

>>> os.path.isfile("/etc/resolv.conf")True>>> os.path.isfile("/etc")False

We can use try and except block(Here we are trying to open a file and it will throw an exception if the file doesn’t exist)

>>> try:...     f = open("/etc/resolv.conf")...     f.close()... except FileNotFoundError:...     print("File doesn't exist")...

Example of exception

>>> try:...     f = open("/etc/resolvs.conf")...     f.close()... except FileNotFoundError:...     print("File doesn't exist")...File doesn't exist

Looking forward from you guys to join this journey and spend a minimum an hour every day for the next 100 days on DevOps work and post your progress using any of the below medium.

Reference

--

--

Prashant Lakhera
Prashant Lakhera

Written by Prashant Lakhera

AWS Community Builder, Ex-Redhat, Author, Blogger, YouTuber, RHCA, RHCDS, RHCE, Docker Certified,4XAWS, CCNA, MCP, Certified Jenkins, Terraform Certified, 1XGCP

No responses yet