1

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
Jeff Schaller
  • 67,283
  • 35
  • 116
  • 255

3 Answers3

1

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)'
0

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.

Kusalananda
  • 333,661
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
Jeff Schaller
  • 67,283
  • 35
  • 116
  • 255