Web Hosting Talk







View Full Version : Checking if a folder exists in Unix


azizny
09-18-2004, 07:03 PM
How would I got about that.

I know that I can use the ls file.s > /dev/null to check is a file exists or not. Can I do the same for folders?

Peace,

Changeling
09-18-2004, 09:17 PM
Yes, as long as it exists ls will return the same exit value.

If you're writing a Bash script you may consider using the following syntax to determine if a file or directory exists:
[ -e file ]

if you want to ensure that it's actually a file and not a directory then you can use:
[ -f file ]

if you want to ensure that it's actually a directory and not a file then you can use:
[ -d directory ]

I dont know whether these constructs are the same in other shells, but each shell should have its equivalent... assuming you're writing a shell script in the first place. :)

Brian

fraudgate
09-19-2004, 12:08 AM
Take a look at: http://us2.php.net/manual/en/function.is-dir.php

That's what you're looking for.

azizny
09-19-2004, 08:58 AM
Originally posted by fraudgate
Take a look at: http://us2.php.net/manual/en/function.is-dir.php

That's what you're looking for.

Thats PHP not unix!

Thanks anway..


Thank you Changeling for your help I will try it ASAP

Peace,

inimino
09-19-2004, 09:07 AM
The [ ] notation is a shortcut for the "test" shell builtin. "help test" should give you the details, also "man bash"