HJ has asked for the wisdom of the Perl Monks concerning the following question:

Hi all,
coming back to seek help regarding an issue. I am trying to search through a particular disk ( that has say 'n' directories and each directory has a sub-directory 'X') to spot the 'X' directory.
On spotting the X under each directory, I want to display the entire path.

Eg, If Disk A has a hierarchy as below:
Main disk: /a/b/c/Disk_A
Sub directories
/a/b/c/Disk_A/dir1/dirw/X/l
/a/b/c/Disk_A/dir2/X
/a/b/c/Disk_A/dir3/...../X
The o/p should be :
/a/b/c/Disk_A/dir1/dirw/
/a/b/c/Disk_A/dir2/
/a/b/c/Disk_A/dir3/...../


The code I tried writing to achieve the above is
#!/usr/bin/perl -w use strict; use warnings; my @findX= `find /a/b/c/Disk_A/ -name 'X'`; print @findX;

I want to do this without using Find:Files module, since mine's a suse machine. Wanted to do this just unix system calls.
Any help will be appreciated. Thanks

Replies are listed 'Best First'.
Re: Find a particular directory in a directory
by Eliya (Vicar) on Dec 15, 2011 at 23:23 UTC

    The respective (GNU) find command would be:

    $ find /a/b/c/Disk_A -type d -name X -printf "%h/\n"

    (%h is the placeholder for the path excluding the last element, i.e. without the X.)

Re: Find a particular directory in a directory
by ww (Archbishop) on Dec 16, 2011 at 02:10 UTC
    Then why not do it from *nix; that is, from the command line, with "unix system calls" rather than asking the question at a site focused on Perl?

    Unless your intent is to bone up on system, exec and backticks, your rationale for not using the best tool in your toolbox is pretty feeble; arguably, as feeble as the frequent 'plaint that 'I can't install modules..." and similar.

      More feeble. I have worked at places where you got the black bag if you installed anything yourself. Everything had to go through 'official channels' no matter how trivial.

      J.C.

Re: Find a particular directory in a directory
by Anonymous Monk on Dec 15, 2011 at 23:08 UTC
    I don't know how `find` works, but File::Find is a core perl module, it comes with every perl 5, even on a suse machine, since 1994-Oct-17
Re: Find a particular directory in a directory
by TJPride (Pilgrim) on Dec 16, 2011 at 16:14 UTC
    use strict; use warnings; my (@dirs, $find, $dir, $file); push @dirs, '/'; ### Assign starting directory $find = 'X'; ### Directory or file to find while ($dir = shift @dirs) { next if !opendir(DIR, $dir); while ($file = readdir(DIR)) { next if $file =~ /^\./; print "$dir\n" if $file eq $find; push @dirs, "$dir$file/" if -d "$dir$file"; } }