ls stands for list
List out the contents of a directory. If you don’t specify anything else, ls will list the contents of the current directory. That’s the same thing as
Remember, the period (
.) refers to the current working directory. So this is like saying, “List the files in my current directory.”
If you tell
ls to list a different directory, like a subdirectory:
This will list the contents of a directory inside the current one. If there is no such directory, it will tell you like so:
ls: abc/: No such file or directory
Remember, the other rules of file paths apply here too, so
ls ../ (parent directory),
ls /usr (the /usr folder at the root of the hard drive), and
ls ~/ (the current user’s home directory) all work too.
ls takes a parameter that is a UNIX path matcher. You already know what a UNIX path matcher is because you’ve used it, probably. You’ve learned the rules of home directory (
~), current directory (
.), parent directory (
..), and root of hard drive (
/), but what makes a path matcher different from specifying a full file name is that when you use a path matcher, you’re saying “any file that matches this specified part.” For example, if you specify
ls a_directory, where you have a directory called
a_directory. The list command will list all of the files that are in that directory because all of those relative paths match what you specified.
Using a Wildcard in A Unix Path Matcher
But you can make it more interesting but using a wildcard.
These path matcher rules apply to all UNIX commands, not just list. Any time you need to pass a reference to a file or set of files, you can probably pass a path matcher. (If you can’t, the documentation may tell you you must use a “full path” or “specific path”.)
Using a Flag with A Unix Command
Passing a flag means we type the command followed by some special characters that tell the command to work in a specific, non-standard way. Most flags have a long form, which is denoted with two dashes (–) and a longer word and a short form, which is denoted with one dash and a single character.
Sometimes, you’ll want to combine flags. That means you run more than one. When you use the short form (a single dash), you can combine multiple flags after the dash, without using a dash for each letter
Let’s take a look at our first flag.
The -A Flag (Show All Files, including Hidden Files)
-A flag shows all files, including hidden files. (The ones that normally begin with a period.)
The -L Flag (Long Format)
-L flag shows the long format. When viewed in a long format, each file entry gets its own line. You also see more information: The owner, group, and file permissions. We’ll return to the concept of the owner, group, and permissions later in this course.
For now, just remember that the
-L format shows you all of this information, and this command with the
-L flag is a great way to examine your file permissions.
What if you want to look at both long format and invisible files? You combine the flags by simply combining each shortcode. In this format, you write only one single slash, followed by the
l and the
The -F Flag (Show Path Objects with Symbols)
The manual page for the -F flag says: “Display a slash (‘/’) immediately after each pathname that is a directory, an asterisk (‘*’) after each that is executable, an at sign (‘@’) after each symbolic link, an equals sign (‘=’) after each socket, a percent sign (‘%’) after each whiteout, and a vertical bar (‘|’) after each that is a FIFO.”
Notice how the directories don’t have trailing slashes (
/) when you use
ls with no flags. Directories have trailing slashes when you run ls with the -F flag.
The -G Flag (Colorized Output)
Or combine it with the -F flag, as shown here: