Listing only directories using ls in bash: An exam

2019-01-05 06:22发布

This command lists directories in the current path: ls -d */

What exactly does the pattern */ do?

And how can we give the absolute path in the above command (e.g. ls -d /home/alice/Documents) for listing only directories in that path?

25条回答
ゆ 、 Hurt°
2楼-- · 2019-01-05 06:58
file *|grep directory

O/P (On my machine) --

[root@rhel6 ~]# file *|grep directory
mongo-example-master:    directory
nostarch:                directory
scriptzz:                directory
splunk:                  directory
testdir:                 directory

Above output can be refined more by using cut .

file *|grep directory|cut -d':' -f1
mongo-example-master
nostarch
scriptzz
splunk
testdir

* could be replaced with any path that's permitted
 file - determine file type
 grep - searches for string named directory
 -d - to specify a field delimiter
 -f1 - denotes field 1
查看更多
我想做一个坏孩纸
3楼-- · 2019-01-05 07:01

A plain listing of the current directory, it'd be:

ls -1d ./*/

Do you want it sorted and clean?

ls -1d ./*/ | cut -c 3- | rev | cut -c 2- | rev | sort
查看更多
相关推荐>>
4楼-- · 2019-01-05 07:02

One-liner to list directories only from "here".

With file count.

for i in `ls -d */`; do g=`find ./$i -type f -print| wc -l`; echo "Directory $i contains $g files."; done
查看更多
叼着烟拽天下
5楼-- · 2019-01-05 07:04

Four ways to get this done, each with a different output format

1. Using echo

Example: echo */, echo */*/
Here is what I got:

cs/ draft/ files/ hacks/ masters/ static/  
cs/code/ files/images/ static/images/ static/stylesheets/  

2. Using ls only

Example: ls -d */
Here is exactly what I got:

cs/     files/      masters/  
draft/  hacks/      static/  

Or as list (with detail info): ls -dl */

3. Using ls and grep

Example: ls -l | grep "^d" Here is what I got:

drwxr-xr-x  24 h  staff     816 Jun  8 10:55 cs  
drwxr-xr-x   6 h  staff     204 Jun  8 10:55 draft  
drwxr-xr-x   9 h  staff     306 Jun  8 10:55 files  
drwxr-xr-x   2 h  staff      68 Jun  9 13:19 hacks  
drwxr-xr-x   6 h  staff     204 Jun  8 10:55 masters  
drwxr-xr-x   4 h  staff     136 Jun  8 10:55 static  

4. Bash Script (Not recommended for filename contain space.)

Example: for i in $(ls -d */); do echo ${i%%/}; done
Here is what I got:

cs  
draft  
files  
hacks  
masters  
static

If you like to have '/' as ending character, the command will be: for i in $(ls -d */); do echo ${i}; done

cs/  
draft/  
files/  
hacks/  
masters/  
static/
查看更多
聊天终结者
6楼-- · 2019-01-05 07:04

I just add this to my .bashrc file (you could also just type it on the command line if you only need/want it for one session)

alias lsd='ls -ld */'

then lsd will produce the desired result.

查看更多
Viruses.
7楼-- · 2019-01-05 07:05

The tree command is also pretty useful here. By default it will show all files and directories to a complete depth, with some ascii characters showing the directory tree.

$ tree
.
├── config.dat
├── data
│   ├── data1.bin
│   ├── data2.inf
│   └── sql
|   │   └── data3.sql
├── images
│   ├── background.jpg
│   ├── icon.gif
│   └── logo.jpg
├── program.exe
└── readme.txt

But if we wanted to get just the directories, without the ascii tree, and with the full path from the current directory, you could do:

$ tree -dfi
.
./data
./data/sql
./images

The arguments being:

-d     List directories only.
-f     Prints the full path prefix for each file.
-i     Makes tree not print the indentation lines, useful when used in conjunction with the -f option.

And if you then want the absolute path, you could start by specifying the full path to the current directory:

$ tree -dfi "$(pwd)"
/home/alice/Documents
/home/alice/Documents/data
/home/alice/Documents/data/sql
/home/alice/Documents/images

And to limit the number of subdirectories, you can set the max level of subdirectories with -L level, eg:

$ tree -dfi -L 1 "$(pwd)"
/home/alice/Documents
/home/alice/Documents/data
/home/alice/Documents/images

More arguments can be seen with man tree

查看更多
登录 后发表回答