How to use the Linux ls
command plus essential options
In this installment of “Conquer The Command Line” we talk about how to use the Linux ls
command.
pwd
or looking at the prompt gives us a way to determine the directory we are in. If we want a way to determine the contents of that directory, we need to use the ls
command. Let's learn about how to use it and some essential options.
Memory Aid: think “list”
To get a listing of the contents of the current directory, type ls
by itself. This displays a densely-packed listing of the current directory.
Unfortunately, if you don’t know which items are files, directories, or something else, this is unhelpful. It’s also unhelpful for some people to easily read this listing the way ls displays it. Here are some helpful options that make ls
show more information.
Note that some options are in upper case and some are in lower case.
Essential Options
- List files, one per line:
ls -1
- List all files including hidden files:
ls -a
- List all files and add a trailing
/
, which shows which items are directories:ls -F
- List files in long format, showing permissions, ownership, size, and
ls -la
- List files in Long format with size displayed using human readable units (kilobytes, megabytes, Gigabytes:
ls -lh
- List files in long format sorted by size, show largest files at the top:
ls -lS
- List files in long format sorted by modification date, oldest first:
ls -ltr
What The Long Format Displays
The long format for ls
displays a line that contains information about each item in the list. That line packs a lot of information into a short space. Here is what each item is, reading left to right:
- The first character of the line displays the type of the list item.
-
indicates a file.d
indicates a directory.l
indicates a link. - the next nine characters display the permissions. The three kinds of permissions are owner, the person who controls the permissions, group, the name of a group the file owner into which the owner has placed the file or directory, and the permission for all other users of the system.
- The next item is the number of symbolic links to the file or directory listed on the line, which are names that point to files. They kind of act like hyperlinks. For Windows people, think of these as shortcuts. Here’s mor information about symbolic links
Getting A Directory Listing for a directory Other Than The Current One
If you know the name of a directory, you can use the above options and get ls
to list it for you. All you have to do is type ls
, then the name of the directory. Below is an example of an ls
command that lists the root, or top level, directory.
ls /
What Now?
Hopefully this post has helped you take one more step in conquering the command line. I welcome any feedback.
This post was originally written at https://www.jimhomme.com