How to list files by type with ls

ls

When I use the ls command with the option -l, the first string of letters gives the info about each file, and the first letter in this string gives the file's type. (d = directory, - = standard file, l = link, etc.)

How can I filter the files according to that first letter?

Best Answer

You can filter out everything but directories using grep this way:

ls -l | grep '^d'

the ^ indicates that the pattern is at the beginning of the line. Replace d with -, l, etc., as applicable.

You can of course use other commands to directly search for specific types (e.g. find . -maxdepth 1 -type d) or use ls -l | sort to group similar types together based on this first character, but if you want to filter you should use grep to only select the appropriate lines from the output.

Related Question