How to find the sub-directories using find
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
add a comment |
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
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 usefind
for this at all; just use theglob
function:@handoff = glob("18????_????"/)
.
– chepner
Nov 19 '18 at 17:51
It's caused byfind *
and a file named-lrt
. Maybefind . -maxdepth 1
?
– James Brown
Nov 19 '18 at 18:23
add a comment |
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
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
bash shell perl csh tcsh
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 usefind
for this at all; just use theglob
function:@handoff = glob("18????_????"/)
.
– chepner
Nov 19 '18 at 17:51
It's caused byfind *
and a file named-lrt
. Maybefind . -maxdepth 1
?
– James Brown
Nov 19 '18 at 18:23
add a comment |
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 usefind
for this at all; just use theglob
function:@handoff = glob("18????_????"/)
.
– chepner
Nov 19 '18 at 17:51
It's caused byfind *
and a file named-lrt
. Maybefind . -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
add a comment |
2 Answers
2
active
oldest
votes
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"
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
add a comment |
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
;
Hmm, that looks nifty. UnfortunatelyPath::...
aren't core modules, soFile::Find
might be a better option.
– Stefan Becker
Nov 20 '18 at 6:06
Isn't theuse 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
add a comment |
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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"
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
add a comment |
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"
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
add a comment |
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"
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"
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
add a comment |
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
add a comment |
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
;
Hmm, that looks nifty. UnfortunatelyPath::...
aren't core modules, soFile::Find
might be a better option.
– Stefan Becker
Nov 20 '18 at 6:06
Isn't theuse 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
add a comment |
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
;
Hmm, that looks nifty. UnfortunatelyPath::...
aren't core modules, soFile::Find
might be a better option.
– Stefan Becker
Nov 20 '18 at 6:06
Isn't theuse 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
add a comment |
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
;
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
;
edited Nov 20 '18 at 20:22
answered Nov 19 '18 at 22:24
GMBGMB
13.4k2824
13.4k2824
Hmm, that looks nifty. UnfortunatelyPath::...
aren't core modules, soFile::Find
might be a better option.
– Stefan Becker
Nov 20 '18 at 6:06
Isn't theuse 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
add a comment |
Hmm, that looks nifty. UnfortunatelyPath::...
aren't core modules, soFile::Find
might be a better option.
– Stefan Becker
Nov 20 '18 at 6:06
Isn't theuse 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
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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 theglob
function:@handoff = glob("18????_????"/)
.– chepner
Nov 19 '18 at 17:51
It's caused by
find *
and a file named-lrt
. Maybefind . -maxdepth 1
?– James Brown
Nov 19 '18 at 18:23