You are probably familiar with the ls
command, but do you know how to use all the different options? In this interactive tutorial, you will learn the different ways to use the following command:
$ ls [options] [file|dir]
Interactive Guide
There is an interactive shell prompt at the bottom of this page where you can run commands. In this tutorial, there are also code blocks that you can execute by hitting the Run button. Let's try it out!
First, we'll need to change directory into the root of our server. We can do that by executing the code below:
cd /
Pretty cool, right? Ok, let's start learning about the different ways to list out files, including all the options.
The ls
command
Using the ls
command, we can easily list out the contents of our current directory. Try out the command below:
ls
This will list out the contents in our current directory. This command is the simplest way to list files, but there are also many options available. Here are a few of the most common options:
-
-a
list all, include hidden files -
--color
colored list [=always/never/auto] -
-d
list directories -
-F
display additional formatting -
-i
list inode index of file -
-l
list w/ long format - show perms -
-la
list long format including hidden files -
-lh
list long format w/ readable size -
-r
list in reverse order -
-R
list recursively directory tree -
-s
list allocated file size -
-S
sort by file size -
-t
sort by time & date -
-X
sort by extension name
Let's see an example of all these options.
The ls -a
command
Using the -a
flag will list out all the files, including hidden files that begin with a .
, try it out by executing the code below:
ls -a
The ls --color
command
By default, you'll see that your commands will output a colored list of files; however, if you need output without color, you can run the following command:
ls --color=never
But, that's boring, right? If we want colors, we could run:
ls --color=always
By default, the color option is set to auto
, which will output in a colored format.
The ls -d
command
The ls -d
command allows you to list out the current directories inside your current folder; however, you'll also need to add */
to the end of that command like so:
ls -d */
The ls -F
command
The ls -F
command will format your output by placing a /
at the end of a directory and an @
at the end of a symbolic link:
ls -F
The ls -i
command
The ls -i
command will output the current inode (index node) of each file. You can learn more about inodes here.
ls -i
The ls -l
command
The ls -l
command will list the files in long format, and output the permissions of each file and folder:
ls -l
The ls -la
command
The ls -la
command will list all files (including hidden files) in a long format with the permissions.
ls -la
The ls -lh
command
The ls -lh
command will list out the files in long format with file permissions, and a human readable file size:
ls -lh
The ls -r
command
The ls -r
command will list your output in reverse order. First, let's try out the ls command again:
ls
Now, we'll try it in the reverse order:
ls -r
The ls -R
command
The ls -R
command will list out files recursively. We probably do not want to do that in the current directory because that will recursively list every file on the system. Instead, let's run this command and add the home
directory as the third argument.
ls -R home
The ls -s
command
The ls -s
command will display the allocated filesize for that specific file. This is the size that Unix has allocated space for that particular file.
ls -s
The ls -S
command
The ls -S
command will list out files and order them by filesize.
ls -S
Now, we could even combine this with the -l
and the -h
command to get a more detailed list sorted by filesize and displayed with a human-readable format, like so:
ls -lhS
The ls -t
command
The ls -t
command will list out files and sort them by time. This command will list out the files by the newest first.
ls -t
The ls -X
command
The ls -X
command will list out the files and sort them by their extension.
ls -X
Combining ls
options
As you've seen, you can combine a lot of these commands. As an example, we could list out our files in long format -l
and order by the time the file/folder was created -t
:
ls -lt
Great, but what if we wanted to sort by oldest first? Easy enough, we can also pass it the reverse -r
option:
ls -ltr
You can combine all these options to come up with the command that fits your needs.
Listing out a directory
As you've seen, you can also pass a folder location as a third argument, and it will execute the command from that folder. For instance, we can list out the contents of the usr
directory, with the following:
ls -a usr
Conclusion
That is the basics of listing out files in a Unix Shell. You may also want to check out the ls man page for more options.
It's unlikely that you will remember all the ways to list files in Unix, but try and incorporate a few into your mix, and eventually, you'll be listing out files like a command line hero.
Comments (0)