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]
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:
Pretty cool, right? Ok, let's start learning about the different ways to list out files, including all the options.
ls command, we can easily list out the contents of our current directory. Try out the command below:
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:
-alist all, include hidden files
--colorcolored list [=always/never/auto]
-Fdisplay additional formatting
-ilist inode index of file
-llist w/ long format - show perms
-lalist long format including hidden files
-lhlist long format w/ readable size
-rlist in reverse order
-Rlist recursively directory tree
-slist allocated file size
-Ssort by file size
-tsort by time & date
-Xsort by extension name
Let's see an example of all these options.
ls -a command
-a flag will list out all the files, including hidden files that begin with a
., try it out by executing the code below:
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:Checkout our latest product - the ultimate tailwindcss page creator 🚀
But, that's boring, right? If we want colors, we could run:
By default, the color option is set to
auto, which will output in a colored format.
ls -d command
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 */
ls -F command
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 -i command
ls -i command will output the current inode (index node) of each file. You can learn more about inodes here.
ls -l command
ls -l command will list the files in long format, and output the permissions of each file and folder:
ls -la command
ls -la command will list all files (including hidden files) in a long format with the permissions.
ls -lh command
ls -lh command will list out the files in long format with file permissions, and a human readable file size:
ls -r command
ls -r command will list your output in reverse order. First, let's try out the ls command again:
Now, we'll try it in the reverse order:
ls -R command
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
ls -s command
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 command
ls -S command will list out files and order them by filesize.
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 -t command
ls -t command will list out files and sort them by time. This command will list out the files by the newest first.
ls -X command
ls -X command will list out the files and sort them by their extension.
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
Great, but what if we wanted to sort by oldest first? Easy enough, we can also pass it the reverse
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
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.