How to check if a file exists in Perl?

I have a relative path

$base_path = "input/myMock.TGZ"; 

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

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

+76
perl
Apr 08 2018-10-10T00:
source share
8 answers

Check if something exists on the given path using the -e file-test statement.

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

However, this test is probably wider than you expect. The above code will generate output if a simple file exists on this path, but it will also run for a directory, named pipe, symlink, or more exotic feature. See the documentation for more details .

Given the .TGZ extension in your question, it seems that you are expecting a simple file, not an alternative. The file-test -f statement asks if the path to a simple file leads.

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

The perlfunc documentation covers a long list of Perl file-test statements , which covers many situations that you will encounter in practice.

  • -r
    The file is readable using uid / gid.
  • -w
    The file is writable using an effective uid / gid.
  • -x
    The file is executable using an effective uid / gid.
  • -o
    File belongs to effective uid.
  • -r
    The file is read using real uid / gid.
  • -w
    The file is writable using real uid / gid.
  • -x
    The file is executed using the real uid / gid.
  • -o
    File belongs to real uid.
  • -e
    The file exists.
  • -z
    File has zero size (empty).
  • -s
    The file has a nonzero size (returns the size in bytes).
  • -f
    A file is a simple file.
  • -d
    A file is a directory.
  • -l
    The file is a symbolic link (false if symlinks arent supported by the file system).
  • -p
    A file is a named pipe (FIFO), or Filehandle is a pipe.
  • -s
    A file is a socket.
  • -b
    A file is a special block file.
  • -c
    The file is a special character file.
  • -t
    The descriptor file opens in tty.
  • -u
    The file has a setuid bit.
  • -g
    The file has a setgid bit.
  • -k
    The file has a sticky bit.
  • -t
    The file is an ASCII or UTF-8 text file (heuristic assumption).
  • -b
    The file is a binary file (opposite -t ).
  • -M
    Script start time minus file modification time, in days.
  • -A
    The same goes for access time.
  • -c
    The same for inode change time (Unix may differ for other platforms).
+122
Apr 08 '10 at 15:10
source share

Perhaps you need a variant of existence ... perldoc -f "-f"

  -X FILEHANDLE -X EXPR -X DIRHANDLE -XA 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. 
+29
Apr 08 '10 at 15:42
source share
 if (-e $base_path) { # code } 

-e is an existence operator in Perl.

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

+13
Apr 08 '10 at 15:10
source share

You can use: if(-e $base_path)

+12
Apr 08 2018-10-10T00:
source share

Using:

 if (-f $filePath) { # code } 

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

+8
May 05 '14 at 11:55
source share
 #!/usr/bin/perl -w $fileToLocate = '/whatever/path/for/file/you/are/searching/MyFile.txt'; if (-e $fileToLocate) { print "File is present"; } 
+5
Nov 19 '14 at 19:12
source share
 if(-e $base_path){print "Something";} 

will do the trick

+3
Jun 21 '16 at 17:59
source share

Use the code below. Here -f checks if this is a file or not:

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

and enjoy

0
Jun 28 '15 at 8:38
source share



All Articles