HowTo: Linux / UNIX List Just Directories Or Di...

How do I list just directory names under Linux and UNIX operating systems?

Under Linux or UNIX 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 and grep to list directory names only.

Display or list all directories

Type the following command:
$ ls -l | egrep `^d'

terminal.png

Display or list only files

Type the following command:
$ ls -l | egrep -v `^d'

grep command used to searches input. It will filter out directories name by matching first character d. To reverse effect (just to display files) you need to pass -v option. It invert the sense of matching, to select non-matching lines.

Task: Create aliases to save time

You can create two aliases as follows to list only directories and files.
alias lf="ls -l | egrep -v '^d'"
alias ldir="ls -l | egrep '^d'"

Put above two aliases in your bash shell startup file:
$ cd
$ vi .bash_profile

Append two lines:
alias lf="ls -l | egrep -v '^d'"
alias ldir="ls -l | egrep '^d'"

Save and close the file.

Now just type lf - to list files and ldir - to list directories only:
$ cd /etc
$ lf

Output:

-rw-r--r--   1 root root      2149 2006-09-04 23:25 adduser.conf
-rw-r--r--   1 root root        44 2006-09-29 05:11 adjtime
-rw-r--r--   1 root root       197 2006-09-04 23:48 aliases
-rw-------   1 root root       144 2002-01-18 13:43 at.deny
-rw-r--r--   1 root root       162 2006-09-22 23:24 aumixrc
-rw-r--r--   1 root root        28 2006-09-22 23:24 aumixrc1
....
..
....

List directory names only:
$ cd /etc
$ ldir
Output:

drwxr-xr-x   4 root root      4096 2006-09-22 16:41 alsa
drwxr-xr-x   2 root root      4096 2006-09-20 20:59 alternatives
drwxr-xr-x   6 root root      4096 2006-09-22 16:41 apm
drwxr-xr-x   3 root root      4096 2006-09-07 02:51 apt
drwxr-xr-x   2 root root      4096 2006-09-08 01:46 bash_completion.d
....
.....
.

find command

The find command can be used as follows to list all directories in /nas, enter:

find /nas -type dfind /nas -type d -ls find . -type d -ls

Sample outputs:

1070785    8 drwxrwxrwt   8 root     root         4096 Jul  5 07:12 .
1070797    8 drwx------   2 root     root         4096 Jul  4 07:22 ./orbit-root
1070843    8 drwxr-xr-x   2 root     root         4096 Jun 16 18:55 ./w
1070789    8 drwxr-xr-x  10 root     root         4096 Jun 17 14:54 ./b
1071340    8 drwxr-xr-x   2 root     root         4096 Jun 16 18:55 ./b/init.d
1071581    8 drwxr-xr-x   3 root     root         4096 Jun 16 18:55 ./b/bind
1071584    8 drwxr-xr-x   2 root     root         4096 Jun 16 18:55 ./b/bind/bak
1071617    8 drwxr-xr-x   2 root     root         4096 Jun 16 18:55 ./b/fw
1071628    8 drwxr-xr-x   8 root     root         4096 Jun 16 18:55 ./b/scripts

转载于:https://my.oschina.net/u/187928/blog/33633

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值