Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

List only directories names which match a pattern

Tags:

bash

shell

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.

like image 808
Jorge Vega Sánchez Avatar asked Mar 18 '13 20:03

Jorge Vega Sánchez


People also ask

How do I list only directories?

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.

How do I get a list of directories in Linux?

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.

How do I get a list of directories in Unix?

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.

How do I list all directories in Bash?

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.


2 Answers

You are probably after the -d switch of ls:

ls -d *pattern*/ ls --directory *pattern*/ 
like image 106
choroba Avatar answered Sep 19 '22 13:09

choroba


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[@]%/}" 
like image 39
Gilles Quenot Avatar answered Sep 21 '22 13:09

Gilles Quenot