Basic Linux Commands: The ls Command

Basic Linux Commands: The ls Command
Page content

History

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.

Basic Use

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.

Intermediate Use

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.

Final Thoughts

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.

This post is part of the series: Linux Command Line

If you ever plan on doing any administration on a Linux machine, you would be well served to get to know the command line interface. In this Bright Hub series you will be introduced to various concepts surrounding one of the most powerful admin tools around.

  1. Linux Command Line: Introduction
  2. Linux Command Line: ls
  3. Linux Command Line: cd
  4. Linux Command Line: mkdir
  5. Linux Command Line: df
  6. Linux Command Line: ln
  7. Linux Command Line: top
  8. Linux Command Line: mount/umount
  9. Linux Command Line: Cron/Crontab
  10. Linux Command Line: chmod
  11. Linux Command Line: wget
  12. Linux Command Line: cat
  13. Linux Command Line: grep
  14. Linux Command Line: dd
  15. Linux Command Line: sudo
  16. Linux Command Line: startx
  17. Linux Command Line: adduser
  18. Linux Command Line: at
  19. Linux Command Line: aterm
  20. Linux Command Line: nano
  21. Linux Command Line: hostname