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:

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

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

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:

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:

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.

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.

4 comments

  1. “By default, ls lists the contents of a directory in columns. Different colors are used to display files and directories”

    Sorry, but by default, ‘ls’ DOES NOT colourize its output. In order to have colourized output, first the environment variable ‘LS_COLORS’ must be set. Then one of the following must be done:
    – type “ls –color”.
    – create aliases that override ‘ls’ and supply the ‘–color=auto’ option (‘alias ls=”ls –color=auto”‘, ‘alias lsa=”ls –color=auto -a”‘, etc.). Note that “auto” detects whether a terminal or something else [a script file, for instance] is being used and only displays colours when a terminal is being used, which is why “auto” should be used in aliases.

    Note that if you alias ‘ls’ with options, you don’t have to include those options when you make aliases for other ‘ls’ variants. For example, if you do ‘alias ls=”ls –color=auto”‘ to get ‘ls’ colourized, then all you have to do is ‘alias lsa=”ls -a”‘ to get ‘lsa’ (which also displays hidden files in a directory) colourized as well.

    “Directories created by the user have their names displayed within single quotes.”

    Sorry, no. Unless you’ve come up with ground-breaking advances in artificial intelligence, the OS has no way of knowing if a file was created by the user sitting at the keyboard or by some other means.

    No, the reason the directory name in your screen cap is within single quotes is because there is a space in the name…the same would happen to a file with a space in the name. See
    https://unix.stackexchange.com/questions/258679/why-is-ls-suddenly-wrapping-items-with-spaces-in-single-quotes
    for more about this, and for a very small representation of the multitudes that are PO’d by the change.

    PS. In 25-ish years of playing with various *nix systems and distros, I’ve never seen a directory/file name with a space in it enclosed in quotes, since I rarely create names with spaces in them…it’s too much of a PITA to have to enclose the names in quotes in order to access them. However, your explanation didn’t sound right, as per the first paragraph of my response to your statement, so I did a DDG search with “user-created directories in single quotes with ls” as the query. The URL I posted above, which contains the correct explanation, was the first hit. But three screens of hits later, I’d found no suggestion at all of your explanation. Things that make you go “Hummmmm”.

    “The colors used to display the names of files, directories and scripts depend on the visual theme used by the terminal.”

    While the colours used *might* be influenced by the theme (if the terminal program being used actually allows the use of themes; the stock ones that come with an OS usually don’t), themes usually just control the colour of the background and the foreground colour of text displayed (output from commands, what’s being typed at the prompt, etc.). Colourization of the names of files, directories, etc. in ‘ls’ is usually determined by the contents of the ‘LS_COLORS’ environment variable.

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

    Firstly, they’re not ‘flags’, they’re options. Secondly, you have the explanations of the options reversed. I suppose in this case it’s pretty obvious, but other times it might not be and you’d end up confusing people.

    “where ls is equivalent to ls – – color=auto.”

    Sorry, no. There shouldn’t be a space between the dashes, and there shouldn’t be a space between the second dash and ‘color’. The actual equivalent is “ls –color=auto”, as seen in the screen cap.

  2. Wow Rick, I know you spent a lot of time on that long reply, but quite literally, ‘ls’ has been colorized on every debian/ubuntu system I’ve used over the past 7 years. Absolutely nothing was changed, it’s colorized straight from the first boot after install.

    1. That’s because Debian/Ubuntu set things up the way I said in my post. Don’t confuse a distro’s OOTB functionality with a program’s default settings/configuration…they’re not necessarily the same.

      Next time you’re in ‘terminal’, do a ‘man ls’ and scroll down to the end of the ‘Description’ section…the first sentence of the last paragraph is “Using color to distinguish file types is disabled both by default and with –color=never.”

      That said, I use Debian from time-to-time myself (my main computer is macOS on a Mac mini) and am glad that Debian sets up ‘ls’ to be colourized OOTB…beats having to set it up manually :-)

Leave a Comment

Yeah! You've decided to leave a comment. That's fantastic! Check out our comment policy here. Let's have a personal and meaningful conversation.