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.
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!
Table of Contents
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
dircommand (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.
dir command below with no options and no path to list all files and folders in your working directory.
The output below shows you the names of all files and folders in your current directory, in this case, your home (root) 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
- 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
dir ~ /etc
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 (
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:
|.||Link to your current directory|
|. .||Link to your current directory’s parent directory|
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
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
Run the below command to list all directories and files and their detailed information.
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.
|Permissions||Owner||Group Owner||Size||Last Modified Date and Time|
|drwxr-xr-x||root||root||4096||Jul 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.
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.
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:
|Symbol||File Type Equivalent|
||||FIFO (first in, first out) file|
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.
As you can see below, the output shows the list of files and directories in one column, making a list more readable.
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.
As you can see below, all files and directories are listed and separated by commas.
3. Now, run the following command to list all files and directories in –verbose to show more information.
Below, you can see the –format=version option outputs the same as dir -l command.
Ignoring Files with a Specific Pattern
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 (
dir [email protected] -F
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.
Now, run the following command to list all files and directories while ignoring any executable files (
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.
Sorting the Output of the
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
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
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?