List subdirectories only n level deep

Festival stores voicepack data in the following example directory structure:

/usr/share/festival/voices/<language>/<voicepack name>

What is the simplest one-liner (preferably using ls) to print out just the <voicepack name>‘s, in all the potentially numerous <language> subdirectories?

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

I’m on Fedora, and these voicepacks are in a slightly different location:

$ ls /usr/share/festival/lib/voices/*/ -1 | grep -vE "/usr|^$"
kal_diphone
ked_diphone
nitech_us_awb_arctic_hts
nitech_us_bdl_arctic_hts
nitech_us_clb_arctic_hts
nitech_us_jmk_arctic_hts
nitech_us_rms_arctic_hts
nitech_us_slt_arctic_hts

You can just modify this like so:

$ ls /usr/share/festival/voices/*/ -1 | grep -vE "/usr|^$"

Using find

Using ls in this manor is typically frowned upon because the output of ls is difficult to parse. Better to use the find command, like so:

$ find /usr/share/festival/lib/voices -maxdepth 2 -mindepth 2 
    -type d -exec basename {} ;
nitech_us_awb_arctic_hts
nitech_us_bdl_arctic_hts
nitech_us_slt_arctic_hts
nitech_us_jmk_arctic_hts
nitech_us_clb_arctic_hts
nitech_us_rms_arctic_hts
ked_diphone
kal_diphone

Details of find & basename

This command works by producing a list of full paths to files that are exactly 2 levels deep with respect to this directory:

/usr/share/festival/lib/voices

This list looks like this:

$ find /usr/share/festival/lib/voices -maxdepth 2 -mindepth 2 
/usr/share/festival/lib/voices/us/nitech_us_awb_arctic_hts
/usr/share/festival/lib/voices/us/nitech_us_bdl_arctic_hts
/usr/share/festival/lib/voices/us/nitech_us_slt_arctic_hts
/usr/share/festival/lib/voices/us/nitech_us_jmk_arctic_hts
/usr/share/festival/lib/voices/us/nitech_us_clb_arctic_hts
/usr/share/festival/lib/voices/us/nitech_us_rms_arctic_hts
/usr/share/festival/lib/voices/english/ked_diphone
/usr/share/festival/lib/voices/english/kal_diphon

But we want the last part of these directories, the leaf node. So we can make use of basename to parse it out:

$ basename /usr/share/festival/lib/voices/us/nitech_us_awb_arctic_hts
nitech_us_awb_arctic_hts

Putting it all together, we can make the find command pass each 2 level deep directory to the basename command. The notation basename {} is what is doing these basename conversions. Find calls it via it’s -exec switch.

Method 2

The easiest is

ls -d /usr/share/festival/voices/*/*

That is expanded by the shell into all sub directories of /usr/share/festival/voices/ and then to the contents of each of those sub directories.

If you only want to descend to a specific level as your title suggests, with some implementations of find like GNU’s and some BSD’s:

find /usr/share/festival/voices/ -mindepth 2 -maxdepth 3 -type d

That will find all directories (-type d) that are in a subdirectory of /usr/share/festival/voices/ because of mindepth 2 but are not deeper than 3 levels down (maxdepth 3). From man find:

   -maxdepth levels
          Descend at most levels (a non-negative integer) levels of direc‐
          tories below the command line arguments.  -maxdepth 0
           means only apply the tests and  actions  to  the  command  line
          arguments.

   -mindepth levels
          Do  not apply any tests or actions at levels less than levels (a
          non-negative integer).  -mindepth  1  means  process  all  files
          except the command line arguments.

Method 3

The accepted answer works correctly
but is somewhat inefficient because it spawns a new basename process for
each subdirectory:

find /usr/share/festival/lib/voices -maxdepth 2 -mindepth 2 
    -type d -exec basename {} ;

When possible, it’s preferable to use features built into find to avoid the
expense of spawning processes. find has a fairly extensive capability to
modify its printed output using the -printf action. The default -print
action prints the entire path, but using -printf and a format string it’s
possible to select portions of the path for printing. To extract just the
filename portion of the path without the leading directories (as basename
does), the format string is %f. To place a newline after each filename,
include n as follows:

$ find /usr/share/festival/lib/voices -maxdepth 2 -mindepth 2 
    -type d -printf '%fn'
nitech_us_awb_arctic_hts
nitech_us_bdl_arctic_hts
nitech_us_slt_arctic_hts
nitech_us_jmk_arctic_hts
nitech_us_clb_arctic_hts
nitech_us_rms_arctic_hts
ked_diphone
kal_diphone

Method 4

TLDR; for those just coming here based on the title of this question; to “List subdirectories only n level[s] deep”: use

find -maxdepth N

where N is any number.


Example:

# list all files and folders 4 levels deep
find -maxdepth 4

And if you need to search for a particular file or folder, just pipe it to grep. Ex:

find -maxdepth 4 | grep -i some_file_name

Note that the -i above makes the grep search of the file and folder names coming out of the find command case ‘i’nsensitive.

For additional help with find, to make it search only where you want and find only what you want, see also my other answer here: How to exclude a directory in find . command

Method 5

For someone using bash looking for something simple just using ls:

ls -d $PWD/*

When creating an alias (in ~/.bash_aliases or wherever), be sure to use single-quotes:

alias ldf='ls -d $PWD/*'

Not quoting it will result in the shell trying to execute ls.

Double-quoting it will create the alias with the value of $PWD at the time of the alias.

You could use $(pwd) if you prefer, but I don’t see the point of spawning a sub-shell when bash provides $PWD for you.

Method 6

In zsh:

print -rC1 -- /usr/share/festival/voices/*/*(N-/:t)

Or the GNU find + sort equivalent:

LC_ALL=C find -L /usr/share/festival/voices -mindepth 2 -maxdepth 2 
  -name '.*' -prune -o -type d -printf '%f' |
  sort -z |
  tr '' 'n'


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x