{"id":286,"date":"2016-09-30T10:43:11","date_gmt":"2016-09-30T10:43:11","guid":{"rendered":"http:\/\/www.blueshell.im\/Blog\/?p=286"},"modified":"2020-08-16T08:20:14","modified_gmt":"2020-08-16T08:20:14","slug":"how-to-list-only-directories-in-unix-or-linux","status":"publish","type":"post","link":"http:\/\/www.blueshell.im\/Blog\/?p=286","title":{"rendered":"How to list only directories in Unix or Linux"},"content":{"rendered":"<p>We were asked recently by a customer how to list only the directories when doing an ls command<\/p>\n<p>Firstly ls lists all files in a directory so an ls would produce something like this<\/p>\n<p><em>ls \/tmp<\/em><\/p>\n<p>storage.log ifcfg.log packaging.log<\/p>\n<p>if we now wanted to list only directories we would issue the command<\/p>\n<p><em>ls -d *\/<\/em><\/p>\n<p>this would give something like this<\/p>\n<p>systemd-private-611c364c72354a2fbb19e7a3484b0262-colord.service-tCwbBC\/<\/p>\n<p>systemd-private-611c364c72354a2fbb19e7a3484b0262-cups.service-zLRpKH\/<\/p>\n<p>systemd-private-611c364c72354a2fbb19e7a3484b0262-rtkit-daemon.service-I6FhXd\/<\/p>\n<p>if we now wanted to see permissions,ownerships and dates we would do the following<\/p>\n<p>for files we would issue the command<\/p>\n<p><em>ls -la<\/em><\/p>\n<p>this would give something similar to the following<\/p>\n<p>-rw-r&#8211;r&#8211;. 1 root mousse    0 Sep 22 11:53 ifcfg.log<\/p>\n<p>-rw-r&#8211;r&#8211;. 1 root mousse    0 Sep 22 11:53 packaging.log<\/p>\n<p>-rw-r&#8211;r&#8211;. 1 root mousse    0 Sep 22 11:53 storage.log<\/p>\n<p>from the above we can see that the files are owned by root and the group is mousse and from the permissions we can see that root has read and write on these files, the group mousse has read and everyone else all has read too.<\/p>\n<p>if we wanted to do this for directories we would issue the following command<\/p>\n<p><em>ls -dal *\/<\/em><\/p>\n<p>this would give us something similar to the following<\/p>\n<p>drwx&#8212;&#8212;.  3 root   mousse         16 Sep 30  11:05 systemd-private-611c364c72354a2fbb19e7a3484b0262-colord.service-tCwbBC<\/p>\n<p>drwx&#8212;&#8212;.  3 root   mousse         16 Sep 30  11:05 systemd-private-611c364c72354a2fbb19e7a3484b0262-cups.service-zLRpKH<\/p>\n<p>drew&#8212;&#8212;.  3 root   mousse         16 Sep 30  11:05 systemd-private-611c364c72354a2fbb19e7a3484b0262-rtkit-daemon.service-I6FhXd<\/p>\n<p>From this we can see the directories are owned by root with read write and execute permissions however the group mousse and everyone else has no rights at all.this means no one apart from root can gain access to this directory or anything in it.<\/p>\n<p>Now if we wanted to be able to look at these files or directories with the additional information of size and date order we would issue the following<\/p>\n<p>for files<\/p>\n<p><em>ls -larth<\/em><\/p>\n<p>this would give something similar to the following<\/p>\n<p>-rw-r&#8211;r&#8211;. 1 root root    107 Sep 22 11:54 storage.log<\/p>\n<p>-rw-r&#8211;r&#8211;. 1 root root    108 Sep 22 11:57 packaging.log<\/p>\n<p>-rw-r&#8211;r&#8211;. 1 root root    57 Sep 22 11:59 ifcfg.log<\/p>\n<p>as you can see the sizes are listed plus the date and time that were last written too handy if you have an error and you can see which log was written to last good place to start your detective work.<\/p>\n<p>Now if we do the same for directories but this time i will choose the \/var directory where logs are written as an example<\/p>\n<p><em>ls -larthd \/var *\/<\/em><\/p>\n<p>You should get something similar to this<\/p>\n<p>drwxr-xr-x.  2 mickey mousse    6 Sep 22 12:06 hsperfdata_nb1302\/<\/p>\n<p>drwxr-xr-x.  2 root   mousse       18 Sep 22 12:07 hsperfdata_root\/<\/p>\n<p>drwx&#8212;&#8212;.  2 mickey mousse   51 Sep 22 16:41 mozilla_nb13020\/<\/p>\n<p>drwx&#8212;&#8212;.  2 mickey mousse   6 Sep 22 16:45 tracker-extract-files.10000\/<\/p>\n<p>drwxr-xr-x. 20 root   mousse  4.0K Sep 30 11:04 \/var<\/p>\n<p>drwx&#8212;&#8212;.  3 root   mousse      16 Sep 30 11:05 systemd-private-611c364c72354a2fbb19e7a3484b0262-rtkit-daemon.service-I6FhXd\/<\/p>\n<p>drwx&#8212;&#8212;.  3 root   mousse      16 Sep 30 11:05 systemd-private-611c364c72354a2fbb19e7a3484b0262-cups.service-zLRpKH\/<\/p>\n<p>drwx&#8212;&#8212;.  3 root   mousse      16 Sep 30 11:05 systemd-private-611c364c72354a2fbb19e7a3484b0262-colord.service-tCwbBC\/<\/p>\n<p>drwx&#8212;&#8212;.  2 mickey mousse  23 Sep 30 11:05 ssh-ZbJbou1039oX\/<\/p>\n<p>hope this helps. Hopefully we will be publishing more tips soon feel free to contact us through our web page www.blueshell.im or follow us on <a href=\"https:\/\/twitter.com\/BlueShellSupport\">twitter<\/a> or our <a href=\"https:\/\/www.facebook.com\/Blue-Shell-Limited-1228991333830285\/?fref=ts\">Facebook page<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>We were asked recently by a customer how to list only the directories when doing an ls command <a href=\"http:\/\/www.blueshell.im\/Blog\/?p=286\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":3,"featured_media":151,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[8],"tags":[],"_links":{"self":[{"href":"http:\/\/www.blueshell.im\/Blog\/index.php?rest_route=\/wp\/v2\/posts\/286"}],"collection":[{"href":"http:\/\/www.blueshell.im\/Blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.blueshell.im\/Blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.blueshell.im\/Blog\/index.php?rest_route=\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"http:\/\/www.blueshell.im\/Blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=286"}],"version-history":[{"count":37,"href":"http:\/\/www.blueshell.im\/Blog\/index.php?rest_route=\/wp\/v2\/posts\/286\/revisions"}],"predecessor-version":[{"id":509,"href":"http:\/\/www.blueshell.im\/Blog\/index.php?rest_route=\/wp\/v2\/posts\/286\/revisions\/509"}],"wp:featuredmedia":[{"embeddable":true,"href":"http:\/\/www.blueshell.im\/Blog\/index.php?rest_route=\/wp\/v2\/media\/151"}],"wp:attachment":[{"href":"http:\/\/www.blueshell.im\/Blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=286"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.blueshell.im\/Blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=286"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.blueshell.im\/Blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=286"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}