2

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
  • 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
2

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 "%s\n" /Test/???(D/:t)'
1

A simple shell loop solution would be to do

for pathname in Test/???/; do
    printf '%s\n' "$( 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 AIX find supported -maxdepth, then the OP's code would work there. – Stéphane Chazelas Jan 3 at 11:36
0

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 use sed remove the trailing slash from each line.

The one-liner is then:

(cd /test; printf '%s\n' ???/ .??/) | 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 to zsh's ???(/)) matches both directories and symlinks to directories. – Stéphane Chazelas Jan 3 at 20:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.