1

How to check whether a particular file exists in a particular directory in perl? Obvious method of checking that absolute directory name is prefix of absolute file name doesn't works as sometime absolute directory name is like /a/b/c/..

3 Answers 3

1

File::Find's find function could be used:

Program

#!/usr/bin/env perl                                                                                    

use strict;                                                                                            
use warnings;                                                                                          

use File::Find 'find';                                                                                 


# file 'foo' in path 'a/b/c'
my $file      = 'foo';                                                                                 
my $directory = 'a';                                                                                   

sub check_existance {                                                                              
    if ( -e $_ && $_ eq $file ) {                                                                      
        print "Found file '$_' in directory '$File::Find::dir'\n";                                     
    }                                                                                                  
}                                                                                                      

find( \&check_existance, $directory );

Output

Found file 'foo' in directory 'a/b/c'                                                                  
Sign up to request clarification or add additional context in comments.

2 Comments

what if there are symbolic links in a directory, then would it search in them also?
abc15: The above program does not follow symbolic links. However, you could pass a follow option to the find function. Refer the documentation for File::Find: perldoc.perl.org/File/Find.html
1

If you don't know the exact path you could use find2perl. It generates File::Find::find() code for you. To execute the command immediately:

$ find2perl /path/to/dir -name filename.txt -exec echo exists {} | perl

Code generated by find2perl

#! /usr/bin/perl -w
    eval 'exec /usr/bin/perl -S $0 ${1+"$@"}'
        if 0; #$running_under_some_shell

use strict;
use File::Find ();

# Set the variable $File::Find::dont_use_nlink if you're using AFS,
# since AFS cheats.

# for the convenience of &wanted calls, including -eval statements:
use vars qw/*name *dir *prune/;
*name   = *File::Find::name;
*dir    = *File::Find::dir;
*prune  = *File::Find::prune;

sub wanted;
sub doexec ($@);

use Cwd ();
my $cwd = Cwd::cwd();

# Traverse desired filesystems
File::Find::find({wanted => \&wanted}, '/path/to/dir');
exit;

sub wanted {
    /^filename\.txt\z/s &&
    doexec(0, 'echo','exists','{}');
}

sub doexec ($@) {
    my $ok = shift;
    my @command = @_; # copy so we don't try to s/// aliases to constants
    for my $word (@command)
        { $word =~ s#{}#$name#g }
    if ($ok) {
        my $old = select(STDOUT);
        $| = 1;
        print "@command";
        select($old);
        return 0 unless <STDIN> =~ /^y/;
    }
    chdir $cwd; #sigh
    system @command;
    chdir $File::Find::dir;
    return !$?;
}

Comments

0
if (-e "/path/to/file/filename.txt") {
   print("file exists");
}

?

2 Comments

It will not work as I don't know the path of file. There may be another directory in known directory.
Ah, you mean look for it in any subdirectories contained within the known directory? There's a File::Find module in CPAN which does this for you: perldoc.perl.org/File/Find.html

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.