Linux Tutorials

Test if a listing exists in Linux or Unix shell

SEOClerks

I have to verify if a listing exists or not, inside a shell script working on Linux or Unix-like system? How do I verify if a listing exists in a shell script?

A listing is nothing however a location for storing recordsdata on the Linux system in a hierarchical format. For instance, $HOME/Downloads/ would retailer all downloaded recordsdata or /tmp/ would retailer non permanent recordsdata. This web page reveals find out how to see if a listing exists in Linux or Unix-like programs.

Adblock detected 😱

My web site is made doable by displaying on-line commercials to my guests. I get it! Adverts are annoying however they assist hold this web site working. It’s exhausting to maintain the positioning working and producing new content material when so many individuals block adverts. Please contemplate donating cash to the nixCraft by way of PayPal/Bitcoin, or grow to be a supporter utilizing Patreon.

The way to verify if a listing exists in Linux

One can verify if a listing exists in a Linux shell script utilizing the next syntax:
[ -d “/path/dir/” ] && echo “Listing /path/dir/ exists.”You should utilize ! to verify if a listing doesn’t exists on Unix:
[ ! -d “/dir1/” ] && echo “Listing /dir1/ DOES NOT exists.”

One can verify if a listing exists in Linux script as follows:

DIR=“/and many others/httpd/”
if [ -d $DIR ]; then
# Take motion if $DIR exists. #
echo “Putting in config recordsdata in $…”
fi

DIR=”/and many others/httpd/”
if [ -d “$DIR” ]; then
# Take motion if $DIR exists. #
echo “Putting in config recordsdata in $…”
fi

OR

DIR=“/and many others/httpd/”
if [ -d $DIR ]; then
### Take motion if $DIR exists ###
echo “Putting in config recordsdata in $…”
else
### Management will bounce right here if $DIR does NOT exists ###
echo “Error: $ not discovered. Can’t proceed.”
exit 1
fi

DIR=”/and many others/httpd/”
if [ -d “$DIR” ]; then
### Take motion if $DIR exists ###
echo “Putting in config recordsdata in $…”
else
### Management will bounce right here if $DIR does NOT exists ###
echo “Error: $ not discovered. Can’t proceed.”
exit 1
fi

Linux verify if a listing exists and take some motion

Here’s a pattern shell script to see if a folder exists or not in Linux:

#!/bin/bash
d=“$1”
 
[ $d == “” ] &&
[ -d $ ] && echo “Listing $d discovered.” || echo “Listing $d not discovered.”

#!/bin/bash
d=”$1″ [ “$d” == “” ] &&
[ -d “$” ] && echo “Listing $d discovered.” || echo “Listing $d not discovered.”

Run it as follows:
./take a look at.sh
./take a look at.sh /tmp/
./take a look at.sh /nixCraft
Bash check if a directory exists in Linux or Unix shell

Test if listing exists in bash and if not create it

Here’s a pattern shell script to verify if a listing doesn’t exist and create it as per our wants:

#!/bin/bash
dldir=$HOME/linux/5.x”
_out=“/tmp/out.$$”
 
# Construct urls
url=“some_url/file.tar.gz”
file=$
 
### Test for dir, if not discovered create it utilizing the mkdir ##
[ ! -d $dldir ] && mkdir -p $dldir
 
# Now obtain it
wget -qc $url -O $dldir/$
 
# Do one thing else under #

#!/bin/bash
dldir=”$HOME/linux/5.x”
_out=”/tmp/out.$$” # Construct urls
url=”some_url/file.tar.gz”
file=”$” ### Test for dir, if not discovered create it utilizing the mkdir ##
[ ! -d “$dldir” ] && mkdir -p “$dldir” # Now obtain it
wget -qc “$url” -O “$dldir/$” # Do one thing else under #

Be sure you at all times wrap shell variables comparable to $DIR in double quotes (“$DIR” to keep away from any surprises in your shell scripts:

DIR=“foo”
[ -d $DIR ] && echo “Discovered”
##
## this can fail as DIR will solely develop to “foo” and to not “foo bar stuff”
## therefore wrap it
##
DIR=“foo bar stuff”
[ -d $DIR ] && echo “Discovered”

DIR=”foo”
[ -d “$DIR” ] && echo “Discovered”
##
## this can fail as DIR will solely develop to “foo” and to not “foo bar stuff”
## therefore wrap it
##
DIR=”foo bar stuff”
[ -d $DIR ] && echo “Discovered”

Utilizing take a look at command

One can use the take a look at command to verify file sorts and evaluate values. For instance, see if FILE exists and is a listing. The syntax is:
take a look at -d “DIRECTORY” && echo “Discovered/Exists” || echo “Doesn’t exist”
The take a look at command is identical as [ conditional expression. Therefore, you should utilize the next syntax too:
[ -d “DIR” ] && echo “sure” || echo “noop”

Getting assist

Learn bash shell man web page by typing the next man command or go to on-line right here:
man bash
assist [
assist [[
man take a look at

Conclusion

This web page defined varied instructions that can be utilized to verify if a listing exists or not, inside a shell script working on Linux or Unix-like programs.

Posted by: Vivek Gite

The writer is the creator of nixCraft and a seasoned sysadmin, DevOps engineer, and a coach for the Linux working system/Unix shell scripting. Get the newest tutorials on SysAdmin, Linux/Unix and open supply subjects by way of RSS/XML feed or weekly e mail e-newsletter.

Source link

Related Articles

Back to top button