Skip to main content


Write a Linux command line to give the names of the sub-directories in just the current working directory (no recursion).

Also, I've decided to reclaim my weekends, so these daily Linux challenges will henceforth be a Mon-Fri affair. I'll check in with the answer to this question on Monday. Everybody enjoy your weekends!

#Linux #DFIR #CommandLine #Trivia

Hal Pomeranz reshared this.

in reply to Hal Pomeranz

For normal files, echo */.
But if you want hidden directories, too, then something like find . -maxdepth 1 -type d.
in reply to Hal Pomeranz

Alternative solutions:

  • tree -daL 1
  • perl -wle 'opendir my $dh, "." or die $!; for (sort readdir $dh) { print if -d }'
  • ghc -e 'import System.Directory' -e 'import Control.Monad' -e 'listDirectory "." >>= filterM doesDirectoryExist >>= mapM_ putStrLn'
in reply to Hal Pomeranz

and for fun: "file *|sed -ne /directory$/p |sed -e s/:[^:]*$//" also per comments, guess it should have been "ls -AF|..." Looks like someone already did the find variant, what else.... stat -f "%HT %N" *| awk '/Directory / {gsub("Directory ",""); print}' also but doesnt work with .dir...I guess for file/stat "./.* *" would work if you dont mind getting . and ..
in reply to Hal Pomeranz

Last Friday's daily Linux command line trivia asked for a command line to print out the names of the subdirectories of the current directory, but NOT to recurse and show all subdirectories below this point. There are lots of ways to accomplish this, and I was curious to see if I would learn any new ones. Turns out I did.

First, there's a classic solution using the "find" command (props to @barubary for being the first to mention it):

find . -maxdepth 1 -type d

Here we're using the "maxdepth" control to only show directories ("-type d") in the current directory.

There are also solutions using "ls". @beejjorgensen checked in with one of my favorites:

ls -lA | grep ^d

In the detailed listing format ("ls -l"), the file type is the first character on the line. Directories are denote with a "d".

@llutz checked in with another "ls" solution that I hadn't considered before:

ls -d .*/ */

@barubary points out that "echo" would work here too, instead of "ls -d". The trailing "/" after the wildcards matches only directories. Don't forget ".*/" to match hidden directories!

@barubary suggested a solution using the "tree" command. Strictly speaking, the "tree" package is not part of the core Linux OS, so it falls outside the rules I've set for myself. But here's the solution:

tree -daL 1

This means show all files ("-a") including hidden files, but display directories only ("-d"). "-L 1" is like the "-maxdepth" control on "find"-- only display output from the current directory.

#Linux #DFIR #CommandLine #Trivia

Hal Pomeranz reshared this.

in reply to Hal Pomeranz

It occurs to me that, at least in the (perhaps distant) past, shell globbing had limits. If there were too many matches for your pattern, your command would error out. If that's still true today, the `find` solution gets around that issue.