How to find the sub-directories using find












0















I'm using this below command to get the sub-directories inside an array @handoff of a perl code.



chomp(@handoff = `find * -maxdepth 0 -type d -name "18????_????" | sort -u | tail -2`);


I'm getting the error as



find: unknown predicate `-lrt'


If I try the same command in terminal directly, I'm able to get the sub-directories. Please suggest me some solution.










share|improve this question




















  • 2





    Is there a file called -lrt in the filesystem?

    – mob
    Nov 19 '18 at 17:43











  • I'm not sure how to check that in the filesystem.

    – Satish Sajjanar
    Nov 19 '18 at 17:49






  • 2





    Don't use find for this at all; just use the glob function: @handoff = glob("18????_????"/).

    – chepner
    Nov 19 '18 at 17:51











  • It's caused by find * and a file named -lrt. Maybe find . -maxdepth 1?

    – James Brown
    Nov 19 '18 at 18:23
















0















I'm using this below command to get the sub-directories inside an array @handoff of a perl code.



chomp(@handoff = `find * -maxdepth 0 -type d -name "18????_????" | sort -u | tail -2`);


I'm getting the error as



find: unknown predicate `-lrt'


If I try the same command in terminal directly, I'm able to get the sub-directories. Please suggest me some solution.










share|improve this question




















  • 2





    Is there a file called -lrt in the filesystem?

    – mob
    Nov 19 '18 at 17:43











  • I'm not sure how to check that in the filesystem.

    – Satish Sajjanar
    Nov 19 '18 at 17:49






  • 2





    Don't use find for this at all; just use the glob function: @handoff = glob("18????_????"/).

    – chepner
    Nov 19 '18 at 17:51











  • It's caused by find * and a file named -lrt. Maybe find . -maxdepth 1?

    – James Brown
    Nov 19 '18 at 18:23














0












0








0








I'm using this below command to get the sub-directories inside an array @handoff of a perl code.



chomp(@handoff = `find * -maxdepth 0 -type d -name "18????_????" | sort -u | tail -2`);


I'm getting the error as



find: unknown predicate `-lrt'


If I try the same command in terminal directly, I'm able to get the sub-directories. Please suggest me some solution.










share|improve this question
















I'm using this below command to get the sub-directories inside an array @handoff of a perl code.



chomp(@handoff = `find * -maxdepth 0 -type d -name "18????_????" | sort -u | tail -2`);


I'm getting the error as



find: unknown predicate `-lrt'


If I try the same command in terminal directly, I'm able to get the sub-directories. Please suggest me some solution.







bash shell perl csh tcsh






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 19 '18 at 17:56







Satish Sajjanar

















asked Nov 19 '18 at 17:37









Satish SajjanarSatish Sajjanar

113




113








  • 2





    Is there a file called -lrt in the filesystem?

    – mob
    Nov 19 '18 at 17:43











  • I'm not sure how to check that in the filesystem.

    – Satish Sajjanar
    Nov 19 '18 at 17:49






  • 2





    Don't use find for this at all; just use the glob function: @handoff = glob("18????_????"/).

    – chepner
    Nov 19 '18 at 17:51











  • It's caused by find * and a file named -lrt. Maybe find . -maxdepth 1?

    – James Brown
    Nov 19 '18 at 18:23














  • 2





    Is there a file called -lrt in the filesystem?

    – mob
    Nov 19 '18 at 17:43











  • I'm not sure how to check that in the filesystem.

    – Satish Sajjanar
    Nov 19 '18 at 17:49






  • 2





    Don't use find for this at all; just use the glob function: @handoff = glob("18????_????"/).

    – chepner
    Nov 19 '18 at 17:51











  • It's caused by find * and a file named -lrt. Maybe find . -maxdepth 1?

    – James Brown
    Nov 19 '18 at 18:23








2




2





Is there a file called -lrt in the filesystem?

– mob
Nov 19 '18 at 17:43





Is there a file called -lrt in the filesystem?

– mob
Nov 19 '18 at 17:43













I'm not sure how to check that in the filesystem.

– Satish Sajjanar
Nov 19 '18 at 17:49





I'm not sure how to check that in the filesystem.

– Satish Sajjanar
Nov 19 '18 at 17:49




2




2





Don't use find for this at all; just use the glob function: @handoff = glob("18????_????"/).

– chepner
Nov 19 '18 at 17:51





Don't use find for this at all; just use the glob function: @handoff = glob("18????_????"/).

– chepner
Nov 19 '18 at 17:51













It's caused by find * and a file named -lrt. Maybe find . -maxdepth 1?

– James Brown
Nov 19 '18 at 18:23





It's caused by find * and a file named -lrt. Maybe find . -maxdepth 1?

– James Brown
Nov 19 '18 at 18:23












2 Answers
2






active

oldest

votes


















2














No need to call an external program to find sub-directories:



opendir(my $dh, '.') || die "Can't opendir '.': $!";
my @handoff = grep { /^18.{4}_.{4}$/ && -d $_ } readdir($dh);
closedir $dh;
print join(' ', @handoff), "n"





share|improve this answer
























  • I would put the -d check before the regex check. That way, you're only doing the regex check on directories only, as opposed to running it against all files and directories.

    – stevieb
    Nov 19 '18 at 19:24






  • 2





    @stevieb Flip side: Filename is already in memory for the regex. -d requires a stat call. Time to break out the benchmarks....

    – tjd
    Nov 19 '18 at 20:00



















0














find expects the path to search as first argument, hence :



    find * -maxdepth 0 -type d -name "18????_????" | sort -u | tail -2


should be :



    find . -maxdepth 1 -type d -name "18????_????" | sort -u | tail -2


(assuming that you want to search the current path - else replace the . with the path to search).



But bottom line, as you are using perl already, why use an external command like find ?



Here is another solution using module Path::Iterator::Rule.



use Path::Iterator::Rule;
my @handoffs = Path::Iterator::Rule
->new
->directory # only directories (not files)
->max_depth(1) # do not recurse
->name("18????_????") # match directory name (glob or regex)
->all(".") # search the current path
;





share|improve this answer


























  • Hmm, that looks nifty. Unfortunately Path::... aren't core modules, so File::Find might be a better option.

    – Stefan Becker
    Nov 20 '18 at 6:06











  • Isn't the use Path::Tiny superfluous in your example?

    – Stefan Becker
    Nov 20 '18 at 6:08











  • @StefanBecker : thanks, removed the use of Path::Tiny

    – GMB
    Nov 20 '18 at 20:23











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53379953%2fhow-to-find-the-sub-directories-using-find%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























2 Answers
2






active

oldest

votes








2 Answers
2






active

oldest

votes









active

oldest

votes






active

oldest

votes









2














No need to call an external program to find sub-directories:



opendir(my $dh, '.') || die "Can't opendir '.': $!";
my @handoff = grep { /^18.{4}_.{4}$/ && -d $_ } readdir($dh);
closedir $dh;
print join(' ', @handoff), "n"





share|improve this answer
























  • I would put the -d check before the regex check. That way, you're only doing the regex check on directories only, as opposed to running it against all files and directories.

    – stevieb
    Nov 19 '18 at 19:24






  • 2





    @stevieb Flip side: Filename is already in memory for the regex. -d requires a stat call. Time to break out the benchmarks....

    – tjd
    Nov 19 '18 at 20:00
















2














No need to call an external program to find sub-directories:



opendir(my $dh, '.') || die "Can't opendir '.': $!";
my @handoff = grep { /^18.{4}_.{4}$/ && -d $_ } readdir($dh);
closedir $dh;
print join(' ', @handoff), "n"





share|improve this answer
























  • I would put the -d check before the regex check. That way, you're only doing the regex check on directories only, as opposed to running it against all files and directories.

    – stevieb
    Nov 19 '18 at 19:24






  • 2





    @stevieb Flip side: Filename is already in memory for the regex. -d requires a stat call. Time to break out the benchmarks....

    – tjd
    Nov 19 '18 at 20:00














2












2








2







No need to call an external program to find sub-directories:



opendir(my $dh, '.') || die "Can't opendir '.': $!";
my @handoff = grep { /^18.{4}_.{4}$/ && -d $_ } readdir($dh);
closedir $dh;
print join(' ', @handoff), "n"





share|improve this answer













No need to call an external program to find sub-directories:



opendir(my $dh, '.') || die "Can't opendir '.': $!";
my @handoff = grep { /^18.{4}_.{4}$/ && -d $_ } readdir($dh);
closedir $dh;
print join(' ', @handoff), "n"






share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 19 '18 at 18:23









Stefan BeckerStefan Becker

2,861823




2,861823













  • I would put the -d check before the regex check. That way, you're only doing the regex check on directories only, as opposed to running it against all files and directories.

    – stevieb
    Nov 19 '18 at 19:24






  • 2





    @stevieb Flip side: Filename is already in memory for the regex. -d requires a stat call. Time to break out the benchmarks....

    – tjd
    Nov 19 '18 at 20:00



















  • I would put the -d check before the regex check. That way, you're only doing the regex check on directories only, as opposed to running it against all files and directories.

    – stevieb
    Nov 19 '18 at 19:24






  • 2





    @stevieb Flip side: Filename is already in memory for the regex. -d requires a stat call. Time to break out the benchmarks....

    – tjd
    Nov 19 '18 at 20:00

















I would put the -d check before the regex check. That way, you're only doing the regex check on directories only, as opposed to running it against all files and directories.

– stevieb
Nov 19 '18 at 19:24





I would put the -d check before the regex check. That way, you're only doing the regex check on directories only, as opposed to running it against all files and directories.

– stevieb
Nov 19 '18 at 19:24




2




2





@stevieb Flip side: Filename is already in memory for the regex. -d requires a stat call. Time to break out the benchmarks....

– tjd
Nov 19 '18 at 20:00





@stevieb Flip side: Filename is already in memory for the regex. -d requires a stat call. Time to break out the benchmarks....

– tjd
Nov 19 '18 at 20:00













0














find expects the path to search as first argument, hence :



    find * -maxdepth 0 -type d -name "18????_????" | sort -u | tail -2


should be :



    find . -maxdepth 1 -type d -name "18????_????" | sort -u | tail -2


(assuming that you want to search the current path - else replace the . with the path to search).



But bottom line, as you are using perl already, why use an external command like find ?



Here is another solution using module Path::Iterator::Rule.



use Path::Iterator::Rule;
my @handoffs = Path::Iterator::Rule
->new
->directory # only directories (not files)
->max_depth(1) # do not recurse
->name("18????_????") # match directory name (glob or regex)
->all(".") # search the current path
;





share|improve this answer


























  • Hmm, that looks nifty. Unfortunately Path::... aren't core modules, so File::Find might be a better option.

    – Stefan Becker
    Nov 20 '18 at 6:06











  • Isn't the use Path::Tiny superfluous in your example?

    – Stefan Becker
    Nov 20 '18 at 6:08











  • @StefanBecker : thanks, removed the use of Path::Tiny

    – GMB
    Nov 20 '18 at 20:23
















0














find expects the path to search as first argument, hence :



    find * -maxdepth 0 -type d -name "18????_????" | sort -u | tail -2


should be :



    find . -maxdepth 1 -type d -name "18????_????" | sort -u | tail -2


(assuming that you want to search the current path - else replace the . with the path to search).



But bottom line, as you are using perl already, why use an external command like find ?



Here is another solution using module Path::Iterator::Rule.



use Path::Iterator::Rule;
my @handoffs = Path::Iterator::Rule
->new
->directory # only directories (not files)
->max_depth(1) # do not recurse
->name("18????_????") # match directory name (glob or regex)
->all(".") # search the current path
;





share|improve this answer


























  • Hmm, that looks nifty. Unfortunately Path::... aren't core modules, so File::Find might be a better option.

    – Stefan Becker
    Nov 20 '18 at 6:06











  • Isn't the use Path::Tiny superfluous in your example?

    – Stefan Becker
    Nov 20 '18 at 6:08











  • @StefanBecker : thanks, removed the use of Path::Tiny

    – GMB
    Nov 20 '18 at 20:23














0












0








0







find expects the path to search as first argument, hence :



    find * -maxdepth 0 -type d -name "18????_????" | sort -u | tail -2


should be :



    find . -maxdepth 1 -type d -name "18????_????" | sort -u | tail -2


(assuming that you want to search the current path - else replace the . with the path to search).



But bottom line, as you are using perl already, why use an external command like find ?



Here is another solution using module Path::Iterator::Rule.



use Path::Iterator::Rule;
my @handoffs = Path::Iterator::Rule
->new
->directory # only directories (not files)
->max_depth(1) # do not recurse
->name("18????_????") # match directory name (glob or regex)
->all(".") # search the current path
;





share|improve this answer















find expects the path to search as first argument, hence :



    find * -maxdepth 0 -type d -name "18????_????" | sort -u | tail -2


should be :



    find . -maxdepth 1 -type d -name "18????_????" | sort -u | tail -2


(assuming that you want to search the current path - else replace the . with the path to search).



But bottom line, as you are using perl already, why use an external command like find ?



Here is another solution using module Path::Iterator::Rule.



use Path::Iterator::Rule;
my @handoffs = Path::Iterator::Rule
->new
->directory # only directories (not files)
->max_depth(1) # do not recurse
->name("18????_????") # match directory name (glob or regex)
->all(".") # search the current path
;






share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 20 '18 at 20:22

























answered Nov 19 '18 at 22:24









GMBGMB

13.4k2824




13.4k2824













  • Hmm, that looks nifty. Unfortunately Path::... aren't core modules, so File::Find might be a better option.

    – Stefan Becker
    Nov 20 '18 at 6:06











  • Isn't the use Path::Tiny superfluous in your example?

    – Stefan Becker
    Nov 20 '18 at 6:08











  • @StefanBecker : thanks, removed the use of Path::Tiny

    – GMB
    Nov 20 '18 at 20:23



















  • Hmm, that looks nifty. Unfortunately Path::... aren't core modules, so File::Find might be a better option.

    – Stefan Becker
    Nov 20 '18 at 6:06











  • Isn't the use Path::Tiny superfluous in your example?

    – Stefan Becker
    Nov 20 '18 at 6:08











  • @StefanBecker : thanks, removed the use of Path::Tiny

    – GMB
    Nov 20 '18 at 20:23

















Hmm, that looks nifty. Unfortunately Path::... aren't core modules, so File::Find might be a better option.

– Stefan Becker
Nov 20 '18 at 6:06





Hmm, that looks nifty. Unfortunately Path::... aren't core modules, so File::Find might be a better option.

– Stefan Becker
Nov 20 '18 at 6:06













Isn't the use Path::Tiny superfluous in your example?

– Stefan Becker
Nov 20 '18 at 6:08





Isn't the use Path::Tiny superfluous in your example?

– Stefan Becker
Nov 20 '18 at 6:08













@StefanBecker : thanks, removed the use of Path::Tiny

– GMB
Nov 20 '18 at 20:23





@StefanBecker : thanks, removed the use of Path::Tiny

– GMB
Nov 20 '18 at 20:23


















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53379953%2fhow-to-find-the-sub-directories-using-find%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







這個網誌中的熱門文章

Tangent Lines Diagram Along Smooth Curve

Yusuf al-Mu'taman ibn Hud

Zucchini