PUBLISHED ON: FEBRUARY 16, 2023
How to List Directory Contents in Linux ( dir command).
In Linux, if we want to list the contents of a directory, we use the dir
command. The dir
(directory) command is used to list the contents of a directory in the Linux/Unix operating system.
If we execute dir
command, it displays the files and folders in the directory, and if we execute the ls
command we will get a similar result. Both commands are part of Coreutils and, just with different default options. We can't say that dir
is the alias of ls, No command in Linux is an alias of another command. We can say that the dir
command is equivalent to
ls -C -b
i.e., by default files are listed in columns, sorted vertically, and special characters by backslash escape sequences. apart from this the output of dir
command is not colored like ls
command.
The general syntax of dir command
dir [OPTION]... [FILE]...
Brief description of options available with the 'dir
' command.
Option |
Description |
-a, --all |
display all hidden files (starting with ' . ') |
-A, --almost-all |
do not list implied . and .. |
--author |
with -l, print the author of each file |
--block-size=SIZE |
with -l, scale sizes by SIZE before printing them |
-B, --ignore-backups |
ignores the listing of backup files. |
-F, --classify |
append indicator (one of */=>@|) to entries |
--file-type |
likewise, except do not append '*' |
--format=WORD |
across -x, commas -m, horizontal -x, long -l, single-column -l, verbose -l, vertical -C |
-i, --inode |
print the index number of each file |
-r, --reverse |
list files in reverse order while sorting |
-R, --recursive |
list subdirectories recursively |
--help |
display help and exit |
--version |
output version information and exit |
Example: Display hidden files starting with ' . ' using -a or --all option.
In this example, in the directory 'shadow' '.textfile.txt
' file available starting with .
but without using -a
or --all
option these files are not visible using dir
command but using -a
or --all
file is visible.
Example: Display author of the files with -l.
In this example, using dir -l --author
in the 'shadow' directory list of all files with owner details, group details are displayed as shown in the below image.
Example: Display files excluding backup files ( ends with ~).
We know that backup files end with '~
' sign. textfile.txt~
is the backup file in the 'shadow' folder but just using dir
command textfile.txt~
is visible but using -B
or --ignore-backups
textfile.txt~
is not visible.
Example: Use of append indicator.
In this example, dir -F
or --classify
command classifies files into their types.
- '
/
' sign indicates a directory.
- '
*
' sign indicates an executable.
- '
@
' sign indicates a symbolic link.
- '
%
' sign indicates a whiteout.
- '
=
' sign indicates a socket.
- '
|
' sign indicates a FIFO.
(' /
') denotes that 'myfolder' is a directory.
Conclusion
In this tutorial, we covered how to list directory contents in the Linux operating system using the dir
command with available options and suitable examples. In Linux ls
and vdir
are also available for the same purpose.