The ls command in Linux is one of the most essential commands that every Linux user should know. If you’re a beginner to using the command line, ls is probably the first command you should try to learn. ls is short for list, and is used to list the files in your present working directory or some other directory if you specify one.
What makes ls so essential is that it allows you to see what files are in a directory. You’ll be using it constantly to list directory contents. ls is not a complex command, but it does include a lot of different options that can be used to list the files with additional information included. Chances are that you’ll find a few of these options very useful, even though ls by itself (without extra options) is always enough to list contents.
Newcomers to Linux may intuitively think that browsing files in the GUI would be infinitely easier than fiddling with the command line. But this couldn’t be further from the truth. Mastering the ls command will allow you to list directory contents and find files a lot more efficiently than any GUI tools. It can also be utilized in Bash scripting to help other tools manipulate files.
In this guide, you’ll learn how to use the ls command in Linux through examples. Follow along below to see learn about the various options that you can use with this command.
In this tutorial you will learn:
- How to use the ls command on Linux
|Requirements, Conventions or Software Version Used
|Any Linux distro
|Privileged access to your Linux system as root or via the
|# – requires given linux commands to be executed with root privileges either directly as a root user or by use of
$ – requires given linux commands to be executed as a regular non-privileged user
Frequently Used Options
The ls command list files and subdirectories that are contained in a directory. The options you can use with ls are mostly just to list additional information, or to format the output differently.
ls command in Linux Basic Examples
- Running the ls command by itself, without any additional options, will list the contents in your present working directory, which is just a technical way of saying the directory that your terminal is currently in.
As you can see in the screenshot above, the output will list a few files per line before wrapping to the next line. This is simply determined by the size of your terminal and the lengths of the file names, but there are options to control this behavior that we’ll cover below.
- Note that file names with a space in them will be wrapped inside single quotes. This makes it easier to determine where one file name ends and another begins, and also makes the output more friendly for copying and pasting.
$ ls 'file name.txt' filename.txt
- Rather than listing the contents of our present working directory, we can list the contents of any directory by specifying the path to it in our ls command. For example, the following command will list the contents of the
$ ls /home/linuxconfig
- One of the most common options to use with ls command in Linux, if not the most common option, is
-l. This option lists the directory contents in a longer format.
$ ls -l total 1918440 lrwxrwxrwx 1 root root 7 Mar 7 01:14 bin -> usr/bin drwxr-xr-x 4 root root 4096 Aug 13 15:36 boot drwxrwxr-x 2 root root 4096 Mar 7 01:20 cdrom drwxr-xr-x 20 root root 4140 Aug 13 15:58 dev drwxr-xr-x 129 root root 12288 Aug 12 13:25 etc drwxr-xr-x 4 root root 4096 Aug 7 23:07 home
This output shows us the file permissions for all the files in the directory, how many symlinks point to the file, the owner and group of each file, last modified time, and more. The arrow also indicates that the
binfile is just a symbolic link to
usr/bin. This could only be determined with the normal ls output by looking at the colors.
-aoption will also list hidden files (files that have a name that begins with
.). Unless you’re in the root directory, it’ll also list
.(present working directory) and
..(one directory up) as files. But that can be suppressed by using the
-Aoption instead. Note that we have also included the previously covered
-loption in this example.
$ ls -la total 8 drwxrwxr-x 2 linuxconfig linuxconfig 4096 Aug 14 00:18 . drwxr-xr-x 17 linuxconfig linuxconfig 4096 Aug 13 15:42 .. -rw-rw-r-- 1 linuxconfig linuxconfig 0 Aug 14 00:18 '.hidden file.txt' OR $ ls -lA total 0 -rw-rw-r-- 1 linuxconfig linuxconfig 0 Aug 14 00:18 '.hidden file.txt'
- Another common option to use with ls is
--human-readable. This will list the file sizes in human readable format, instead of bytes.
$ ls -lh total 1.3G -rw-rw-r-- 1 linuxconfig linuxconfig 437M Aug 14 00:21 file1.txt -rw-rw-r-- 1 linuxconfig linuxconfig 164M Aug 14 00:21 file2.txt -rw-rw-r-- 1 linuxconfig linuxconfig 713M Aug 14 00:21 file3.txt
You can always use the man command to read more about the ls command and its official documentation. Click the previous link to see how to open the manual pages for any command on a Linux system.
The ls command is pretty simple, but it comes packed with a lot of options. Many of these options fly under the radar, and even some seasoned system administrators may not know of them. However, they can definitely come in handy in various situations. In this section of the guide, we’ll show you a few of the lesser known options of the ls command that we think are useful.
ls command in Linux Advanced Examples
- Use the
-toption to sort files by modification time. This will bring the most recently edited files to the top of the output, making them easier to find.
$ ls -lt
- Use the
-Roption to recursively list a directory’s contents. This means that the contents of every subdirectory will also be listed. Be careful because a deep directory structure means you will receive an unwieldy amount of output from ls.
$ ls -R /home/linuxconfig
-1option will list files one per line, but only list the file names without any additional information. This is extremely useful when you need to grab a list of files without any of the extra formatting.
$ ls -1 /home/linuxconfig Desktop Documents Downloads Music Pictures Public Templates Videos
- Use the
-roption to reverse the order of the files that are listed.
$ ls -r
In this guide, we learned all about the ls command on Linux. It’s an essential command packed with tons of options, but in the end they all serve the same core purpose, which is to list the files in a directory. After mastering ls, you’ll quickly find that it can become even more convenient than using a GUI file browser. There are many more command options than we covered here, all serving their own niche purpose, but we recommend searching the manual pages for those.