Useful Linux Commands to List Contents of a Directory

Bg Variants

When it comes to Linux commands, there are a ton of them that you can use to run tasks. And if you want to list the contents of a directory just like a file manager, there are a few commands to do so. Let’s check them out.

ls

The most common Linux command to list the content of a directory is ls. By default, ls lists the contents of a directory in columns. Different colors are used to display files and directories.

Variants Ls

Directories created by the user have their names displayed within single quotes. Directories created by the system have their names simply displayed. The colors used to display the names of files, directories and scripts depend on the visual theme used by the terminal.

ls just lists the content of the current directory that you are in. If you want to view the content in another directory, you can add the file path in the command:

ls /add/file/path/here

You can add the -a flag to show hidden files:

ls -a

or the -l and -h flag to have it display in long-list format and human readable format.

ls -lh

Dir

dir, like ls, also lists the contents of the directory in columns. If any names have special characters in them like spaces, then those characters are preceded by a backslash. All the flags developed for ls have also been made available for dir.

Variants Dir

dir behaves similar to the following command:

ls -C -b

where

-C : displays the output in columns
-b : precedes special characters with a backslash

vdir

The next command to show content in a directory is vdir.

Variants Vdir

This command provides a long listing of the contents of the directory. Again, the special characters in names are preceded with a backslash. In the long listing, details about the permissions associated with a file or directory, link count, the owner, group owner, file size, last modification time and file/directory name are displayed.

vdir behaves similarly to the following command:

ls -l -b

where

-l : provides a long listing of directory contents
-b : precedes special characters with a backslash

Why do we have “dir” and “vdir” when “ls” can do the same job?

Shell commands are often used as a part of scripts. The output of such scripts may be displayed on a terminal screen, redirected to a file or simply piped as input to another command. In certain scenarios, ls did not behave as expected due to some performance issues during script execution. To handle this situation, Linux developers came up with two alternate commands that provide the same output as ls and do not run into such performance issues.

Also, some people consider dir to be the Linux equivalent of the DOS command “dir,” which also lists the contents of the directory. However, this is not a correct assumption.

Fun Fact

In the output of ls, the files and folders are color-coded. In the outputs for dir and vdir, the same colors are used throughout. If you are wondering why, the reason lies in the .bashrc file. In the “~/.bashrc” file, which is a hidden file in the home directory, an alias for “ls” has been defined as shown below,

Variants Bashrc

where ls is equivalent to ls --color=auto. The similar aliases for “dir” and “vdir” have been commented out. You can uncomment them to view the outputs with color. The following command will perform the same activity.

dir --color
Variants Dir Color

When it comes to Linux commands, there is often more than one way to get things done. Don’t forget to check out some of the commonly-used commands for new users.

Divya Lakshmanan

Divya divides her time between speculating the existence of aliens and writing about her technical findings.

Subscribe to our newsletter!

Our latest tutorials delivered straight to your inbox