How to list only the directories inside a directory?

Answer Posted / rikesh

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
$ ldirOutput:

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
....
.....
.

Is This Answer Correct ?    2 Yes 0 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

i am prepairing for 2+ yrs exp in oracle/unix production support,can u tell me which type issue comes in server plz help

6642


I am new to Unix and Unix Shell scripting could you guide me on how to go about these subjects and where to start from with concern to Oracle?. I also would like to know where does UNIX shell Scripting help in terms of development of a application in Oracle? I have no clue in the subject so do help me. Thank You Neelima

2295


Am using Mac OS 10.4.11 on an Imac (PPC) Internal hard disk got named disk0s3 and will not mount. What commands can be used in TERMINAL to rename the internal hard disk to C96?

2104


Which is the most commonly used replacement algorithm?

693


What is YACC?

653






why unix commands simpler rather than complex task

3842


If Media Server fails. What are the steps to be followed? Can anyone please reply for this question Thanks, Ganesh

2561


What are the events done by the Kernel after a process is being swapped out from the main memory?

1975


What are the main differences between Apache 1.x and 2.x?

7564


what is output mkvg pvname

1704


What is ant_build.sh? Have u seen that file?

1816


Explain about Inodes?

653


what is difference between milestone and run-levels in Solaris ?

2564


. Using sed and grep, write a command which lists files and directories with following properties: (i) created in 1999 (ii) with user's as owner and group

2198


In detail elaborate the system? What is driver?

1678