How can I check if a file exists in Perl?

Perl

Perl Problem Overview


I have a relative path

   $base_path = "input/myMock.TGZ";

myMock.TGZ is the file name located in input folder. The filename can change. But the path is always stored in $base_path.

I need to check if the file exists in $base_path.

Perl Solutions


Solution 1 - Perl

Test whether something exists at given path using the -e file-test operator.

print "$base_path exists!\n" if -e $base_path;

However, this test is probably broader than you intend. The code above will generate output if a plain file exists at that path, but it will also fire for a directory, a named pipe, a symlink, or a more exotic possibility. See the documentation for details.

Given the extension of .TGZ in your question, it seems that you expect a plain file rather than the alternatives. The -f file-test operator asks whether a path leads to a plain file.

print "$base_path is a plain file!\n" if -f $base_path;

The perlfunc documentation covers the long list of Perl's file-test operators that covers many situations you will encounter in practice.

> * -r
> File is readable by effective uid/gid. > * -w
> File is writable by effective uid/gid. > * -x
> File is executable by effective uid/gid. > * -o
> File is owned by effective uid. > * -R
> File is readable by real uid/gid. > * -W
> File is writable by real uid/gid. > * -X
> File is executable by real uid/gid. > * -O
> File is owned by real uid. > * -e
> File exists. > * -z
> File has zero size (is empty). > * -s
> File has nonzero size (returns size in bytes). > * -f
> File is a plain file. > * -d
> File is a directory. > * -l
> File is a symbolic link (false if symlinks aren’t supported by the file system). > * -p
> File is a named pipe (FIFO), or Filehandle is a pipe. > * -S
> File is a socket. > * -b
> File is a block special file. > * -c
> File is a character special file. > * -t
> Filehandle is opened to a tty. > * -u
> File has setuid bit set. > * -g
> File has setgid bit set. > * -k
> File has sticky bit set. > * -T
> File is an ASCII or UTF-8 text file (heuristic guess). > * -B
> File is a “binary” file (opposite of -T). > * -M
> Script start time minus file modification time, in days. > * -A
> Same for access time. > * -C
> Same for inode change time (Unix, may differ for other platforms)

Solution 2 - Perl

You might want a variant of exists ... perldoc -f "-f"

      -X FILEHANDLE
       -X EXPR
       -X DIRHANDLE
       -X      A file test, where X is one of the letters listed below.  This unary operator takes one argument,
               either a filename, a filehandle, or a dirhandle, and tests the associated file to see if something is
               true about it.  If the argument is omitted, tests $_, except for "-t", which tests STDIN.  Unless
               otherwise documented, it returns 1 for true and '' for false, or the undefined value if the file
               doesn’t exist.  Despite the funny names, precedence is the same as any other named unary operator.
               The operator may be any of:

                   -r  File is readable by effective uid/gid.
                   -w  File is writable by effective uid/gid.
                   -x  File is executable by effective uid/gid.
                   -o  File is owned by effective uid.

                   -R  File is readable by real uid/gid.
                   -W  File is writable by real uid/gid.
                   -X  File is executable by real uid/gid.
                   -O  File is owned by real uid.

                   -e  File exists.
                   -z  File has zero size (is empty).
                   -s  File has nonzero size (returns size in bytes).

                   -f  File is a plain file.
                   -d  File is a directory.
                   -l  File is a symbolic link.
                   -p  File is a named pipe (FIFO), or Filehandle is a pipe.
                   -S  File is a socket.
                   -b  File is a block special file.
                   -c  File is a character special file.
                   -t  Filehandle is opened to a tty.

                   -u  File has setuid bit set.
                   -g  File has setgid bit set.
                   -k  File has sticky bit set.

                   -T  File is an ASCII text file (heuristic guess).
                   -B  File is a "binary" file (opposite of -T).

                   -M  Script start time minus file modification time, in days.

Solution 3 - Perl

if (-e $base_path)
{ 
 # code
}

-e is the 'existence' operator in Perl.

You can check permissions and other attributes using the code on this page.

Solution 4 - Perl

You can use: if(-e $base_path)

Solution 5 - Perl

Use:

if (-f $filePath)
{
  # code
}

-e returns true even if the file is a directory. -f will only return true if it's an actual file

Solution 6 - Perl

#!/usr/bin/perl -w

$fileToLocate = '/whatever/path/for/file/you/are/searching/MyFile.txt';
if (-e $fileToLocate) {
    print "File is present";
}

Solution 7 - Perl

if(-e $base_path)
{
    print "Something";
}

would do the trick.

Solution 8 - Perl

Use the below code. Here -f checks if it's a file or not:

print "File $base_path is exists!\n" if -f $base_path;

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionsuperstarView Question on Stackoverflow
Solution 1 - PerlGreg BaconView Answer on Stackoverflow
Solution 2 - PerlhpavcView Answer on Stackoverflow
Solution 3 - PerlmdmView Answer on Stackoverflow
Solution 4 - PerlKlaus Byskov PedersenView Answer on Stackoverflow
Solution 5 - PerlZain RizviView Answer on Stackoverflow
Solution 6 - PerlBalaji Boggaram RamanarayanView Answer on Stackoverflow
Solution 7 - Perluser5240420View Answer on Stackoverflow
Solution 8 - PerlNikhil RanjanView Answer on Stackoverflow