[ale] existence of a file

Mike Fletcher fletch at phydeaux.org
Wed Apr 11 13:55:59 EDT 2007


Terry Bailey wrote:
> How do you test if a file does not exist?
>
> I thought it would be
>
> 	if (!(-e $file)
>
> but that doesn't seem to be working.
>   
Define "doesn't seem to be working" (and also what you're using; it 
looks like Perl, but . . .).

$ mkdir foozle
$ cd ./foozle
$ ls
$ touch exists
$ ls
exists
$ perl -le 'for my $file ( qw( exists nonexistent ) ) { print "$file 
exists" if -e $file }'
exists exists
$ perl -le 'for my $file ( qw( exists nonexistent ) ) { print "$file 
does not exist" if !-e $file }'
nonexistent does not exist
$


And keep in mind that explicitly testing for a files existence with -e 
can create a race condition that could lead to problems down the line.



More information about the Ale mailing list