FUNDAMENTALS A Complete Guide for Beginners
The “dir” command in Linux is a versatile tool for managing files and directories in the Linux file system. It provides users with an easy way to list directory contents, organize files, and perform basic file system operations. This command-line interface follows a simple and intuitive syntax to enable users to search directory structures and access file information efficiently.
In this article, I’ll demonstrate the most common uses of the dir command in Linux with relevant examples.
Syntax of “dir” Command
The syntax of the dir command typically follows the following format:
dir [Option] [Directory]
Note: The element enclosed by a square bracket in the above syntax is not mandatory.
- Option: These are optional flags or parameters that modify the behavior of the dir command.
- Directory: You can provide the directory’s path to the dir command to list the contents of a directory that isn’t in your current working directory. This means you don’t need to navigate to the directory first; you can directly specify its path instead of its name.
Options of “dir” Command
As dir is an external command, it does have a man page that can help you get all the required information regarding this command. Just write the following command inside your terminal:
man dir
There are several options associated with the dir command. Some of the most used options are:
Options | Functionality |
---|---|
-l | Displays a long listing format, showing detailed information about files. |
-s -l | Sorts and lists files by size. |
-t -l | Sorts and lists files by modification time. |
-a or – -all / -al | Lists all files, including hidden files starting with “.” |
-A or – -almost-all | Lists all files except “’.” and “..”, which represent current directory and parent directory. |
-d */ | Lists directories only, excluding files. |
-author (-l – -author) | Displays authors of files when available. |
-B or – -ignore-backups | Ignores backup files ending with a “~” |
– -color | Enables colorized output for improved readability. |
-F or – -classify (- -file-type) | Appends indicators to filenames indicating their type (e.g., “/” for directories) |
– -format=WORD | Customizes output format using the specified word format. |
– -hide=PATTERN / – -ignore=PATTERN | Customizes output format using the specified word format. The WORD can take the following values: across (-x), commas, (-m), horizontal (-x), long (-l), single-column (-1), verbose (-l), vertical (-C). |
-n or – -numeric-uid-gid | Displays numeric user and group IDs instead of names. |
-r or – -reverse | Lists entries in reverse order. |
-R or – -recursive | Lists subdirectories recursively. |
– -sort=PARAMETER | Specifies the parameter for sorting the output. The PARAMETER can take the following values: none (-U), size (-S), time (-t), version (-v), extension (-X) |
– -help | Displays usage information and options. |
– -version | Displays version information. |
-h -l | Lists file sizes in a human-readable format. |
-i, – -inode | Displays inode or index numbers of files. |
-g, -gG | -g lists files without their owners and -G lists files without their group owners. |
-l – -group-directories-first | Lists directories first in the output, before files. |
Difference Between “dir” and “ls” Command
Similar to the ls command, the Linux dir tool lists the contents of directories, but with a few significant differences. By default, dir organizes the output in columns, sorted vertically, and represents special characters using backslash escape sequences. Nevertheless, dir doesn’t produce colored output in the terminal as ls does. Furthermore, dir is exclusive to some distributions like Ubuntu and might not be available on other computers, whereas ls is extensively used and accessible on a variety of Unix-like operating systems.
Practical Examples Using “dir” Command in Linux
Below, I’ll outline a handful of practical applications and examples of using the dir command so that you can gain a deeper understanding of this topic and learn to use the dir command in Linux more effectively:
1. Listing Files and Directories
Listing the contents of a directory is the primary task of the dir command. There are a lot of options associated of this command through which you can manipulate the listing process. Some of the most useful options with their use cases are:
1.1. Listing Files and Directories in Current Directory
To list all the files and directories inside the current directory, simply run the dir command without any arguments:
dir
Here, all the contents (except the hidden ones) of the current directory are listed.
1.2. Listing Files & Directories Through Definite Path
To list all the files and directories of any directory of the system while working in a different directory, you can write dir [directory_path]
.
For instance, to list all the contents of the Desktop directory from a different directory (Home), run the following command:
dir /home/soumik/Desktop
1.3. Listing Files With Detailed Information
To list all the contents of a directory including their detailed information such as permission details, owners & groups, file size, modification time, etc, run the following command:
dir -l
1.4. Listing Files With Sorted Detailed Information
To list all the contents of a directory and sort them according to a custom parameter, you can write: dir -l --sort=PARAMETER
. Some of the common sorting parameters are none (-U), size (-S), time (-t), version (-v), extension (-X), etc.
For example, if you want to sort the contents of the current directory by time, run the following command:
dir -l --sort=time
There is a shorter way of writing the sort command with desired sorting options:
dir -l -s -t
This command also sorts the contents of the working directory by time (descending order).
1.5. Listing & Highlighting Contents With Separate Colors
By default, the dir command does not color the directories differently as the ls command does. But if you want to separate the directories from the files by highlighting them, run the following command:
dir --color
In the above image, you can see that the directories are colored blue.
1.6. Listing All Files (Including Hidden)
The dir command by default does not list the hidden contents inside a directory. If you want the hidden files to be also included inside the list, run the following command:
dir -a
Again, if you want to list the hidden files but leave out the ‘.’ (current directory) and ‘..’ (parent directory) directories, run the following command:
dir -A
1.7. Listing Contents With File Type Indicators
The dir command uses some special characters that indicate the content type. They are:
Indicator Character | Indications |
---|---|
/ | Indicates a directory. |
* | Indicates an executable file. |
@ | Indicates a symbolic link. |
% | Indicates a whiteout. |
= | Indicates a socket. |
| | Indicates a FIFO (First In, First Out) |
To display the contents of the working directory including their file type indicators run the following command:
dir -F
In the image above, the directories are listed with a ‘/’ (backslash) character to their right. So, according to the output, “New Folder”, “hello”, and “world” are 3 directories inside the desktop.
1.8. Listing File Details in Human-Readable Format
To make data easier for people to grasp, the human-readable format usually converts bytes to bigger units like kilobytes (KB), megabytes (MB), or gigabytes (GB). This allows for a quicker understanding of file sizes and resource use.
To list all the files inside the working directory with their corresponding details in human-readable format, run the following command:
dir -h -l
1.9. Listing Directories Only
The dir command in Linux doesn’t have a built-in option to filter directories only, but you can achieve the desired result by combining it with wildcards.
To list only directories and leave out other content types, run the following command:
dir -d */
1.10. Listing Contents With Index Number
An inode number, short for “index node number,” is a unique identifier assigned to each file or directory in a Unix-like file system. It serves as a reference or index to locate and access the associated data blocks and metadata of the file or directory.
In Linux, the dir -i
or dir --inode
command displays the inode numbers of files and directories in the current directory. For example, run the following command:
dir -i
In the above image, all the files and directories are listed and separated with a unique inode number.
1.11. Listing Contents Without Owners
If you want to list the files and directories with detailed information but intentionally leave out the owner’s names run the following command:
dir -g
This way the command will only show the group owner names for each content. But if you also want to leave out the group owner’s names along with the owner’s names run the following command:
dir -gG
1.12. Listing Directories Before Others
To list the directories before the other contents inside the working directory, run the following command:
dir -l --group-directories-first
In the above image, you can see the directories listed on top of the output.
Note: The -l
option was used to display the files with details that help you identify the directories from other types of content. If you do not need that, just the command dir --group-directories-first
would do the job.
1.13. Listing Contents in Reverse Order
By default, the dir command will list the contents of the working directory in alphabetical order. But, if you want that order to be reversed and generate an inverted output, run the following command:
dir -r
1.14. Listing Contents With User & Group IDs
If you want the user and group IDs to be listed alongside the contents, instead of their user and group names, run the following command:
dir -n
1.15. Listing Contents Including Authors
By default, the dir command does not display the author names of the contents. The file owner name refers to the user account that owns the file, while the author name typically refers to the individual who created or authored the content of the file.
To display the contents with author names along with owner and group names, run the following command:
dir -l --author
In the above image, all the contents are displayed with their corresponding owner and group names. But, if you want to display only the author names, not the owner and group names, you can run the command dir -author
instead.
2. Changing Default Output Format
By default, the dir command displays the output in a horizontal format. The dir command’s -format=WORD
option adjusts the output appearance. Where, WORD options represent “across,” “commas,” “horizontal,” “long,” “single-column,” “verbose,” and “vertical,” corresponding to -x
, -m
, -x
, -l
, -1
, -l
, -C
options respectively.
For example, to display the output in a single column, run the following command:
dir --format=single-column
3. Viewing Contents Recursively
Recursive viewing involves examining directory contents and subdirectories, continuing through each level of nested directories to explore all files.
To list the contents of the working directory recursively, run the following command:
dir -R
In the above image, all the contents inside nested directories are displayed separately. For example: inside the current directory, there is a directory named “hello” and that directory has another file inside it named “hello there”.
4. Ignoring Contents of Specific Patterns
Suppose, you want to ignore or hide some contents of specific patterns while listing the contents of a directory using the dir command. For example: if you wish to ignore all the files inside the working directory containing a .txt extension, run the following 1 of 2 commands:
dir --ignore=*.txt
Or,
dir --hide=*.txt
Both commands have similar functionality. Here, the asterisk (*) acts as a wildcard. A wildcard is a symbol or character that represents one or more other characters, allowing for flexible pattern matching in commands or search operations. Here, *.txt represents any sequence of characters in file names with the “.txt” extension.
Conclusion
In this article, I have explored the features of the Linux “dir” command, showing how useful it is for accessing and managing files and directories through various practical examples. I urge readers to learn more about the “dir” command and use it along with other file management programs to customize their file navigation processes to suit their own requirements and tastes. Thank you!
People Also Ask
What is a directory in Linux?
In Linux, a directory is a type of file that organizes and stores other files and directories. It serves as a container for organizing and structuring the file system hierarchy.
Can I search for files with specific names or patterns using the ‘dir’ command?
No, the “dir” command in Linux is primarily used for listing files and directories in a directory, and it does not include built-in search functionality for specific names or patterns. However, you can use other commands such as “find” or “grep” to search for files with specific names or patterns in Linux.
How do I navigate to a specific directory using the ‘dir’ command?
The “dir” command does not allow direct navigation to a specific directory. Instead, you can use the “cd” (change directory) command followed by the directory path to navigate to a specific directory in Linux.