The Linux "ls" command is your best friend when needing to list the contents of a directory. It's also one of the most basic commands. In this entry to the Linux Command Line series, we'll examine one of the most used commands in the Linux toolkit.
The ls command goes all the way back to the original AT&T UNIX operating system. The name, ls, was originally an acronym for list segments. In the early days of UNIX the word segments was used in the same way we use files today (hence ls and not lf.) Now there are two versions of the ls command available: The FSF (Free Software Foundation) release that comes with the coreutils package, and a BSD variant. The version of ls you will find on Linux is the FSF version. Should you be using a BSD-based distribution, you will be using the BSD variant of ls.
The ls command is simple to use. From the command line you can enter the command ls and hit enter to list the contents of the current working directory. In other words, if you are in the /home/jack directory and you issue the ls command, you will see the contents of the /home/jack directory. Of course you are not limited to only the current working directory. Say, for example, you are in the /home/jack/ directory but you want to see the contents of the /opt directory. To do this you would enter ls /opt and hit enter to display the contents of the /opt directory.
Without any arguments, the ls command only lists names of the visible files in a directory. Say, however, you want to see all files including hidden files (files that begin with a "."). To include hidden files in the listing you add the -a flag to the ls command. Now the command looks like: ls -a.
Say, however, you want to see more details of your directory files. You can add the l flag to change the basic listing to a long listing. The long listing includes: file types, permissions, hard links, owner, group, file size, and file name.
More Advanced Use
With Linux you can pipe one command through another command. This might be easier explained with the use of ls. When you enter the ls command the contents of the directory will fly by until they reach the end. If there are quite a few files within a directory you will not see every item. To be able to page through the contents you can pipe the ls command to the less command which will list the contents one page at a time. The piping of ls to the less command will look like this: ls | less. The "|" indicates the piping. Now you will see one page of contents and hit the space bar to see the next page.
The ls command is one of those tools that will become invaluable in your use of the Linux command line. Although there is much more to be had with its usage, this article will give you all you need to be able to use ls.