1752590 Members
2914 Online
108788 Solutions
New Discussion юеВ

Directory check

 
SOLVED
Go to solution
Unix or Linux?
Frequent Advisor

Directory check

How do I check for the existence of a directory?

Basically what I would like to do is the user inputs a name, then a script checks whether the directory exist. If it does not exist then create the directory and subdirectories. Also chmod -R 777. The script should be able to check this from any directory that it is run from.
8 REPLIES 8
Patrick Wallek
Honored Contributor

Re: Directory check

Something like this:

#!/usr/bin/sh
echo "Enter Dir Name: "
read DIRNAME
if [[ -d ${DIRNAME} ]] ; then
echo "Dir Exists"
else
echo "Dir does not exist"
mkdir ${DIRNAME}
chmod 770 ${DIRNAME}
fi

You notice I didnot chmod 777. 777 are VERY VERY VERY BAD permissions. That means that ANYBODY on the server can royall screw up anything in that directory.
Yang Qin_1
Honored Contributor

Re: Directory check

First of all, to grant directories with 777 is really not secure and you use at least 775?

Secondly, user input must contain full path otherwise you won't know where to start with

#!/usr/bin/ksh

ls -l $1
if [ $? -eq 0 ] ; then
echo $1 " exists"
else
mkdir -p $1
chmod -R 777 $1
echo $1 " has been created"
fi

exit

call it dirchk.sh

user need to run it like "dirchk.sh /aaa/bbb/ccc"
A. Clay Stephenson
Acclaimed Contributor

Re: Directory check

It's rather simple:

#!/usr/bin/sh

typeset DNAME=""
typeset STAT=255
echo "Enter directory name: \c"
read DNAME
if [ -e "${DNAME}" ]
then
echo "${DNAME} already exists.] >&2
else
mkdir "${DNAME}"
STAT=${?}
if [ ${STAT} -ne 0 ]
then
echo "Cannot mkdir ${DNAME}; status ${STAT}." >72
fi
exit ${STAT}

I have no idea what the subdirectories should be. It should be obvious where your chmod goes but setting mode to 777 should be done only in extremely rare cases; it is a huge security hole.
If it ain't broke, I can fix that.
Peter Nikitka
Honored Contributor

Re: Directory check

Hi,

if you use

umask 0
mkdir -p -m 777 dirname

you will get all you need (whether it makes sense to have world writable directories or not).
In this form you will
- not get an error message for existing directories
- an error message if the creation failes
- the exit status will be accordingly

mfG Peter
The Universe is a pretty big place, it's bigger than anything anyone has ever dreamed of before. So if it's just us, seems like an awful waste of space, right? Jodie Foster in "Contact"
Unix or Linux?
Frequent Advisor

Re: Directory check

Another small issue: I need the directory to be created all in lowercase. So if the user enters DIR123 the directory will be created as dir123 if the directory does not already exist.

I have tried using the tr command to no success as follows.

LUsr=`echo $DIRNAME | tr'[:upper:]''[:lower:]'`

What am I doing wrong here?


I also tried the following:

LUsr=`nawk '{print tolower($DIRNAME)}'`
Sandman!
Honored Contributor
Solution

Re: Directory check

Instead use the commands below...

# echo $DIRNAME | tr [:upper:] [:lower:]

OR

LUsr=`echo $DIRNAME | nawk '{print tolower($0)}'`

cheers!
James R. Ferguson
Acclaimed Contributor

Re: Directory check

Hi:

If you are collecting your data interactively, use 'typeset -l' to lowercase your input:

# typeset -l NAME
# read NAME
# echo ${NAME}

Regards!

...JRF...
Unix or Linux?
Frequent Advisor

Re: Directory check

thanks