Administration toolsCommands

How to use dir command in linux

The dir command is often considered to be the Windows equivalent but it works in Linux in pretty much the same way.
In this guide, I will show you how to use the dir command in Linux and introduce you to the key switches which can be used to get the most out of it.

Dir Command Syntax

The general syntax of the dir command is as follows:

# dir [OPTION] [FILE]

Example output dir command

To get a list of all the files and folders in the current directory use the dir command as follows:

rasho@Gandalf / $ dir
0     cdrom  home	     lib    lost+found	opt   run   sys  var
bin   dev    initrd.img      lib32  media	proc  sbin  tmp  vmlinuz
boot  etc    initrd.img.old  lib64  mnt		root  srv   usr  vmlinuz.old

A list of files and folders will appear in a column format.

How To Show Hidden Files Using The Dir Command

By default, the dir command only shows normal files and folders. In Linux you can hide a file by making the first character a full stop.
To show hidden files using the dir command use the -a or -all switch:

Example:

rasho@Gandalf / $ dir -a
.			     Music
..			     .nano
.bash_history		     .nv
.bash_logout		     Pictures
.bashrc			     .pki
.cache			     .profile
.cinnamon		     Public
.config			     .purple
.ssh                         .thumbnails
.dbus			     Templates
Desktop	                     .themes

You can include the -l option to format output as a list.

rasho@Gandalf / $ dir -al
total 550676
drwxr-xr-x 40 rasho rasho      4096 Mar  5 08:23 .
drwxr-xr-x  4 root  root       4096 Dec 28 21:00 ..
-rw-------  1 rasho rasho     10967 Mar  4 20:28 .bash_history
-rw-r--r--  1 rasho rasho       220 Dec 28 21:00 .bash_logout
-rw-r--r--  1 rasho rasho      4000 Dec 28 21:00 .bashrc
drwx------ 16 rasho rasho      4096 Mar  4 19:13 .cache
drwxr-xr-x  5 rasho rasho      4096 Mar  5 08:23 .cinnamon
drwxr-xr-x 31 rasho rasho      4096 Mar  4 16:49 .config
drwx------  3 rasho rasho      4096 Dec 28 21:10 .dbus
drwxr-xr-x  7 rasho rasho      4096 Mar  5 13:28 Desktop
-rw-------  1 rasho rasho        29 Mar  5 08:23 .dmrc
-rw-------  1 rasho rasho        29 Feb  5 20:57 .dmrc.ARU4UY

Display the author of a file

You can as well view the author of a file by using the –author flag as follows.

rasho@Gandalf / $ dir -al --author /home/rasho
total 550680
drwxr-xr-x 40 rasho rasho rasho      4096 Mar  5 08:23 .
drwxr-xr-x  4 root  root  root       4096 Dec 28 21:00 ..
-rw-------  1 rasho rasho rasho     10967 Mar  4 20:28 .bash_history
-rw-r--r--  1 rasho rasho rasho       220 Dec 28 21:00 .bash_logout
-rw-r--r--  1 rasho rasho rasho      4000 Dec 28 21:00 .bashrc
drwx------ 16 rasho rasho rasho      4096 Mar  4 19:13 .cache
drwxr-xr-x  5 rasho rasho rasho      4096 Mar  5 08:23 .cinnamon
drwxr-xr-x 31 rasho rasho rasho      4096 Mar  4 16:49 .config
drwx------  3 rasho rasho rasho      4096 Dec 28 21:10 .dbus
drwxr-xr-x  7 rasho rasho rasho      4096 Mar  5 13:28 Desktop
-rw-------  1 rasho rasho rasho        29 Mar  5 08:23 .dmrc
-rw-------  1 rasho rasho rasho        29 Feb  5 20:57 .dmrc.ARU4UY
drwxr-xr-x  7 rasho rasho rasho      4096 Feb 22 23:47 Documents

View entries separated by commas

This can be archived by using -m option.

rasho@Gandalf / $ dir -am /
., .., 0, bin, boot, cdrom, dev, etc, home, initrd.img, initrd.img.old, lib, lib32,
lib64, lost+found, media, mnt, opt, proc, root, run, sbin, srv, sys, tmp, usr, var,
vmlinuz, vmlinuz.old

View human readable file sizes

To see the file size in a human readable format use -h

rasho@Gandalf / $ dir -l -h
total 538M
drwxr-xr-x  7 rasho rasho 4,0K Mar  5 13:28 Desktop
drwxr-xr-x  7 rasho rasho 4,0K Feb 22 23:47 Documents
drwxr-xr-x 21 rasho rasho  36K Mar  5 13:23 Downloads
drwxr-xr-x  2 rasho rasho 4,0K Dec 28 21:10 Music
drwxr-xr-x  8 rasho rasho 4,0K Feb 24 22:46 Pictures
drwxr-xr-x  2 rasho rasho 4,0K Dec 28 21:10 Public
drwxr-xr-x  2 rasho rasho 4,0K Dec 28 21:10 Templates
drwxr-xr-x  2 rasho rasho 4,0K Jan 30 23:49 Videos
drwxr-xr-x  3 rasho rasho 4,0K Feb 12 18:09 VirtualBox\ VMs
-rw-r--r--  1 rasho rasho  14M Nov 22 11:59 wkhtmltox-0.12.4_linux-generic-amd64.tar.xz
-l is required to turn the display into a listing.

List directories first

If you want the directories to be shown first and the files afterwards use --group-directories-first switch:
rasho@Gandalf / $ dir -l --group-directories-first
total 550472
drwxr-xr-x  7 rasho rasho      4096 Mar  5 13:28 Desktop
drwxr-xr-x  7 rasho rasho      4096 Feb 22 23:47 Documents
drwxr-xr-x 21 rasho rasho     36864 Mar  5 13:23 Downloads
drwxr-xr-x  2 rasho rasho      4096 Dec 28 21:10 Music
drwxr-xr-x  8 rasho rasho      4096 Feb 24 22:46 Pictures
drwxr-xr-x  2 rasho rasho      4096 Dec 28 21:10 Public
drwxr-xr-x  2 rasho rasho      4096 Dec 28 21:10 Templates
drwxr-xr-x  2 rasho rasho      4096 Jan 30 23:49 Videos
drwxr-xr-x  3 rasho rasho      4096 Feb 12 18:09 VirtualBox\ VMs
drwxr-xr-x  2 rasho rasho      4096 Feb 16 15:27 work

The -l switch is required to turn the display into a listing.
You can also view subdirectories recursively, meaning that you can list all other subdirectories in a directory using the -R switch as follows.

rasho@Gandalf / $ dir -R
.:
backups  cache	lib  local  lock  log  mail  opt  run  spool  tmp

./backups:
alternatives.tar.0	  dpkg.arch.0		  dpkg.statoverride.2.gz
alternatives.tar.1.gz	  dpkg.arch.1.gz	  dpkg.statoverride.3.gz
alternatives.tar.2.gz	  dpkg.arch.2.gz	  dpkg.statoverride.4.gz

./cache:
app-info	  cracklib  dictionaries-common  ldconfig  pm-utils
apt		  cups	    fontconfig		 libvirt   samba
apt-xapian-index  debconf   fwupdate		 man

./cache/app-info:
xmls

./cache/app-info/xmls:

./cache/apt:
archives  pkgcache.bin	srcpkgcache.bin

Reversing The Order

You can reverse the order in which the files and folders are listed by using the following command:

$ dir -r

Summary

The dir command is very similar to the ls command. It is probably worth learning about the ls command as this is the more commonly available program although most systems include dir as well.

Leave a Reply

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

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

Close
Close