博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
HowTo: Linux / UNIX List Just Directories Or Di...
阅读量:6949 次
发布时间:2019-06-27

本文共 2750 字,大约阅读时间需要 9 分钟。

hot3.png

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'

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 alsadrwxr-xr-x   2 root root      4096 2006-09-20 20:59 alternativesdrwxr-xr-x   6 root root      4096 2006-09-22 16:41 apmdrwxr-xr-x   3 root root      4096 2006-09-07 02:51 aptdrwxr-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-root1070843    8 drwxr-xr-x   2 root     root         4096 Jun 16 18:55 ./w1070789    8 drwxr-xr-x  10 root     root         4096 Jun 17 14:54 ./b1071340    8 drwxr-xr-x   2 root     root         4096 Jun 16 18:55 ./b/init.d1071581    8 drwxr-xr-x   3 root     root         4096 Jun 16 18:55 ./b/bind1071584    8 drwxr-xr-x   2 root     root         4096 Jun 16 18:55 ./b/bind/bak1071617    8 drwxr-xr-x   2 root     root         4096 Jun 16 18:55 ./b/fw1071628    8 drwxr-xr-x   8 root     root         4096 Jun 16 18:55 ./b/scripts

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

你可能感兴趣的文章
iOS 如何在一个应用程序中调用另一个应用程序
查看>>
FTP数据备份
查看>>
第三方登录之QQ登录(一)——QQ互联开放平台新建应用
查看>>
Ubuntu上搭建Hadoop环境
查看>>
是运维就必须硬起来,插件化运维系统思路
查看>>
ibatis bug
查看>>
L7 linux shell编程练习
查看>>
开会 顺口溜
查看>>
用PHP和树莓派开发一个比特币/以太坊交易机器人
查看>>
有热备,有事物损坏 薛忠权(ERIKXUE)
查看>>
MySQL入门(二)
查看>>
手把手教 centos+nginx1.3.9+php5.4.9+mysql5.5.28+memcached
查看>>
PHP设计模式(3)观察者模式
查看>>
数据库中的左连接(left join)和右连接(right join)区别
查看>>
spring data jpa 调用oracle 存储过程
查看>>
夺命雷公狗---无限极分类NO4
查看>>
Teams新功能更新【已发布】Teams PowerShell 命令详解
查看>>
我的友情链接
查看>>
Ansible快速开始-指挥集群
查看>>
Java容器详解(以Array Arrays ArrayList为例)
查看>>