We were asked recently by a customer how to list only the directories when doing an ls command
Firstly ls lists all files in a directory so an ls would produce something like this
ls /tmp
storage.log ifcfg.log packaging.log
if we now wanted to list only directories we would issue the command
ls -d */
this would give something like this
systemd-private-611c364c72354a2fbb19e7a3484b0262-colord.service-tCwbBC/
systemd-private-611c364c72354a2fbb19e7a3484b0262-cups.service-zLRpKH/
systemd-private-611c364c72354a2fbb19e7a3484b0262-rtkit-daemon.service-I6FhXd/
if we now wanted to see permissions,ownerships and dates we would do the following
for files we would issue the command
ls -la
this would give something similar to the following
-rw-r–r–. 1 root mousse 0 Sep 22 11:53 ifcfg.log
-rw-r–r–. 1 root mousse 0 Sep 22 11:53 packaging.log
-rw-r–r–. 1 root mousse 0 Sep 22 11:53 storage.log
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.
if we wanted to do this for directories we would issue the following command
ls -dal */
this would give us something similar to the following
drwx——. 3 root mousse 16 Sep 30 11:05 systemd-private-611c364c72354a2fbb19e7a3484b0262-colord.service-tCwbBC
drwx——. 3 root mousse 16 Sep 30 11:05 systemd-private-611c364c72354a2fbb19e7a3484b0262-cups.service-zLRpKH
drew——. 3 root mousse 16 Sep 30 11:05 systemd-private-611c364c72354a2fbb19e7a3484b0262-rtkit-daemon.service-I6FhXd
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.
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
for files
ls -larth
this would give something similar to the following
-rw-r–r–. 1 root root 107 Sep 22 11:54 storage.log
-rw-r–r–. 1 root root 108 Sep 22 11:57 packaging.log
-rw-r–r–. 1 root root 57 Sep 22 11:59 ifcfg.log
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.
Now if we do the same for directories but this time i will choose the /var directory where logs are written as an example
ls -larthd /var */
You should get something similar to this
drwxr-xr-x. 2 mickey mousse 6 Sep 22 12:06 hsperfdata_nb1302/
drwxr-xr-x. 2 root mousse 18 Sep 22 12:07 hsperfdata_root/
drwx——. 2 mickey mousse 51 Sep 22 16:41 mozilla_nb13020/
drwx——. 2 mickey mousse 6 Sep 22 16:45 tracker-extract-files.10000/
drwxr-xr-x. 20 root mousse 4.0K Sep 30 11:04 /var
drwx——. 3 root mousse 16 Sep 30 11:05 systemd-private-611c364c72354a2fbb19e7a3484b0262-rtkit-daemon.service-I6FhXd/
drwx——. 3 root mousse 16 Sep 30 11:05 systemd-private-611c364c72354a2fbb19e7a3484b0262-cups.service-zLRpKH/
drwx——. 3 root mousse 16 Sep 30 11:05 systemd-private-611c364c72354a2fbb19e7a3484b0262-colord.service-tCwbBC/
drwx——. 2 mickey mousse 23 Sep 30 11:05 ssh-ZbJbou1039oX/
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 twitter or our Facebook page