只列出Linux中的文件或目录
来源:互联网 发布:阿里云服务器购买折扣 编辑:程序博客网 时间:2024/05/08 11:43
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 -lsfind . -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
- 只列出Linux中的文件或目录
- Linux中只列出目录
- Linux Shell 只列出目录的方法
- linux中只列出目录项
- 列出目录中的所有文件
- CMD列出目录中的文件
- 列出文件或目录下的文件名
- ls只列出目录
- ls只列出目录
- 列出目录下的所有文件或目录
- 递归列出目录中的文件列表
- 01列出一个目录中的所有文件
- 列出一个目录中的所有文件
- ls 列出文件或目录下的文件名
- ls命令只显示目录或文件
- ls命令只显示目录或文件
- linux 列出目录下的所有文件和目录
- Linux目录遍历实现 -- 列出当前目录下所有文件
- 为基于Wordpress的博客添加CNZZ统计代码
- 感悟天才
- 排序
- UIP移植到CC2530上
- 蛇形矩阵
- 只列出Linux中的文件或目录
- UML和模式应用(第三版)思维导图(初稿)
- 循环队列判断满与空
- ZOJ 1115 Digital Roots 水题
- 《C++程序设计语言》5.9_11输入读一系列的单词,使用quit作为输入的结束单词
- 华为校园招聘上机考试题目
- Crack the code interview_Q1.4
- [BAT][JAVA]定时任务之-Quartz使用篇(通过这个配置可以知道在做Quartz的时候需要的jar文件/Cron表达式使用语法/常用Cron表达式)
- 2013.9.21.TSQL学习