LinuxQuestions.org
Register a domain and help support LQ
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices

Reply
 
Search this Thread
Old 05-10-2010, 11:29 AM   #1
Drigo
Member
 
Registered: Jun 2009
Posts: 104

Rep: Reputation: 15
Thumbs up Check if directory DOES NOT exist in BASH


Well...anytime i tried to create a if else statements or either an else statement I get a message saying that the directory exists (mkdir: cannot create directory `./MAY2010': File exists
). I am running this in a crontab file and I dont want any output telling me that the directory exists. I just want to check if the directory doesnt exist, create one else do nothing (not even a message telling me that the directory exists).

Here is what I tried:
1. First try:

if [ -d $DIR ] ; then
echo " " #NOW I WANT TO GET RID OF THIS LINE AND SIMPLY
#DO NOTHING BUT I CANT just dont put nothign, it
#gives em some error when
#running the bash.
else
mkdir $DIR
fi



2nd try:

if ! [ -d $DIR ]; then
mkdir $DIR
fi

#Still tells me that the directory is created as output:
mkdir: cannot create directory `./MAY2010': File exists


3rd try:

if [ ! -d $DIR]; then
mkdir $DIR
fi

#Still the same message.

Any suggestions?
thanks!
 
Old 05-10-2010, 11:38 AM   #2
yooy
Senior Member
 
Registered: Dec 2009
Posts: 1,216

Rep: Reputation: 139Reputation: 139
maybe you should try exit..
or command false (that does nothing)

however your 1st try did't gave me any error
 
Old 05-10-2010, 11:38 AM   #3
grail
Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 7,689

Rep: Reputation: 1987Reputation: 1987Reputation: 1987Reputation: 1987Reputation: 1987Reputation: 1987Reputation: 1987Reputation: 1987Reputation: 1987Reputation: 1987Reputation: 1987
Try using double brackets:
Code:
if [[ ! -d $DIR ]]; then
    <blah>
fi

#or

[[ -d $DIR ]] || <blah>

#or if you really prefer (as you think it is clearer) to use not

[[ ! -d $DIR ]] && <blah>
 
1 members found this post helpful.
Old 05-10-2010, 11:40 AM   #4
sploot
Member
 
Registered: Sep 2009
Location: Phoenix, AZ
Distribution: Gentoo, Debian, Ubuntu
Posts: 121

Rep: Reputation: 27
Your third try is the proper syntax. Try quoting your variable

Code:
if [ ! -d "${DIR}" ]; then
  mkdir ${DIR}
fi

Last edited by sploot; 05-10-2010 at 11:43 AM. Reason: fixed it.
 
Old 05-10-2010, 11:59 AM   #5
Drigo
Member
 
Registered: Jun 2009
Posts: 104

Original Poster
Rep: Reputation: 15
yooy is does not give me an error but it outputs whatever the echo shows... and again output: (mkdir: cannot create directory `./MAY2010': File exists
), which I dont want.

grail and sploot, none of your suggestions gets rid of the output:

mkdir: cannot create directory `./MAY2010': File exists



:S

Thanks for all your help!
 
Old 05-10-2010, 12:02 PM   #6
yooy
Senior Member
 
Registered: Dec 2009
Posts: 1,216

Rep: Reputation: 139Reputation: 139
try "false" as zero output command
 
Old 05-10-2010, 12:04 PM   #7
colucix
Moderator
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957
Use mkdir -p instead. It does not complain if directory already exists.
 
Old 05-10-2010, 12:11 PM   #8
Drigo
Member
 
Registered: Jun 2009
Posts: 104

Original Poster
Rep: Reputation: 15
colucix thats what I was looking for ! Thanks again to all
 
Old 05-10-2010, 08:22 PM   #9
grail
Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 7,689

Rep: Reputation: 1987Reputation: 1987Reputation: 1987Reputation: 1987Reputation: 1987Reputation: 1987Reputation: 1987Reputation: 1987Reputation: 1987Reputation: 1987Reputation: 1987
Well color me confused??
Firstly I am curious why you would want to create a directory if one already existed (or a file of the same name as implied by error message)?
Second, does this mean the test is inaccurate? (ie the if says only try to mkdir if directory does not exist or is the issue that also a test for file needs to be included)?

btw. not having a shot at you Drigo ... just curious
 
Old 05-02-2012, 04:50 PM   #10
Mike_V
Member
 
Registered: Apr 2009
Location: Boston MA
Distribution: CentOS 6.2 x86_64 GNU/Linux
Posts: 59

Rep: Reputation: 19
I like this one most:

Code:
[[ ! -d $DIR ]] && <blah>
thanks Grail!

-m

Last edited by Mike_V; 05-02-2012 at 04:51 PM.
 
Old 05-02-2012, 04:53 PM   #11
suicidaleggroll
Senior Member
 
Registered: Nov 2010
Location: Colorado
Distribution: OpenSUSE, CentOS
Posts: 3,199

Rep: Reputation: 1136Reputation: 1136Reputation: 1136Reputation: 1136Reputation: 1136Reputation: 1136Reputation: 1136Reputation: 1136Reputation: 1136
Quote:
Originally Posted by sploot View Post
Your third try is the proper syntax. Try quoting your variable

Code:
if [ ! -d "${DIR}" ]; then
  mkdir ${DIR}
fi
Quote:
Originally Posted by Drigo View Post
sploot, none of your suggestions gets rid of the output:

mkdir: cannot create directory `./MAY2010': File exists
That syntax has always worked without issue for me. I use it all the time.
Are you sure you don't have a file called MAY2010 there? The if will return false since the -d only tests for a directory of that name, and the mkdir will fail with that message since there's already a file with that name there.
 
Old 05-03-2012, 06:59 AM   #12
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950
Please use [code][/code] tags around your code and data, to preserve formatting and to improve readability. Please do not use quote tags, colors, or other fancy formatting.


Actually, all of the attempts posted in the OP should work just fine, provided the correct syntax is used (and there are no other file/directory conflicts, as discussed).

Code:
if [ -d "$dir"" ] ; then
	:
else
	mkdir "$dir"
fi


if ! [ -d "$dir" ]; then
	mkdir "$dir"
fi

if [ ! -d "$dir" ]; then
	mkdir "$dir"
fi

In the first example, we simply use any sub-command that does nothing on a successful match. The "true" placeholder command (":" is a synonym for it) is usually the recommended option. "false" can be used as well, but it might have a different effect if any subsequent command depends on reading the exit status of it. Even something like "echo -n" would do.

The second one works because placing a "!" in front of any command inverts the success/failure exit status of it. In this case you're reversing the status of the "test" command.

The third one is probably best though, as "!" is also the test command syntax for reversing the status of that individual match.


The important thing about all of them though is to remember that "test/[" is a command, just like any other, and so it's important to include spaces around each argument that comes after it. You also have to quote any variables and command substitutions, so that the expanded value doesn't get split into separate arguments from word-splitting and glob expansion.


See the Bash Pitfalls for details on common problems like this.
(I believe #4 is the main one, but for some reason the site currently isn't responding for me.)


As mentioned though, when using bash or ksh, it's usually recommended to use [[..]] for string/file tests. Since "[[" is a shell keyword, not a command, the shell can parse the line more safely, and doesn't do any word-splitting or expansion of variable contents.

In addition, when doing integer comparisons, you should use the ((..)) arithmetic evaluation brackets instead. In either case, avoid using the old [..] test unless you specifically need POSIX-style portability.

http://mywiki.wooledge.org/ArithmeticExpression
http://mywiki.wooledge.org/BashFAQ/031
http://wiki.bash-hackers.org/commands/classictest
http://wiki.bash-hackers.org/syntax/...nal_expression


And to give one final bit of advice, environment variables are generally all upper-case. So while not absolutely necessary, it's good practice to keep your own user variables in lower-case or mixed-case, to help differentiate them.

Last edited by David the H.; 05-03-2012 at 07:04 AM. Reason: minor fixes
 
Old 05-03-2012, 07:35 AM   #13
pan64
Guru
 
Registered: Mar 2012
Location: Hungary
Distribution: debian i686 (solaris)
Posts: 5,139

Rep: Reputation: 1363Reputation: 1363Reputation: 1363Reputation: 1363Reputation: 1363Reputation: 1363Reputation: 1363Reputation: 1363Reputation: 1363Reputation: 1363
try [[ -d "$DIR/." ]]

Last edited by pan64; 05-03-2012 at 07:37 AM.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Bash-Check If FS is Already Mounted On Directory fortezza Programming 15 04-27-2013 08:06 PM
how to check directory exist or not nagendrar Solaris / OpenSolaris 1 04-30-2010 03:06 AM
bash script to check how many files in directory guest Programming 8 02-01-2009 12:55 AM
check if directory exist jaepi Programming 6 05-10-2007 09:45 PM
How do I check for directory in bash script? nadavvin Programming 1 09-13-2006 05:03 PM


All times are GMT -5. The time now is 04:00 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration