ls only files or directories examples in linux

Test dir

linux ls only directory

First way

➜ ls -d */
-d Directories are listed as plain files (not searched recursively).

*/ is a pattern that matches all of the subdirectories in the current directory (* would match all files and subdirectories; the / restricts it to directories). 
Reference:https://stackoverflow.com/questions/14352290/listing-only-directories-using-ls-in-bash-an-examination

Second way: using linux ls and linux grep command

➜ ls -l | grep "^d"

Third way:using linux find command

➜ find . -type d -maxdepth 1

linux ls only files

First way

➜ ls -l | grep -v "^[d|l]"
➜ ls -l | grep "^-"

Second way: using linux find command

➜ find ./ -type f -maxdepth 1

Add a Comment

Your email address will not be published. Required fields are marked *