Learning the dir Command in Linux Through Examples

Published:21 October 2022 - 7 min. read

Michael Nguyen Tu Image

Michael Nguyen Tu

Read more tutorials by Michael Nguyen Tu!

Block over 3 billion compromised passwords & strengthen your Active Directory password policy. Try Specops Password Policy for free!

As a system administrator, you’d typically work most of the time in a command line environment, and one essential skill is navigating your file system. With the dir command in Linux, you’re one step closer to mastering that skill.

The dir command allows you to view the contents of a directory, a handy command to know in finding files and folders on your system. And in this tutorial, you’ll learn how to navigate your file system through a series of examples.

Ready? Stay tuned and never be astray in finding your files and folders!

Prerequisites

This tutorial will be a hands-on demonstration. If you’d like to follow along, be sure you have a Linux system. This tutorial uses Ubuntu 20.04, but any modern Linux distribution will work.

Listing Files and Folders (Basic dir Command in Linux)

The most basic use of the dir command is to list the files and folders in the current directory, which is handy, especially if you’re working in a terminal.

The basic syntax for the dir command is as follows where:

  • <path> – is the path to the directory you want to list
  • <option> – is an optional parameter to control the behavior of the dir command (will be covered in more detail later).
dir <path> <option>

Suppose you want to see all files and folders under the current working directory. If so, you’ll only need the bare minimum.

Run the dir command below with no options and no path to list all files and folders in your working directory.

dir

The output below shows you the names of all files and folders in your current directory, in this case, your home (root) directory.

Listing all files and folders in the working directory
Listing all files and folders in the working directory

Listing Files and Directories in a Specific Path

Perhaps you want to list all files and folders in a specific path rather than the ones in your current working directory. In that case, appending the <path> option comes in handy.

You can use an absolute path or a relative path with the dir command:

  • An absolute path is a full path from the root of your file system.
  • A relative path is a path from your current working directory.

Run the below dir command to list files and directories under the /etc directory.

dir ~ /etc
Listing files and directories under a specific path
Listing files and directories under a specific path

Listing Hidden Files and Directories

You may have noticed that when you run the dir command without any options, you’ll only see visible files listed in the output. But what about the hidden files?

In Linux, hidden files and directories start with a ‘.’ (dot) symbol. These files and directories are usually configuration files or folders.

Run the following dir command to list all files and directories, including the hidden ones (-a).

dir -a

The output below shows all files and directories in your current directory, including hidden ones such as .bashrc, a configuration file for the Bash shell.

You can also see two symbolically linked (symlinks) directories at the beginning of the list, which are called:

SymlinkDescription
.Link to your current directory
. . Link to your current directory’s parent directory
Listing hidden files and directories
Listing hidden files and directories

Perhaps you prefer to exclude the symlinks from the list. If so, run the same dir command, but this time, append the -A option in uppercase. dir -A

Excluding the symlinks from the list
Excluding the symlinks from the list

Retrieving Detailed List of Files and Directories

The output from running the dir command is helpful but can be confusing and challenging to determine which is which, especially when there are many files and directories. As a solution, append the -l option in your dir command.

Run the below command to list all directories and files and their detailed information.

dir -l

As you can see below, each file and directory is listed on its own line. Notice each item’s format (Desktop, for example) below.

This behavior is advantageous when you want to take a quick look at the permissions on a file or directory.

PermissionsOwnerGroup OwnerSizeLast Modified Date and Time
drwxr-xr-xrootroot4096Jul 16 16:45

Some of these columns might not have any information. Why? Not all file types have permissions, owners, or groups. For example, symbolic links don’t have any of these attributes.

Listing detailed files and directories
Listing detailed files and directories

Showing File Type Information

For more experienced Linux users, knowing what file type you’re dealing with can be helpful. If you’re unsure of the file type (i.e., a regular file or a directory), appending the -F option with the dir command will do the trick.

Run the following dir command to list all files and directories, including their file type.

dir -F

As you can see below, for each file and directory listed, there is a symbol at the end of the line that indicates their file type.

The most common symbols are as follows:

SymbolFile Type Equivalent
/Directory
*Executable File
@Symbolic Link
=Socket
|FIFO (first in, first out) file
Showing file type information
Showing file type information

Customizing Output Format

By default, the dir command’s output is formatted in columns. But if you want to see the output differently, the --format option can help.

1. Run the below dir command to list the files and directories in a single column.

dir --format=single-column

As you can see below, the output shows the list of files and directories in one column, making a list more readable.

Listing the files and directories in a single column
Listing the files and directories in a single column

2. Next, run the same dir command below to list all files and directories. But this time, change the –format option’s value to commas.

This command changes the output’s format to comma-separated values, which can be used for machine parsing.

dir --format=commas

As you can see below, all files and directories are listed and separated by commas.

Listing files in comma-separated values
Listing files in comma-separated values

3. Now, run the following command to list all files and directories in –verbose to show more information.

dir --format=verbose

Below, you can see the –format=version option outputs the same as dir -l command.

Using the --format=verbose option to show even more information
Using the –format=verbose option to show even more information

Ignoring Files with a Specific Pattern

The dir command is often used in Bash scripts, to ignoring certain file types might be helpful to speed up the script execution. For example, if you’re looking for all the regular files in a directory, you might want to ignore any symbolic links. Lucky for you, the --ignore option lets you do the magic.

Run the below dir command to list all files and directories in the current directory, except for any symbolic links (@).

As you can see below, only regular files and directories are listed, and any symbolic links that might be present in the directory are ignored.

Ignoring symbolic links
Ignoring symbolic links

Now, run the following command to list all files and directories while ignoring any executable files (*.exe).

dir --ignore=*.exe -F

You can confirm in the output below that this command extensively narrows files and directory listings since you’re excluding executable files.

Ignoring any executable files
Ignoring any executable files

Sorting the Output of the dir Command

Naturally, when you run the dir command, files and directories are listed in ascending order alphabetically. But perhaps you want to list the files and directories in a certain order, for example, in size order or by the last modified date.

In that case, you can use other options for the dir command to sort the list of files and directories in the output.

Run the below command to list all files and directories in the current directory by size, with the largest ones listed first.

dir -S -l
Listing all of the files and directories by size,
Listing all of the files and directories by size,

Finally, run the following dir command to list files and directories sorted by time. Note that the topmost file or directory is the one that was most recently modified.

dir -t -l
Listing files and directories sorted by date and time
Listing files and directories sorted by date and time

Conclusion

The essential skill any Linux user needs is the ability to list the files and directories in their file system. And In this tutorial, you’ve looked at some of the more common options for the dir command in Linux to navigate your file system effectively.

At this point, you should be confident enough to work with your file system via your terminal as long as you have the dir command. Why not try writing a script that automatically finds files and directories to manage on your system?

Hate ads? Want to support the writer? Get many of our tutorials packaged as an ATA Guidebook.

Explore ATA Guidebooks

Looks like you're offline!