How to list files or directories only

There are many instances where I have wanted to list only the files or only the directories. The find tool seems to be the most convenient way to do this.

To list only the files in current directory:

$ find . -maxdepth 1 -type f | sort

To list only the directories in current directory:

$ find . -maxdepth 1 -type d | sort

The -maxdepth says it should only look in the directory and not recurse into subdirectories. The -type indicates that only files (or directories) should be found. The results of find are not in sorted order, so we pipe them to sort.

I recommend making an aliases out of this, so that they can be called easily like say lsf or lsd.

Tried with: FindUtils 4.4.2-7 and Ubuntu 14.04

Advertisements

Leave a Reply

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

WordPress.com Logo

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

Twitter picture

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

Facebook photo

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

Google+ photo

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

Connecting to %s