Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

What would be the correct perl regex for detecting only Unix paths?

./dir1/dir2/file.ext - invalid
./dir1/dir2/ - valid
./dir1/dir2 - valid
share|improve this question
Why is the first path invalid? Dot is legal in directory name. – choroba May 16 '13 at 22:52
Those are all valid paths. Also multiple slashes like ///dir1/dir2//dir3 are valid. – Matt H May 16 '13 at 22:53
What are you really trying to do? Trying to detect directories with a regex is almost certainly the wrong solution. You are usually better off just testing to see if the path exists.… – Zoredache May 16 '13 at 23:15
@choroba and Matt H I was not aware of that. Good to know. Zoredache I was trying to use regex to omit unix paths for Google Search Appliance (GSA), since it accepts regex. I'll stick with simple specific paths then. – slashoofpez May 17 '13 at 17:08

There is none.

There's no way to distinguish whether a basename is a file or directory by looking at the path alone.

share|improve this answer

Do man perlfunc and look at the first set of functions under Alphabetical Listing of Perl Functions. There are a set of functions, beginning with -, that test what type of file/whatever a path refers to. The one you probably want is -d, which tests if a file refers to a directory.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.