简体   繁体   中英

How to list all the files using shell script present in a specific folder under all the directories those are present in the root directory?

I want to list all the files that are present in a specific folder under all the present directories present in my root directory.
Say my root directory is . and there multiples folders like a1,b1,c1........a2,b2 are present.
Now in all those folders there is a specific folder which is source/ so that will be like

a1/source/
b1/source/
.
.
a2/source/
b2/source/

where all the necessary files are present. Can I list all the files in a file that is in the . directory by a shell script source file along with their absolute path?

StackOverflow isn't really a "write my code for me" service. But this is really simple in Perl.

Assuming that the source directories only ever appear at the same level:

my @files = glob '*/source/*';

In Perl you could use File::Find :

use feature qw(say);
use strict;
use warnings;

use File::Basename qw(basename);
use File::Find;

find( \&wanted, '.') ;

sub wanted {
    my $name = $_;    
    my $parent_dir = basename( $File::Find::dir );
    if ( (-f $name) && ( $parent_dir eq "source") ) {
        say $File::Find::name;
    }
}

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