LinuxQuestions.org
Latest LQ Deal: Linux Power User Bundle
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 03-02-2007, 02:55 AM   #1
Ian D
Member
 
Registered: Aug 2005
Location: Solihull, UK
Distribution: Fedora 9
Posts: 127

Rep: Reputation: 15
Testing for a folder


I want to write a BASH script which can detect if a folder exists on the disk.

What command do I use?

Thanks for your help.

Ian
 
Old 03-02-2007, 03:29 AM   #2
acid_kewpie
Moderator
 
Registered: Jun 2001
Location: UK
Distribution: Gentoo, RHEL, Fedora, Centos
Posts: 43,417

Rep: Reputation: 1974Reputation: 1974Reputation: 1974Reputation: 1974Reputation: 1974Reputation: 1974Reputation: 1974Reputation: 1974Reputation: 1974Reputation: 1974Reputation: 1974
if [ -d /where/ever ]
...
fi
 
Old 03-02-2007, 11:49 AM   #3
Ian D
Member
 
Registered: Aug 2005
Location: Solihull, UK
Distribution: Fedora 9
Posts: 127

Original Poster
Rep: Reputation: 15
Thanks, That gets me going.

Actually, I need to mkdir if the folder does not exist (which I realize is not what I asked for) and I find that I have to say
if [ -d $folder ]; then
nothing=1
else
mkdir $folder
fi

I know that you will be able to tell me a more elegant way to do this than having a dummy assignment statement.

Ian
 
Old 03-02-2007, 12:59 PM   #4
acid_kewpie
Moderator
 
Registered: Jun 2001
Location: UK
Distribution: Gentoo, RHEL, Fedora, Centos
Posts: 43,417

Rep: Reputation: 1974Reputation: 1974Reputation: 1974Reputation: 1974Reputation: 1974Reputation: 1974Reputation: 1974Reputation: 1974Reputation: 1974Reputation: 1974Reputation: 1974
[ -d /where/ever ] || mkdir /where/ever
 
Old 03-02-2007, 01:17 PM   #5
tuxrules
Senior Member
 
Registered: Jun 2004
Location: Chicago
Distribution: Slackware64 -current
Posts: 1,144

Rep: Reputation: 56
A slightly concise solution...

Code:
if [ ! -d /what/ever] ; then
mkdir /what/ever
fi
 
Old 03-02-2007, 03:23 PM   #6
acid_kewpie
Moderator
 
Registered: Jun 2001
Location: UK
Distribution: Gentoo, RHEL, Fedora, Centos
Posts: 43,417

Rep: Reputation: 1974Reputation: 1974Reputation: 1974Reputation: 1974Reputation: 1974Reputation: 1974Reputation: 1974Reputation: 1974Reputation: 1974Reputation: 1974Reputation: 1974
mine's conciserer. i could say [ ! -d /where/ever ] && mkdir /where/ever but that uses two more characters!
 
Old 03-02-2007, 04:06 PM   #7
Ian D
Member
 
Registered: Aug 2005
Location: Solihull, UK
Distribution: Fedora 9
Posts: 127

Original Poster
Rep: Reputation: 15
Great!

Elegant!

Works!
 
  


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
I can not open the user folder in home and write in a shared folder jorge_ivan Linux - General 8 08-02-2006 12:28 PM
Sarge-testing To Etch-testing? SMurf7 Debian 3 02-21-2006 11:59 PM
Folder Max Size and Limiting SSH access to home folder. Mefistofeles Linux - General 4 11-26-2005 03:09 PM
Setup as getting debian testing files from ftp - will it stay with testing BrianHenderson Debian 2 09-02-2004 07:06 PM
how can i default the max folder file size when it create inside a folder antony_csf Linux - Software 1 06-17-2004 03:26 AM


All times are GMT -5. The time now is 10:38 PM.

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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration