The ls command invokes a program that is part of the GNU Core Utitlities and is a part of all Unix-like operating systems. The ls command lists information about files and directories with many optional features accessible via options flags. Using ls in linux operating systems, such as Ubuntu, will invoke the ls program.
By default, the
ls program lists all files and subdirectories within the current working directory, non-recursively, omitting “dotfiles” whose names begin with a
., and displays this information sorted in alphabetical order. There are many optional features of this program and this article takes a look at some of the most popular among them.
ls Basic Usage
ls command sometimes referred to as the “list” command, is intended to display information about files to the terminal window1. To explore the features of the
ls command we will use a sample project directory, created using the
create_sample_project.py script (via Github), and demonstrate various command usage on the following project structure:
. └── sample_project/ ├── sample_file_0.txt ├── .hidden_file_0.txt ├── sample_dir_0/ │ ├── sample_file_0_0.txt │ ├── sample_file_0_1.txt │ └── sample_file_0_2.txt └── sample_dir_1/ ├── sample_file_1_0.txt ├── sample_file_1_1.txt └── sample_file_1_2.txt
.hidden_file_0.txt files were added manually.
sample_project directory, executing the
ls command without any additional arguments produces the following output:
/sample_project$ ls // output sample_dir_0 sample_dir_1 sample_file_0.txt
Depending on which system this command is executed, various terminal styling may be used to indicate
sample_dir_1 are directories.
ls -l Command
-l (lowercase L) flag changes the display to “long” format and displays the total number of files, the files, and the following file metadata:
- File permissions
- Number of hard links
- Last-modified Date
This information, sorted alphabetically by default, is displayed in a rows and columns format as such:
sample_project$ ls -l // output total 0 drwxrwxrwx 1 owner group 4096 Jun 23 08:53 sample_dir_0 drwxrwxrwx 1 owner group 4096 Jun 23 08:53 sample_dir_1 -rwxrwxrwx 1 owner group 256 Jun 23 08:53 sample_file_0.txt
d character appearing in the first position of the file permissions column. That indicates the first two files are directories.
Additional parameters can be passed to the
-ls command to sort the output in varying ways. Check out the list of output sorting options on the GNU.org website for more information there. For now, we’ll stick with the defaults.
ls -a command in linux will list all files in the current directory including hidden files. In this case, our
sample_project directory has a single hidden file. The output is as follows:
$ ls -a // output . .. .hidden_file.txt sample_dir_0 sample_dir_1 sample_file_0.txt
Here we see the output much like before but this time the file
.hidden_file.txt is present. Also, note that
.. are present as well (a.k.a. Dot Files.) These are references to the current working directory and the parent directory.
ls <path> Command: List Files of Specified Path
Passing an argument into the
ls command of another directory will list the contents of that directory, rather than the contents of the current working directory. For example, if we had another
sample_project_2 directory we could view the contents of that directory — while still working from the
sample_project directory — via the following command:
This assumes that our
sample_project_2 directory is a sibling directory to our current working directory (
There are three notable “universal”
<path> arguments that can be passed to the
.– list contents of the current working directory, the equivalent of passing no arguments.
..– list contents of the parent directory.
/– list contents of the root directory.
ls -F Command: Add
/ to All Directory Names
Sometimes working in the terminal can cause some visual confusion — especially when viewing lots of information. Passing the
-F option into the
ls program will add a trailing slash
/ character to any directory and a
* character to files for easier visual identification. Sample output:
$ ls -F // output sample_dir_0/ sample_dir_1/ sample_file_0.txt*
ls -R Command: List Files Recursively
sample_project directory contains two subdirectories each of which contain files. To this point, no
ls command has listed any files in those subdirectories. Passing in the
-R flag (must be uppercase) the
ls program will recursive into any subdirectories subsequently listing any files found therein. Example output:
$ ls -R // output .: sample_dir_0 sample_dir_1 sample_file_0.txt ./sample_dir_0: sample_file_0_0.txt sample_file_0_1.txt sample_file_0_2.txt ./sample_dir_1: sample_file_1_0.txt sample_file_1_1.txt sample_file_1_2.txt
Note the name of the directory is printed to the terminal window before the listing of files contained therein. This option can be super helpful in getting a better idea of a directory’s structure.
The options discussed so far are among the most commonly used in my experience. However, I often find myself preferring the output from multiple options simultaneously. As with other commands, the
ls program can accept multiple arguments for options where the latter options will override earlier ones where applicable. Consider the following
ls -Ral which will show hidden files, file metadata, and recurse:
$ ls -Ral // output .: total 0 drwxrwxrwx 1 owner group 4096 Jun 23 08:58 . drwxrwxrwx 1 owner group 4096 Jun 23 08:53 .. -rwxrwxrwx 1 owner group 9 Jun 23 08:58 .hidden_file.txt drwxrwxrwx 1 owner group 4096 Jun 23 08:53 sample_dir_0 drwxrwxrwx 1 owner group 4096 Jun 23 08:53 sample_dir_1 -rwxrwxrwx 1 owner group 256 Jun 23 08:53 sample_file_0.txt ./sample_dir_0: total 0 drwxrwxrwx 1 owner group 4096 Jun 23 08:53 . drwxrwxrwx 1 owner group 4096 Jun 23 08:58 .. -rwxrwxrwx 1 owner group 256 Jun 23 08:53 sample_file_0_0.txt -rwxrwxrwx 1 owner group 256 Jun 23 08:53 sample_file_0_1.txt -rwxrwxrwx 1 owner group 256 Jun 23 08:53 sample_file_0_2.txt ./sample_dir_1: total 0 drwxrwxrwx 1 owner group 4096 Jun 23 08:53 . drwxrwxrwx 1 owner group 4096 Jun 23 08:58 .. -rwxrwxrwx 1 owner group 256 Jun 23 08:53 sample_file_1_0.txt -rwxrwxrwx 1 owner group 256 Jun 23 08:53 sample_file_1_1.txt -rwxrwxrwx 1 owner group 256 Jun 23 08:53 sample_file_1_2.txt
Note: The options can be passed in any order.
aRl are will all produce the same output.
For debugging issues with
ls one can use the
ls --version command or
ls --v command (note the double slashes) to get the current version. Using the
ls --help (again not the double slashes) will print a list of useful information including all available commands and their respective syntaxes.
Another useful approach is the alias the
ls command to ensure certain options are enabled by default. For example, consider the case where one realizes that hidden files (dotfiles) and file information are preferred in almost all cases. An alias to the
ls -la command can be created as such:
alias ls="ls -la"
Now, anytime the
ls command is issued it is the equivalent of issuing the
ls -la command. See the following:
$ ls // output total 0 drwxrwxrwx 1 owner group 4096 Jun 23 08:58 . drwxrwxrwx 1 owner group 4096 Jun 23 08:53 .. -rwxrwxrwx 1 owner group 9 Jun 23 08:58 .hidden_file.txt drwxrwxrwx 1 owner group 4096 Jun 23 08:53 sample_dir_0 drwxrwxrwx 1 owner group 4096 Jun 23 08:53 sample_dir_1 -rwxrwxrwx 1 owner group 256 Jun 23 08:53 sample_file_0.txt
At any point, one can view the current aliases of a system by typing
alias. In this case, we would note that
alias ls='ls -la' is included as the last entry. Should this alias become annoying, it can be removed via the
unalias ls command, at which point the
ls command will revert to default behavior.
Basic working knowledge of Linux command-line tools affords developers a serious advantage in many cases. Certainly, in the case where only terminal access to a system is available, such as with many remote deployments, this is an advantage. However, the ability to quickly and accurately display information via tools like the
ls program can make handling simple tasks much less burdensome.
ls program is particularly useful when using the tar program to compress files in Linux as well as when using the cp command to copy files. The
ls command can be used in these cases to confirm which files are present before creating an archive, ensuring the intended file to copy is present, confirm that a copied file was successful, and so much more. As with the output from any other command, the information given via the
ls command can be used with piping and redirection!
- GNU.org. “ls: List Directory Contents.” Accessed: 6/23/2022. Available: https://web.archive.org/web/20220412102427/https://www.gnu.org/software/coreutils/manual/html_node/ls-invocation.html