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

# ls /etc/resolv.conf/etc/resolv.conf
# ls -l /etc/resolv.conf-rw-r--r--. 1 root root 76 Apr  6 18:45 /etc/resolv.conf
# cat /etc/resolv.conf# Generated by NetworkManagernameserver 192.168.0.1
# less /etc/resolv.conf
# touch myfile
# ls -l myfile
-rw-r--r-- 1 root root 0 Apr 6 16:42 myfile
# touch myfile# ls -l myfile-rw-r--r-- 1 root root 0 Apr  6 16:43 myfile
# rm myfile
test - check file types and compare values
-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.
# test -f /etc/resolv.conf
# echo $?
0
# test -f /etc/nonexistfile# echo $?1
# 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
#!/bin/bashFILE="$1"if [ -f "$FILE" ];thenecho "$FILE exist"elseecho "$FILE doesn't exist"fi
chmod +x filecheck.sh
# bash filecheck.sh /etc/resolv.conf/etc/resolv.conf exist
# bash filecheck.sh /etc/notexist/etc/notexist doesn't exist
  • $1 is for positional parameters
# bash filecheck.shdoesn't exist
#!/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.
# bash filecheck.shPlease enter the file name
# bash filecheck.sh  /etc/resolv.conf/etc/resolv.conf exist
# bash filecheck.sh  /etc/dontexistfile/etc/dontexistfile doesn't exist
-d: Return true value if exists and is a directory.
>>> import os>>> os.path.exists("/etc/resolv.conf")True
>>> os.path.exists("/etc/notexist")False
>>> os.path.isfile("/etc/resolv.conf")True>>> os.path.isfile("/etc")False
>>> try:...     f = open("/etc/resolv.conf")...     f.close()... except FileNotFoundError:...     print("File doesn't exist")...
>>> try:...     f = open("/etc/resolvs.conf")...     f.close()... except FileNotFoundError:...     print("File doesn't exist")...File doesn't exist

--

--

--

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

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

My Experience at Outboxedu, I am glad I am part of the team.

zkTube Weekly Report 1.01–1.07

Why Do Businesses Prefer Hiring Java Developers For Their Growing Businesses?

Building Ubuntu 20.04 Root Filesystem for Firecracker

Strong Consistency: A more detailed view about th

Power Control and DCI in 5G NR

Port Forwarding

Quick words on Vector

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Prashant Lakhera

Prashant Lakhera

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

More from Medium

List of 16 Linux Networking and Troubleshooting Commands for Beginners

Docker — Log Management

Ubuntu: How to use netplan to configure static IP

How to install Nginx web server on CentOS 8