How to list just directory and file names under Linux??

How do I list just directory and file names?

A. Under Linux use 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’

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

You can also 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

Edit two lines:
alias lf=”ls -l | egrep -v ‘^d'”
alias ldir=”ls -l | egrep ‘^d'”

Save and close the file.(Esc:+wq!)

Now just type lf – to list files and ldir – to list directories only:
# cd /etc
# lf

It will display only files under the present working directory.

List directory names only:
# cd /etc
# ldir

This will display only directories under present working directory.

Hope this will helps you!!!!!!

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: