print all 3-character-sub-directories using shell script in AIX
In my system, there is a /Test
directory under which many directories are available. I want to print all directories with 3-character-name, in AIX system.
I got code for Gnu/Linux.
find /test -maxdepth 1 -type d | awk -F / 'length($NF) == 3' |awk -F / '{print $3} '
This is an AIX server.
ex : /test
directory contains sub directories
test1
AAA
BBB
Test2
test3
required output :
AAA
BBB
shell-script directory aix
add a comment |
In my system, there is a /Test
directory under which many directories are available. I want to print all directories with 3-character-name, in AIX system.
I got code for Gnu/Linux.
find /test -maxdepth 1 -type d | awk -F / 'length($NF) == 3' |awk -F / '{print $3} '
This is an AIX server.
ex : /test
directory contains sub directories
test1
AAA
BBB
Test2
test3
required output :
AAA
BBB
shell-script directory aix
could you please edit your question to make it a bit more clear ? Also did you installed the gnu tools on AIX ? or Are you using the basic tools of AIX ?
– Kiwy
Jan 3 at 10:08
What is wrong with your current solution?
– ctrl-alt-delor
Jan 3 at 10:21
i'm looking for unix command
– Kannan Manokaran
Jan 3 at 10:25
If any of the answers solved your problem, please accept it by clicking the checkmark next to it. Thank you!
– Jeff Schaller
Jan 6 at 14:35
add a comment |
In my system, there is a /Test
directory under which many directories are available. I want to print all directories with 3-character-name, in AIX system.
I got code for Gnu/Linux.
find /test -maxdepth 1 -type d | awk -F / 'length($NF) == 3' |awk -F / '{print $3} '
This is an AIX server.
ex : /test
directory contains sub directories
test1
AAA
BBB
Test2
test3
required output :
AAA
BBB
shell-script directory aix
In my system, there is a /Test
directory under which many directories are available. I want to print all directories with 3-character-name, in AIX system.
I got code for Gnu/Linux.
find /test -maxdepth 1 -type d | awk -F / 'length($NF) == 3' |awk -F / '{print $3} '
This is an AIX server.
ex : /test
directory contains sub directories
test1
AAA
BBB
Test2
test3
required output :
AAA
BBB
shell-script directory aix
shell-script directory aix
edited Jan 3 at 12:19
Jeff Schaller
39.3k1054125
39.3k1054125
asked Jan 3 at 10:04
Kannan ManokaranKannan Manokaran
113
113
could you please edit your question to make it a bit more clear ? Also did you installed the gnu tools on AIX ? or Are you using the basic tools of AIX ?
– Kiwy
Jan 3 at 10:08
What is wrong with your current solution?
– ctrl-alt-delor
Jan 3 at 10:21
i'm looking for unix command
– Kannan Manokaran
Jan 3 at 10:25
If any of the answers solved your problem, please accept it by clicking the checkmark next to it. Thank you!
– Jeff Schaller
Jan 6 at 14:35
add a comment |
could you please edit your question to make it a bit more clear ? Also did you installed the gnu tools on AIX ? or Are you using the basic tools of AIX ?
– Kiwy
Jan 3 at 10:08
What is wrong with your current solution?
– ctrl-alt-delor
Jan 3 at 10:21
i'm looking for unix command
– Kannan Manokaran
Jan 3 at 10:25
If any of the answers solved your problem, please accept it by clicking the checkmark next to it. Thank you!
– Jeff Schaller
Jan 6 at 14:35
could you please edit your question to make it a bit more clear ? Also did you installed the gnu tools on AIX ? or Are you using the basic tools of AIX ?
– Kiwy
Jan 3 at 10:08
could you please edit your question to make it a bit more clear ? Also did you installed the gnu tools on AIX ? or Are you using the basic tools of AIX ?
– Kiwy
Jan 3 at 10:08
What is wrong with your current solution?
– ctrl-alt-delor
Jan 3 at 10:21
What is wrong with your current solution?
– ctrl-alt-delor
Jan 3 at 10:21
i'm looking for unix command
– Kannan Manokaran
Jan 3 at 10:25
i'm looking for unix command
– Kannan Manokaran
Jan 3 at 10:25
If any of the answers solved your problem, please accept it by clicking the checkmark next to it. Thank you!
– Jeff Schaller
Jan 6 at 14:35
If any of the answers solved your problem, please accept it by clicking the checkmark next to it. Thank you!
– Jeff Schaller
Jan 6 at 14:35
add a comment |
3 Answers
3
active
oldest
votes
See Limit POSIX find to specific depth? for the standard equivalent of GNU find
's -maxdepth
predicate. So here:
(cd /Test && find . ! -name . -prune -type d -name '???') | sed 's|./||'
Or if zsh
is installed:
zsh -c 'printf "%sn" /Test/???(D/:t)'
add a comment |
A simple shell loop solution would be to do
for pathname in Test/???/; do
printf '%sn' "$( basename "$pathname" )"
done
This would print each subdirectory name that matches the given pattern (or names of symbolic links to subdirectories).
If you want to do anything other than listing the names, then you would do something like
for pathname in Test/???/; do
# some code using "$pathname" here
done
I.e., you would not first generate the list and then iterate over it.
find Test -type d -name '???' -- this command finding 3 character sub directory for each sub directory. i'm looking sub directories list which is having 3 character next to the /test directory
– Kannan Manokaran
Jan 3 at 10:29
@KannanManokaran Are you saying that using-type d
returns non-directories?
– Kusalananda
Jan 3 at 10:30
no. it is giving sub directories only..i'm doing this on Linux, something i want to do it on AIX
– Kannan Manokaran
Jan 3 at 10:39
If AIXfind
supported-maxdepth
, then the OP's code would work there.
– Stéphane Chazelas
Jan 3 at 11:36
add a comment |
Given that you don't want to recurse into the directory, a simple wildcard could suffice. The baseline is ???/
, meaning "match directory names that have exactly three characters; in the default AIX shell of ksh, you would need to add .??/
to match "hidden" directories that start with a period and are followed by two characters (assuming you count the period as one of the three; use .???/
if the period doesn't count).
Beyond that, the only "tricks" are:
to use a subshell to
cd
into the /test directory; otherwise, you would need to additionally post-process away the leading "/test" strings.since we're using a trailing slash
/
to force the wildcard to match directories (versus files), we usesed
remove the trailing slash from each line.
The one-liner is then:
(cd /test; printf '%sn' ???/ .??/) | sed 's!/$!!'
With a sample setup of:
mkdir /test
mkdir /test/AAA /test/BBB /test/.AB
touch /test/aaa
The sample results are:
AAA
BBB
.AB
1
Note that???/
(contrary tozsh
's???(/)
) matches both directories and symlinks to directories.
– Stéphane Chazelas
Jan 3 at 20:41
add a comment |
Your Answer
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "106"
};
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: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
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%2funix.stackexchange.com%2fquestions%2f492200%2fprint-all-3-character-sub-directories-using-shell-script-in-aix%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
See Limit POSIX find to specific depth? for the standard equivalent of GNU find
's -maxdepth
predicate. So here:
(cd /Test && find . ! -name . -prune -type d -name '???') | sed 's|./||'
Or if zsh
is installed:
zsh -c 'printf "%sn" /Test/???(D/:t)'
add a comment |
See Limit POSIX find to specific depth? for the standard equivalent of GNU find
's -maxdepth
predicate. So here:
(cd /Test && find . ! -name . -prune -type d -name '???') | sed 's|./||'
Or if zsh
is installed:
zsh -c 'printf "%sn" /Test/???(D/:t)'
add a comment |
See Limit POSIX find to specific depth? for the standard equivalent of GNU find
's -maxdepth
predicate. So here:
(cd /Test && find . ! -name . -prune -type d -name '???') | sed 's|./||'
Or if zsh
is installed:
zsh -c 'printf "%sn" /Test/???(D/:t)'
See Limit POSIX find to specific depth? for the standard equivalent of GNU find
's -maxdepth
predicate. So here:
(cd /Test && find . ! -name . -prune -type d -name '???') | sed 's|./||'
Or if zsh
is installed:
zsh -c 'printf "%sn" /Test/???(D/:t)'
edited Jan 3 at 20:55
answered Jan 3 at 10:17
Stéphane ChazelasStéphane Chazelas
301k55564916
301k55564916
add a comment |
add a comment |
A simple shell loop solution would be to do
for pathname in Test/???/; do
printf '%sn' "$( basename "$pathname" )"
done
This would print each subdirectory name that matches the given pattern (or names of symbolic links to subdirectories).
If you want to do anything other than listing the names, then you would do something like
for pathname in Test/???/; do
# some code using "$pathname" here
done
I.e., you would not first generate the list and then iterate over it.
find Test -type d -name '???' -- this command finding 3 character sub directory for each sub directory. i'm looking sub directories list which is having 3 character next to the /test directory
– Kannan Manokaran
Jan 3 at 10:29
@KannanManokaran Are you saying that using-type d
returns non-directories?
– Kusalananda
Jan 3 at 10:30
no. it is giving sub directories only..i'm doing this on Linux, something i want to do it on AIX
– Kannan Manokaran
Jan 3 at 10:39
If AIXfind
supported-maxdepth
, then the OP's code would work there.
– Stéphane Chazelas
Jan 3 at 11:36
add a comment |
A simple shell loop solution would be to do
for pathname in Test/???/; do
printf '%sn' "$( basename "$pathname" )"
done
This would print each subdirectory name that matches the given pattern (or names of symbolic links to subdirectories).
If you want to do anything other than listing the names, then you would do something like
for pathname in Test/???/; do
# some code using "$pathname" here
done
I.e., you would not first generate the list and then iterate over it.
find Test -type d -name '???' -- this command finding 3 character sub directory for each sub directory. i'm looking sub directories list which is having 3 character next to the /test directory
– Kannan Manokaran
Jan 3 at 10:29
@KannanManokaran Are you saying that using-type d
returns non-directories?
– Kusalananda
Jan 3 at 10:30
no. it is giving sub directories only..i'm doing this on Linux, something i want to do it on AIX
– Kannan Manokaran
Jan 3 at 10:39
If AIXfind
supported-maxdepth
, then the OP's code would work there.
– Stéphane Chazelas
Jan 3 at 11:36
add a comment |
A simple shell loop solution would be to do
for pathname in Test/???/; do
printf '%sn' "$( basename "$pathname" )"
done
This would print each subdirectory name that matches the given pattern (or names of symbolic links to subdirectories).
If you want to do anything other than listing the names, then you would do something like
for pathname in Test/???/; do
# some code using "$pathname" here
done
I.e., you would not first generate the list and then iterate over it.
A simple shell loop solution would be to do
for pathname in Test/???/; do
printf '%sn' "$( basename "$pathname" )"
done
This would print each subdirectory name that matches the given pattern (or names of symbolic links to subdirectories).
If you want to do anything other than listing the names, then you would do something like
for pathname in Test/???/; do
# some code using "$pathname" here
done
I.e., you would not first generate the list and then iterate over it.
edited Jan 3 at 11:40
answered Jan 3 at 10:15
KusalanandaKusalananda
124k16232382
124k16232382
find Test -type d -name '???' -- this command finding 3 character sub directory for each sub directory. i'm looking sub directories list which is having 3 character next to the /test directory
– Kannan Manokaran
Jan 3 at 10:29
@KannanManokaran Are you saying that using-type d
returns non-directories?
– Kusalananda
Jan 3 at 10:30
no. it is giving sub directories only..i'm doing this on Linux, something i want to do it on AIX
– Kannan Manokaran
Jan 3 at 10:39
If AIXfind
supported-maxdepth
, then the OP's code would work there.
– Stéphane Chazelas
Jan 3 at 11:36
add a comment |
find Test -type d -name '???' -- this command finding 3 character sub directory for each sub directory. i'm looking sub directories list which is having 3 character next to the /test directory
– Kannan Manokaran
Jan 3 at 10:29
@KannanManokaran Are you saying that using-type d
returns non-directories?
– Kusalananda
Jan 3 at 10:30
no. it is giving sub directories only..i'm doing this on Linux, something i want to do it on AIX
– Kannan Manokaran
Jan 3 at 10:39
If AIXfind
supported-maxdepth
, then the OP's code would work there.
– Stéphane Chazelas
Jan 3 at 11:36
find Test -type d -name '???' -- this command finding 3 character sub directory for each sub directory. i'm looking sub directories list which is having 3 character next to the /test directory
– Kannan Manokaran
Jan 3 at 10:29
find Test -type d -name '???' -- this command finding 3 character sub directory for each sub directory. i'm looking sub directories list which is having 3 character next to the /test directory
– Kannan Manokaran
Jan 3 at 10:29
@KannanManokaran Are you saying that using
-type d
returns non-directories?– Kusalananda
Jan 3 at 10:30
@KannanManokaran Are you saying that using
-type d
returns non-directories?– Kusalananda
Jan 3 at 10:30
no. it is giving sub directories only..i'm doing this on Linux, something i want to do it on AIX
– Kannan Manokaran
Jan 3 at 10:39
no. it is giving sub directories only..i'm doing this on Linux, something i want to do it on AIX
– Kannan Manokaran
Jan 3 at 10:39
If AIX
find
supported -maxdepth
, then the OP's code would work there.– Stéphane Chazelas
Jan 3 at 11:36
If AIX
find
supported -maxdepth
, then the OP's code would work there.– Stéphane Chazelas
Jan 3 at 11:36
add a comment |
Given that you don't want to recurse into the directory, a simple wildcard could suffice. The baseline is ???/
, meaning "match directory names that have exactly three characters; in the default AIX shell of ksh, you would need to add .??/
to match "hidden" directories that start with a period and are followed by two characters (assuming you count the period as one of the three; use .???/
if the period doesn't count).
Beyond that, the only "tricks" are:
to use a subshell to
cd
into the /test directory; otherwise, you would need to additionally post-process away the leading "/test" strings.since we're using a trailing slash
/
to force the wildcard to match directories (versus files), we usesed
remove the trailing slash from each line.
The one-liner is then:
(cd /test; printf '%sn' ???/ .??/) | sed 's!/$!!'
With a sample setup of:
mkdir /test
mkdir /test/AAA /test/BBB /test/.AB
touch /test/aaa
The sample results are:
AAA
BBB
.AB
1
Note that???/
(contrary tozsh
's???(/)
) matches both directories and symlinks to directories.
– Stéphane Chazelas
Jan 3 at 20:41
add a comment |
Given that you don't want to recurse into the directory, a simple wildcard could suffice. The baseline is ???/
, meaning "match directory names that have exactly three characters; in the default AIX shell of ksh, you would need to add .??/
to match "hidden" directories that start with a period and are followed by two characters (assuming you count the period as one of the three; use .???/
if the period doesn't count).
Beyond that, the only "tricks" are:
to use a subshell to
cd
into the /test directory; otherwise, you would need to additionally post-process away the leading "/test" strings.since we're using a trailing slash
/
to force the wildcard to match directories (versus files), we usesed
remove the trailing slash from each line.
The one-liner is then:
(cd /test; printf '%sn' ???/ .??/) | sed 's!/$!!'
With a sample setup of:
mkdir /test
mkdir /test/AAA /test/BBB /test/.AB
touch /test/aaa
The sample results are:
AAA
BBB
.AB
1
Note that???/
(contrary tozsh
's???(/)
) matches both directories and symlinks to directories.
– Stéphane Chazelas
Jan 3 at 20:41
add a comment |
Given that you don't want to recurse into the directory, a simple wildcard could suffice. The baseline is ???/
, meaning "match directory names that have exactly three characters; in the default AIX shell of ksh, you would need to add .??/
to match "hidden" directories that start with a period and are followed by two characters (assuming you count the period as one of the three; use .???/
if the period doesn't count).
Beyond that, the only "tricks" are:
to use a subshell to
cd
into the /test directory; otherwise, you would need to additionally post-process away the leading "/test" strings.since we're using a trailing slash
/
to force the wildcard to match directories (versus files), we usesed
remove the trailing slash from each line.
The one-liner is then:
(cd /test; printf '%sn' ???/ .??/) | sed 's!/$!!'
With a sample setup of:
mkdir /test
mkdir /test/AAA /test/BBB /test/.AB
touch /test/aaa
The sample results are:
AAA
BBB
.AB
Given that you don't want to recurse into the directory, a simple wildcard could suffice. The baseline is ???/
, meaning "match directory names that have exactly three characters; in the default AIX shell of ksh, you would need to add .??/
to match "hidden" directories that start with a period and are followed by two characters (assuming you count the period as one of the three; use .???/
if the period doesn't count).
Beyond that, the only "tricks" are:
to use a subshell to
cd
into the /test directory; otherwise, you would need to additionally post-process away the leading "/test" strings.since we're using a trailing slash
/
to force the wildcard to match directories (versus files), we usesed
remove the trailing slash from each line.
The one-liner is then:
(cd /test; printf '%sn' ???/ .??/) | sed 's!/$!!'
With a sample setup of:
mkdir /test
mkdir /test/AAA /test/BBB /test/.AB
touch /test/aaa
The sample results are:
AAA
BBB
.AB
answered Jan 3 at 18:08
Jeff SchallerJeff Schaller
39.3k1054125
39.3k1054125
1
Note that???/
(contrary tozsh
's???(/)
) matches both directories and symlinks to directories.
– Stéphane Chazelas
Jan 3 at 20:41
add a comment |
1
Note that???/
(contrary tozsh
's???(/)
) matches both directories and symlinks to directories.
– Stéphane Chazelas
Jan 3 at 20:41
1
1
Note that
???/
(contrary to zsh
's ???(/)
) matches both directories and symlinks to directories.– Stéphane Chazelas
Jan 3 at 20:41
Note that
???/
(contrary to zsh
's ???(/)
) matches both directories and symlinks to directories.– Stéphane Chazelas
Jan 3 at 20:41
add a comment |
Thanks for contributing an answer to Unix & Linux Stack Exchange!
- 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%2funix.stackexchange.com%2fquestions%2f492200%2fprint-all-3-character-sub-directories-using-shell-script-in-aix%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
could you please edit your question to make it a bit more clear ? Also did you installed the gnu tools on AIX ? or Are you using the basic tools of AIX ?
– Kiwy
Jan 3 at 10:08
What is wrong with your current solution?
– ctrl-alt-delor
Jan 3 at 10:21
i'm looking for unix command
– Kannan Manokaran
Jan 3 at 10:25
If any of the answers solved your problem, please accept it by clicking the checkmark next to it. Thank you!
– Jeff Schaller
Jan 6 at 14:35