LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
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, 10:29 AM   #1
Drigo
Member
 
Registered: May 2009
Posts: 111

Rep: Reputation: 17
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, 10:38 AM   #2
yooy
Senior Member
 
Registered: Dec 2009
Posts: 1,387

Rep: Reputation: 174Reputation: 174
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, 10:38 AM   #3
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,005

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
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, 10: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 10:43 AM. Reason: fixed it.
 
Old 05-10-2010, 10:59 AM   #5
Drigo
Member
 
Registered: May 2009
Posts: 111

Original Poster
Rep: Reputation: 17
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, 11:02 AM   #6
yooy
Senior Member
 
Registered: Dec 2009
Posts: 1,387

Rep: Reputation: 174Reputation: 174
try "false" as zero output command
 
Old 05-10-2010, 11:04 AM   #7
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
Use mkdir -p instead. It does not complain if directory already exists.
 
Old 05-10-2010, 11:11 AM   #8
Drigo
Member
 
Registered: May 2009
Posts: 111

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

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
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, 03: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 03:51 PM.
 
Old 05-02-2012, 03:53 PM   #11
suicidaleggroll
LQ Guru
 
Registered: Nov 2010
Location: Colorado
Distribution: OpenSUSE, CentOS
Posts: 5,573

Rep: Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142
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, 05:59 AM   #12
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
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 06:04 AM. Reason: minor fixes
 
Old 05-03-2012, 06:35 AM   #13
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,811

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

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


Reply



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 07:06 PM
how to check directory exist or not nagendrar Solaris / OpenSolaris 1 04-30-2010 02:06 AM
bash script to check how many files in directory guest Programming 8 01-31-2009 11:55 PM
check if directory exist jaepi Programming 6 05-10-2007 08:45 PM
How do I check for directory in bash script? nadavvin Programming 1 09-13-2006 04:03 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 07:53 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
Open Source Consulting | Domain Registration