I'm bit confused about the command or modifiers to obtain: List of directories (only directories, not including subdirectories) which names include a pattern.
Thanks in advance.
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.
Use the ls command to display the contents of a directory. The ls command writes to standard output the contents of each specified Directory or the name of each specified File, along with any other information you ask for with the flags.
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.
Use the ls Command to List Directories in Bash. We use the ls command to list items in the current directory in Bash. However, we can use */ to print directories only since all directories finish in a / with the -d option to assure that only the directories' names are displayed rather than their contents.
You are probably after the -d
switch of ls
:
ls -d *pattern*/ ls --directory *pattern*/
Use this little hack:
printf '%s\n' *pattern*/
if you prefer all on the same line :
echo *pattern*/
or using bash array :
arr=( *pattern*/ ) printf '%s\n' "${arr[@]%/}"
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