Posted: . At: 12:56 PM. This was 6 years ago. Post ID: 12568
Page permalink. WordPress uses cookies, or tiny pieces of information stored on your computer, to verify who you are. There are cookies for logged in users and for commenters.
These cookies expire two weeks after they are set.


How to list only directories with the ls command on Mac OSX.


The ls command on Mac OSX works a little differently than on a Linux machine, but listing only directories is very simple.

This is one example, using only the ls command.

deusexmachina:Documents jason$ ls -ld -- */
drwxr-xr-x   3 jason  staff   102  5 Mar  2018 POL/
drwxrwxrwx  31 jason  staff  1054  7 Jan  2018 Stalker Complete/
drwxr-xr-x   4 jason  staff   136 24 Jan  2018 Virtual Machines.localized/
drwxrwxrwx  21 jason  staff   714 18 Jan  2018 co60_AW_Invade_Annex_2_85C.Altis/
drwxrwxrwx   5 jason  staff   170 11 May  2016 ipinfo/
drwxrwxrwx  11 jason  staff   374  1 Oct 21:49 sysinfo/
drwxr-xr-x   4 jason  staff   136  2 Oct 10:55 system/

This will only show the directories in the current folder.

Listing only directories with the bash shell.
Listing only directories with the bash shell.

This works perfectly, as the screenshot above shows.

Another method, using bash and grep is this one, this lists all subdirectories under the current working directory.

deusexmachina:Documents jason$ ls -F | grep /
POL/
Stalker Complete/
Virtual Machines.localized/
co60_AW_Invade_Annex_2_85C.Altis/
ipinfo/
sysinfo/
system/

This below, is yet another method to list all directories without using ls.

deusexmachina:Documents jason$ echo -- */ | tr ' ' '\n'
--
POL/
Stalker
Complete/
Virtual
Machines.localized/
co60_AW_Invade_Annex_2_85C.Altis/
ipinfo/
sysinfo/
system/

List all files in a directory using this method.

deusexmachina:ipinfo jason$ echo -- * | tr ' ' '\n'
--
Makefile
obj
src

This gives a proper ls styled directory listing, without using ls at all. This is very nice.

Yet another method, using awk. This is using wildcards to list all files matching a certain pattern.

deusexmachina:Documents jason$ echo -- Arma* | awk -v RS=" " '{print}'
--
Arma3_x64_2017_11_13_21_50_20_373.avi
Arma3_x64_2018_01_04_15_29_53_844.avi
Arma3_x64_2018_01_04_15_32_03_159.avi
Arma3_x64_2018_01_04_15_34_52_367.avi

Finally, the find command may be used to execute a command on each file it finds.

deusexmachina:sysinfo jason$ find . -type f -d 1 -exec ls -l {} +
-rwxrwxrwx  1 jason  staff  18015 20 Sep 09:08 ./LICENSE
-rwxrwxrwx  1 jason  staff    351 20 Sep 09:08 ./README.md
-rwxrwxrwx  1 jason  staff    703 20 Sep 09:45 ./makefile
-rwxrwxrwx  1 jason  staff  17952  1 Oct 20:58 ./sysinfo
-rwxrwxrwx  1 jason  staff  12921 20 Sep 18:21 ./system-info

This way, I can run the ls command on each file and list them.

Or without using ls.

deusexmachina:sysinfo jason$ find . -type f -d 1 -exec echo -- * {} +
-- LICENSE README.md doc makefile obj src sysinfo system-info ./LICENSE ./makefile ./README.md ./sysinfo ./system-info

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.