How could I list sub-directories with ls, with '-d' only the current directory is shown. I want something like find . -type d -maxdepth 1
would give me.
By default, ls lists just one directory. If you name one or more directories on the command line, ls will list each one. The -R (uppercase R) option lists all subdirectories, recursively.
Linux or UNIX-like system use the ls command to list files and directories. However, ls does not have an option to list only directories. You can use combination of ls command, find command, and grep command to list directory names only. You can use the find command too.
The ls command is used to list files or directories in Linux and other Unix-based operating systems. Just like you navigate in your File explorer or Finder with a GUI, the ls command allows you to list all files or directories in the current directory by default, and further interact with them via the command line.
This should help:
ls -d */
*/
will only match directories under the current dir. The output directory names will probably contain the trailing '/' though.
You can combine with grep:
ls -l | grep '^d'
To get just the filenames:
ls -l | grep '^d' | awk '{ print $9 }'
You can make this into a handy alias:
alias ldir="ls -l | grep '^d'"
ls -d */
and ls -d */*/
seem to work just fine.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With