List Directory Contents Using The Dir Command

The Linux Dir Command
The Linux Dir Command.

Most Linux users will use the ls command for listing files and folders within 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.

Example Use Of The Dir Command

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

dir

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. (i.e .myhiddenfile).

To show hidden files using the dir command use the following switch:

dir -a

dir --all

You may notice when you run the command in this fashion that it lists a file called . and another called ..

The first dot signals the current directory and the two dots signal the previous directory. You can hide these when running the dir command by using the following command:

dir -A

dir --almost-all

How To Display The Author Of A File

You can display the author of the files (people who created the files) by using the following dir command:

dir -l --author

 The -l is required to turn the display into a listing.

How To Hide Backups

When you run certain commands such as the mv command or the cp command you may end up with files which end with a tilde (~).

The tilde at the end of a file suggests a command backed up the original file before creating a new one.

You might not want to see the backed up files when returning a directory listing as these files will just be noise.

To hide them run the following command:

dir -B

dir --ignore-backups

Add A Colour To The Output

If you want to use colours to differentiate between files, folders and links you can use the following switch:

dir --color=always

dir --color=auto

dir --color=never

Format The Output

You can format the output so that it doesn't always appear in a column format.

The options are as follows:

dir --format=across

dir --format=commas

dir --format=horizontal

dir --format=long

dir --format=single-column

dir --format=verbose

dir --format=vertical

Across lists all of the files on each line, commas delimits each item by commas, horizontal is the same as across, long and verbose produce a long listing with lots of other information, vertical is the default output.

You can also get the same effect by using the following switches:

dir -x (same as across and horizontal)

dir -m (same as commas)

dir -l (same as long and verbose)

dir -1 (single-column)

dir -c (vertical)

Return A Long Or Verbose Listing

As shown in the formating section you can get a long listing by running one of these commands:

dir --format=long

dir --format=verbose

dir -l

The long listing returns the following information:

  • permissions
  • inodes
  • owner
  • group
  • file size
  • last access date
  • file name

If you don't want to list the file owner you can use the following command instead:

dir -g

Similarly you can hide groups by using the following command:

dir -G -l

Human Readable File Sizes

By default the file sizes are listed in bytes which was fine about 30 years ago but now with files stretching into the gigabytes it is much better to see the size in a human readable format such as 2.5 G or 1.5 M.

To see the file size in a human readable format use the following command:

dir -l -h

List Directories First

If you want the directories to be shown first and the files afterwards use the following switch:

dir -l --group-directories-first

Hide Files With A Certain Pattern

If you want to hide certain files you can use the following command:

dir --hide=pattern

For instance to produce a directory listing of your music folder but ignore wav files use the following.

dir --hide=.wav

You can achieve a similar effect using the following command:

dir -I pattern

 

Show More Information About Files And Folders

The following command can be used to distinguish between files, folders and links:

dir --indicator-style=classify

This will show folders by add a slash to the end, files have nothing after them, links have an @ symbol at the end and executable files have a * at the end.

The indicator style can be set to these values as well:

  • none (default)
  • slash (directories have slashes)
  • file-type

You can also show folders with slashes at the end by using the following command:

dir -p

You can show file types by using the following command:

dir -F

List All The Files And Folders In Sub-Folders

To get a listing of all the sub-folders and files within those sub-folders you can perform a recursive listing by using the following command:

dir -R

Sorting Output

You can sort the order in which the files and folders are returned by using the following commands:

dir --sort=none

dir --sort=size

dir --sort=time

dir --sort=version

dir --sort=extension

You can also specify the following commands to achieve the same effect:

dir -s (sort by size)

dir -t (sort by time)

dir -v (sort by version)

dir -x (sort by extension)

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.