简体   繁体   中英

How can Perl detect a file if I don't know the exact filepath?

I am trying to search for a file under specific path/location.

Sample Path - /var/log/<parent1>/<parent2>/<folder>/file

Now, there are two directories <parent1> and <parent2> as show in sample path. These two directories have some random name so their names can not be predicted. Except these two directory names, every part of path is known.

I tried doing something like this --

if ( -e $filePath ) {
    # Do something here
}

But it doesn't seem to work. So, how can search for the specific file?

Thanks!

Use a file glob:

</var/log/*/*/folder/file>

This does the same thing the shell does with a similar pattern.

Or you can use the glob function. perldoc -f glob for details.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM