Linux Command: ls

Published on September 2, 2021 at 2:24 pm by LEW

Tux Linux LogoIntroduction

The ls command has been around since 1987, and was part of early AT&T Unix. Being part of UNIX, it was naturally adopted by early LINUX distributions. The ls command is short for list. Its purpose is to list files and directories/folders.

Syntax and Function

The syntax of the ls command is:

ls [options] [files/directory]

The ls command sends the content of a directory to stdout (short for standard out). In most cases this will be the display. The files and directories are printed to the terminal in alphabetical order, using as many columns as the display will support.

There are some differences in how ls behaves, depending on where stdout is pointing. For example, when the output is sent to a physical printer, the files and directories are printed alphabetically in list fashion, not column fashion.

Examples

To display the content of the directory you are in (current working directory), you only have to issue the ls command by itself.

ls
Desktop   Downloads Pictures Templates

Documents Music     Public   Videos

To view the content of another directory, add the directory path to ls.

ls /home/guest

If you are interested in a bit more information about the directory, you can add the -l option. This will list file permissions, user, group, size, date, and name. The information will be listed one entry per line. To list the content of the current working directory use;

ls -l  

total 32

    drwxr-xr-x 2 mc mc 4096 Jul 27 21:22 Desktop

    drwxr-xr-x 2 mc mc 4096 Jul 27 21:22 Documents

    drwxr-xr-x 2 mc mc 4096 Jul 30 06:26 Downloads

    drwxr-xr-x 2 mc mc 4096 Jul 27 21:22 Music

    drwxr-xr-x 2 mc mc 4096 Jul 27 21:22 Pictures

    drwxr-xr-x 2 mc mc 4096 Jul 27 21:22 Public

    drwxr-xr-x 2 mc mc 4096 Jul 27 21:22 Templates

    drwxr-xr-x 2 mc mc 4096 Jul 27 21:22 Videos

To list the content of a different directory, append the file path to the command;

ls -l home/guest

Linux has something known as a dot file. Any file name beginning with a period is a hidden dot file. To view these files use the -a option;

ls -a
.             Documents Public
..            Downloads .ssh
.aptitude     .gnupg    Templates
.bash_history .local    .vboxclient-clipboard.pid
.bash_        logout    .vboxclient-display-svga-x11.pid
.bashrc       Music     .vboxclient-draganddrop.pid
.cache        Pictures  .vboxclient-seamless.pid
.config       .pki      Videos
Desktop       .profile

You can chain together options. Using the two previously discussed options we can display all files and their information. There are two forms for chaining option together. Giving each of them their own “”, or just using one “” and listing all the option after that.

ls -a -l

or

ls -al

The ls command normally outputs file size in bytes. You can add the -h option for human-readable. This will output file size in kilobytes, megabytes, and gigabytes.

ls -alh

When specifying a directory to search, you can use wild cards. The ? represents a single character, and the * represents multiple single characters. To list only files that start with a capital D in the working directory, the entry would look like this.

ls D*

Conclusion

The ls command is probably one of the most common commands you will use in a terminal. This post is intended to give you basic instructions for using the command. We have only looked at a few of the most common options. Nor have we touched on more advanced usage of the ls command. For a complete list of options check the Man Page for the command to see specifics for your distribution of Linux.

Add New Comment

Your email address will not be published. Required fields are marked *