79442488

Date: 2025-02-16 01:46:37
Score: 1.5
Natty:
Report link

This question might be ancient, but it's still pertinent. The answer for this is "it depends".

Can 'myfile.txt' be anywhere on the system? Or is it in a fixed structure?

$string=`ls /abc/def/*/*/*/*/myfile.txt`;

This only works if myfile.txt is exactly 4 directories deep under 'def', and there's another problem with it. Perl is platform-independent, unless you make it not be platform-independent.

This (using 'ls') won't work on Windows (unless you have Unix tools of some sort installed).

Stay in Perl and use glob or better yet bsd_glob.

use File::Glob 'bsd_glob';
my ($full_path) = bsd_glob('/abc/def/*/*/*/*/myfile.txt');

But this will be just as slow as using 'ls'.

BUT if myfile.txt can be at any directory level, you need to use File::Find. If the performance is too slow, then you need to use operating system tools like 'find', but keep in mind these tools (find, ls etc) don't work on Windows (unless you have Cygwin or something).


However, this took very long time

This is because there can be a HUGE number of files and directories scanned by the 'ls' command, depending on what's in abc/def/*, and what's in each directory under that, and each directory under that, etc etc.

Reasons:
  • RegEx Blacklisted phrase (1.5): fixed structure?
  • Long answer (-1):
  • Has code block (-0.5):
  • Contains question mark (0.5):
  • Low reputation (1):
Posted by: BitDreamer