How to use dir command with Various Arguments and Options in Linux

To use dir command with Various Arguments and Options in Linux

dir command is used to view the contents of directory with various Arguments and options are explained with examples in this tutorial.

 

Syntax

dir [OPTION] [FILE]

 

The sample example for dir Command usage is as follows,

Output

[user1@linuxhelp ~]$ dir / 
bin   dev  home  lib64         media  opt   root    sbin  sys  usr
boot  etc  lib     lost+found  mnt    proc  run    srv   tmp  var

 

To view the /home directory file

[user1@linuxhelp ~]$ dir /home
user1

 

Another example to view /etc directory

[user1@linuxhelp ~]$ dir /etc 
abrt               gssproxy           profile
adjtime            hba.conf           profile.d
aliases            highlight          protocols
alsa               host.conf          pulse
alternatives       hostname           purple
anacrontab         hosts              qemu

 

 

In order to list single file per line, use -1 option as like below,

Without viewing single line in file

[user1@linuxhelp ~]$ dir
Desktop    Music     Templates
Documents  Pictures  Videos
Downloads  Public    wkhtmltopdf-0.10.0_rc2-static-amd64.tar.bz2

 

With using single line file

[user1@linuxhelp ~]$ dir -1
Desktop
Documents
Downloads
Music
Pictures
Public
Templates
Videos

 

To view all the files including hidden files in a directory

Use -a option,is used to view the hidden files and also can include -1 option to format output as like in the command line below.

[user1@linuxhelp ~]$ dir -a
.              Desktop        Music
..             Documents      Pictures
.bash_history  Downloads      Public
.bash_logout   .esd_auth      .ssh
.bash_profile  .gnupg         Templates
.bashrc        .ICEauthority  Videos
.cache         .local         wkhtmltopdf-0.10.0_rc2-static-amd64.tar.bz2
.config        .mozilla

[user1@linuxhelp ~]$ dir -al
total 10888
drwx------. 16 user1 user1     4096 Mar 23 07:32 .
drwxr-xr-x.  3 root  root      4096 Sep 10  2015 ..
-rw-------.  1 user1 user1     121 Mar 22 12:15 .bash_history
-rw-r--r--.  1 user1 user1     18 Jan 23  2015 .bash_logout
-rw-r--r--.  1 user1 user1     193 Jan 23  2015 .bash_profile
-rw-r--r--.  1 user1 user1     231 Jan 23  2015 .bashrc
drwx------. 18 user1 user1     4096 Mar 22 12:28 .cache
drwx------. 15 user1 user1     4096 Mar 21 19:58 .config
drwxr-xr-x.  2 user1 user1     4096 Mar 21 14:22 Desktop
drwxr-xr-x.  2 user1 user1     4096 Mar 22 18:07 Documents
drwxr-xr-x.  2 user1 user1     4096 Mar 21 14:22 Downloads

 

To view directory entries instead of content

We can use the -d option to show only directory entries and -dl option to show a long listing of the directory like, group owner, owner and permissions.

[user1@linuxhelp ~]$ dir -d /etc
/etc

[user1@linuxhelp ~]$ dir -dl /etc
drwxr-xr-x. 132 root root 12288 Mar 23 07:30 /etc

 

To view the file index number

We use the -i option to view the index or inode number of the file.

[user1@linuxhelp ~]$ dir -il
total 10804
687460 drwxr-xr-x. 2 user1 user1     4096 Mar 21 14:22 Desktop
687464 drwxr-xr-x. 2 user1 user1     4096 Mar 22 18:07 Documents
687461 drwxr-xr-x. 2 user1 user1     4096 Mar 21 14:22 Downloads
687465 drwxr-xr-x. 2 user1 user1     4096 Mar 21 14:22 Music
687466 drwxr-xr-x. 2 user1 user1     4096 Mar 21 14:22 Pictures
687463 drwxr-xr-x. 2 user1 user1     4096 Mar 21 14:22 Public
687462 drwxr-xr-x. 2 user1 user1     4096 Mar 21 14:22 Templates
687467 drwxr-xr-x. 2 user1 user1     4096 Mar 21 14:22 Videos
658856 -rw-rw-r--. 1 user1 user1 10995568 Jan 31  2011 wkhtmltopdf-0.10.0_rc2-static-amd64.tar.bz2

 

To list the files according to their allocated sizes in blocks

We use the -s option to view files sizes and -S option to sort files with respect to their size. Use -h option to show files in a human-readable format.
Output

[user1@linuxhelp ~]$ dir -shl
total 11M
8.0K drwxr-xr-x. 2 user1 user1 4.0K Mar 21 14:22 Desktop
8.0K drwxr-xr-x. 2 user1 user1 4.0K Mar 22 18:07 Documents
8.0K drwxr-xr-x. 2 user1 user1 4.0K Mar 21 14:22 Downloads
8.0K drwxr-xr-x. 2 user1 user1 4.0K Mar 21 14:22 Music
8.0K drwxr-xr-x. 2 user1 user1 4.0K Mar 21 14:22 Pictures
8.0K drwxr-xr-x. 2 user1 user1 4.0K Mar 21 14:22 Public
8.0K drwxr-xr-x. 2 user1 user1 4.0K Mar 21 14:22 Templates
8.0K drwxr-xr-x. 2 user1 user1 4.0K Mar 21 14:22 Videos
11M -rw-rw-r--. 1 user1 user1  11M Jan 31  2011 wkhtmltopdf-0.10.0_rc2-static-amd64.tar.bz2

 

The first column shows the file size in Kilobyte.
Sort by file size using -S option.

Output

[user1@linuxhelp ~]$ dir -Sshl
total 11M
11M -rw-rw-r--. 1 user1 user1  11M Jan 31  2011 wkhtmltopdf-0.10.0_rc2-static-amd64.tar.bz2
8.0K drwxr-xr-x. 2 user1 user1 4.0K Mar 21 14:22 Desktop
8.0K drwxr-xr-x. 2 user1 user1 4.0K Mar 22 18:07 Documents
8.0K drwxr-xr-x. 2 user1 user1 4.0K Mar 21 14:22 Downloads
8.0K drwxr-xr-x. 2 user1 user1 4.0K Mar 21 14:22 Music

 

Using -t option, we can sort the files according to modification time.

[user1@linuxhelp ~]$ dir -ashlt
total 11M
8.0K drwx------. 16 user1 user1 4.0K Mar 23 07:32 .
8.0K -rw-------.  1 user1 user1 4.0K Mar 23 07:32 .ICEauthority
8.0K drwxr-xr-x.  2 user1 user1 4.0K Mar 22 18:07 Documents
8.0K drwx------. 18 user1 user1 4.0K Mar 22 12:28 .cache
8.0K -rw-------.  1 user1 user1  121 Mar 22 12:15 .bash_history
4.0K drwx------. 15 user1 user1 4.0K Mar 21 19:58 .config
4.0K drwx------.  2 user1 user1 4.0K Mar 21 16:55 .gnupg
4.0K drwxr-xr-x.  5 user1 user1 4.0K Mar 21 15:16 .mozilla

 

To show files without their owner or group owner

We have to use -g option to get the list of files without owner and -G option to get files without group owner.

[user1@linuxhelp ~]$ dir -ahgG /home/user1/
total 11M
drwx------. 16 4.0K Mar 23 07:32 .
drwxr-xr-x.  3 4.0K Sep 10  2015 ..
-rw-------.  1  121 Mar 22 12:15 .bash_history
-rw-r--r--.  1   18 Jan 23  2015 .bash_logout
-rw-r--r--.  1  193 Jan 23  2015 .bash_profile
-rw-r--r--.  1  231 Jan 23  2015 .bashrc
drwx------. 18 4.0K Mar 22 12:28 .cache

 

If we need author of the file to display, then use –author option.

[user1@linuxhelp ~]$ dir  -al --author 
total 10888
drwx------. 16 user1 user1 user1     4096 Mar 23 07:32 .
drwxr-xr-x.  3 root  root  root      4096 Sep 10  2015 ..
-rw-------.  1 user1 user1 user1     121 Mar 22 12:15 .bash_history
-rw-r--r--.  1 user1 user1 user1     18 Jan 23  2015 .bash_logout
-rw-r--r--.  1 user1 user1 user1     193 Jan 23  2015 .bash_profile
-rw-r--r--.  1 user1 user1 user1     231 Jan 23  2015 .bashrc
drwx------. 18 user1 user1 user1     4096 Mar 22 12:28 .cache
drwx------. 15 user1 user1 user1     4096 Mar 21 19:58 .config
drwxr-xr-x.  2 user1 user1 user1     4096 Mar 21 14:22 Desktop

 

To list directories

Using -R option, we can also list in the sub directories files first.

[user1@linuxhelp ~]$ dir -R
.:
Desktop    Music     Templates
Documents  Pictures  Videos
Downloads  Public    wkhtmltopdf-0.10.0_rc2-static-amd64.tar.bz2

./Desktop:

./Documents:
fedora update.odt  wkhtmltopdf.odt

./Downloads:

./Music:

./Pictures:

./Public:

./Templates:

./Videos:

In output snap, (.) symbol is the current directory

 

To view IDs instead of user and group names

Using -n option, we can see the ids of user.

For example, without -n option.

[user1@linuxhelp ~]$ dir -l --author 
total 10804
drwxr-xr-x. 2 user1 user1 user1     4096 Mar 21 14:22 Desktop
drwxr-xr-x. 2 user1 user1 user1     4096 Mar 22 18:07 Documents
drwxr-xr-x. 2 user1 user1 user1     4096 Mar 21 14:22 Downloads
drwxr-xr-x. 2 user1 user1 user1     4096 Mar 21 14:22 Music
drwxr-xr-x. 2 user1 user1 user1     4096 Mar 21 14:22 Pictures
drwxr-xr-x. 2 user1 user1 user1     4096 Mar 21 14:22 Public
drwxr-xr-x. 2 user1 user1 user1     4096 Mar 21 14:22 Templates
drwxr-xr-x. 2 user1 user1 user1     4096 Mar 21 14:22 Videos
-rw-rw-r--. 1 user1 user1 user1 10995568 Jan 31  2011 wkhtmltopdf-0.10.0_rc2-static-amd64.tar.bz2

 

With -n option

[user1@linuxhelp ~]$ dir -nl --author 
total 10804
drwxr-xr-x. 2 1000 1000 1000     4096 Mar 21 14:22 Desktop
drwxr-xr-x. 2 1000 1000 1000     4096 Mar 22 18:07 Documents
drwxr-xr-x. 2 1000 1000 1000     4096 Mar 21 14:22 Downloads
drwxr-xr-x. 2 1000 1000 1000     4096 Mar 21 14:22 Music
drwxr-xr-x. 2 1000 1000 1000     4096 Mar 21 14:22 Pictures
drwxr-xr-x. 2 1000 1000 1000     4096 Mar 21 14:22 Public
drwxr-xr-x. 2 1000 1000 1000     4096 Mar 21 14:22 Templates
drwxr-xr-x. 2 1000 1000 1000     4096 Mar 21 14:22 Videos
-rw-rw-r--. 1 1000 1000 1000 10995568 Jan 31  2011 wkhtmltopdf-0.10.0_rc2-static-amd64.tar.bz2

 

To view entries spilted by commas

Using -m option, we can separate the entries as follows.

[user1@linuxhelp ~]$ dir -am
., .., .bash_history, .bash_logout, .bash_profile, .bashrc, .cache, .config,
Desktop, Documents, Downloads, .esd_auth, .gnupg, .ICEauthority, .local,
.mozilla, Music, Pictures, Public, .ssh, Templates, Videos,
wkhtmltopdf-0.10.0_rc2-static-amd64.tar.bz2

 

In dir command, for help use --help option and for version use --version option.

 


Thank you! for using Linux Help.

You find this tutorial helpful? Share with your friends to keep it alive.
For more help topics browse our website www.linuxhelp.com
Be the first to comment, we value your suggestions. For further queries please comment below.

Tags: dir command
user image Author :  Jackson