8

I'm aware you can test if a file is a directory using:

if(-d $filename)

but how can you test if it's not a directory?

sj755
  • 3,944
  • 14
  • 59
  • 79
  • 2
    possible duplicate of [How do I distinguish a file from a directory in Perl?](http://stackoverflow.com/questions/206320/how-do-i-distinguish-a-file-from-a-directory-in-perl) – Greg Hewgill Oct 11 '11 at 03:32
  • 1
    The canonical Stack Overflow question is *[How do I distinguish a file from a directory in Perl?](http://stackoverflow.com/questions/206320)*. – Peter Mortensen Sep 04 '13 at 07:30

4 Answers4

16

Have you thought of trying the following?

if (! -d $filename) ...

The result of -d is, after all, something that can be treated as a boolean value, hence the logical not operator ! will work fine.

And, if you're after something more specific than "not a directory", see here. There are quite a few things that aren't directories that it sometimes doesn't make sense to treat as regular files.

Keep in mind that's assuming you've already validated that the filename exists. If you do a ! -d on a non-existent filename, it will return true.

It's a philosophical matter as to whether you consider a non-existent thing to be "not a directory" but, if you want to ensure it exists as well, you can use ! -d in conjunction with -e, something like:

if ((-e $filename) && (! -d $filename)) ...
paxdiablo
  • 854,327
  • 234
  • 1,573
  • 1,953
  • 3
    I just couldn't get to 30 characters fast enough ... I think the question actually stunned me. – Brian Roach Oct 11 '11 at 02:42
  • 2
    Please note: depending on how you parse OP's need, this answer may be wrong. The way I parsed it, he wants to know whether something is a file BUT not a directory - this answer would return true even for something that's NOT a file in the first place (e.g. `!-d NO_SUCH_FILE` returns true). – DVK Oct 11 '11 at 02:55
5

To test if something is a file, BUT not a directory, you need to simply combine the 2 tests (there's no single test):

if (-e $file && !-d $file) { # a file but not a directory }

Please note that:

  • With all due respect, paxdiablo's answer is wrong for the way the question is worded ( pure !-d does not work for what the user asked as it checks if a random thing/string is not a directory, NOT whether something is a file which is not a directory). E.g. !-d NO_SUCH_FILE returns true.

  • Greg's answer may be correct depending entirely on whether the original user meant to include non-plain files (e.g. symbolic links, named pipes, etc..) in their definition of "file which is not a directory". If they meant to include all those special "files", Greg's answer is also wrong as "-f" excludes them (as Greg wisely noted in his answer in the first place).

DVK
  • 126,886
  • 32
  • 213
  • 327
3

In the spirit of TIMTOWTDI:

unless (-d $filename)
Ilmari Karonen
  • 49,047
  • 9
  • 93
  • 153
2

The -f operator tests to see whether something is a "regular file". This excludes directories, but also excludes such things as pipes and device nodes.

if (-f $filename) { ...
Greg Hewgill
  • 951,095
  • 183
  • 1,149
  • 1,285