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
The general syntax of dir command
dir [OPTION]... [FILE]...
Brief description of options available with the '
||display all hidden files (starting with ' . ')
||do not list implied . and ..
||with -l, print the author of each file
||with -l, scale sizes by SIZE before printing them
||ignores the listing of backup files.
||append indicator (one of */=>@|) to entries
||likewise, except do not append '*'
||across -x, commas -m, horizontal -x, long -l, single-column -l, verbose -l, vertical -C
||print the index number of each file
||list files in reverse order while sorting
||list subdirectories recursively
||display help and exit
||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
--all option these files are not visible using
dir command but using
--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 '
textfile.txt~ is the backup file in the 'shadow' folder but just using
textfile.txt~ is visible but using
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.
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
vdir are also available for the same purpose.