Code Yarns ‍👨‍💻
Tech BlogPersonal Blog

How to list files or directories only

📅 2015-Apr-03 ⬩ ✍️ Ashwin Nanjappa ⬩ 🏷️ find, ls ⬩ 📚 Archive

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