Lesson 15: dir Command
In this lesson, you'll learn how to use the dir command to list the contents of a directory in Linux.
The dir command lists the contents of a directory. It is not a commonly used command in Linux, but it works similarly to the ls command which most Linux users prefer to use.
dir Command Syntax
$ dir [OPTION] [FILE]
dir Command Options
| Option | Description |
|---|---|
-1 |
List one file per line |
-a |
List all files including hidden files |
-l |
Format output as a long list with detailed information |
-d |
List directory entries instead of directory contents |
-i |
Show the index number (inode) of each file |
-s |
Show the size of each file |
-S |
Sort files by size |
-h |
Display file sizes in human-readable format |
-t |
Sort files by modification time |
-g |
List files without the file owner |
-G |
List files without the group owner |
-n |
Show numeric user and group IDs |
-m |
List files separated by commas |
-R |
List directories and subdirectories recursively |
--author |
Show the author of each file |
--group-directories-first |
List directories before files |
--help |
Display help information |
--version |
Display version information |
1. List Files and Directories
The most common usage of the dir command is to display the contents of a directory in ascending alphabetical order.
If you run the dir command without any options or location of the directory, it will display all files and directories in your current working directory.
$ dir
Desktop Documents Downloads Music Pictures Public Videos
2. List Files in a Particular Path
If you want to list all the files and directories of a particular path, then append the path of your directory as shown.
Here, we will list files and directories under the /etc directory.
$ dir /etc
adduser.conf bash.bashrc cron.d default environment fstab hostname
hosts hosts.allow hosts.deny issue mtab networks passwd profile
3. List One File Per Line
To list one file per line, use the -1 option as follows.
$ dir -1 /etc
adduser.conf
bash.bashrc
cron.d
default
environment
fstab
hostname