Table of Contents
Seeing What Is In a Directory
When you work in the terminal, you cannot “see” your files like in a graphical file manager. Instead, you ask the shell to list them for you. The basic tool for this is the ls command.
This chapter focuses only on listing files. Navigating between directories and other operations have their own chapters.
The Basic `ls` Command
The simplest way to see what is inside your current directory is to type:
ls
This prints the names of files and directories in the current working directory. By default, ls shows only visible entries. Hidden entries, which start with a dot such as .bashrc, are not shown unless you ask for them.
You can also tell ls which directory to show:
ls /etc
ls /var/log
In these examples, ls lists the contents of /etc or /var/log instead of your current directory.
Listing Hidden Files with `-a`
Hidden files and directories all start with a . character at the beginning of the name. They are common in Linux, especially for configuration.
To show absolutely everything, including these hidden entries, you use the -a option:
ls -a
You will see entries like . and .. in the output. . refers to the current directory and .. refers to the parent directory. These are special entries that exist in every directory.
Sometimes, you might want to show hidden files but avoid the . and .. entries. You can do this with:
ls -A
The option -A is similar to -a but omits . and ...
Important rule: ls alone does not show hidden files. Use ls -a or ls -A when you need to see files and directories whose names begin with ..
Long Listings with `-l`
The short output of ls only shows names. To see more details about each file or directory, use the long format with -l:
ls -l
A typical line in ls -l output might look like this:
-rw-r--r-- 1 alice alice 1024 Jan 7 12:34 notes.txtFrom left to right you see:
A block of letters and dashes that describes the file type and permissions.
A number that is the link count.
The owner name.
The group name.
The file size in bytes.
The last modification date and time.
The file name itself.
Permissions, owners, and groups are explained in later chapters. For now it is enough to recognize that ls -l gives you a detailed listing.
You can combine -l with a directory path:
ls -l /usr/bin
This shows detailed information for each item in /usr/bin.
File Sizes in Human Friendly Units with `-h`
The size that ls -l shows is in bytes. Large files are easier to read as kilobytes, megabytes, or gigabytes. The option -h tells ls to display sizes in a human friendly way.
You usually combine it with -l:
ls -lh
Now sizes appear as values like 4.0K, 2.3M, or 1.1G instead of large numbers of bytes.
You can also use it on a specific directory:
ls -lh /var/log
Useful combination: ls -lh shows a detailed listing with sizes in units like K, M, and G, which are much easier to read than raw bytes.
Listing Directories Recursively with `-R`
Sometimes you want to see not only what is in one directory but also everything in all its subdirectories. The -R option tells ls to work recursively.
For example:
ls -Rlists the current directory and then each subdirectory underneath it. The output can become long, so use this with care.
You can also use a path:
ls -R /etc
This prints /etc, then /etc subdirectories, and so on.
Combining Multiple Options
You can pass several options to ls at once. Options can be written separately or combined together. The following are equivalent:
ls -l -a
ls -la
ls -alThis shows a long listing, including hidden files.
Here are some useful combinations:
ls -lahThis prints a long listing, includes hidden files, and shows human readable sizes.
ls -lRThis prints a detailed listing recursively.
Order of the options usually does not matter. You can group them in any way that feels comfortable.
Sorting the Output
By default, ls sorts by name, alphabetically. You can change the sort order with options.
To sort by modification time, most recent first, use:
ls -ltTo add human readable sizes and hidden files as well:
ls -lathTo sort by size, largest first, use:
ls -lS
If you want to reverse the sort order, for example oldest first or smallest first, add -r:
ls -ltr # oldest files at the bottom
ls -lrS # smallest files at the bottomYou can combine these sorting flags with paths as usual:
ls -lhS /var/logDistinguishing File Types with Colors and Markers
Most Linux systems configure ls to use colors by default. Different colors can represent directories, regular files, symbolic links, and executable files. The exact colors depend on your distribution and terminal settings.
Even if colors are disabled, you can ask ls to append a character that marks the type of each entry. Use:
ls -FThis might show you names like:
Documents/ script.sh* notes.txt socket= link@
Common markers include / at the end of directory names, * after executable files, and @ after symbolic links.
You can combine this with other options:
ls -lFThis gives a detailed listing and also shows markers at the end of names.
Listing Only Directories or Only Matching Names
While ls itself does not have a switch like “only directories,” you can often reach what you want using patterns on the command line. Shell patterns, also called globs, are covered in another chapter, so this section will stay simple.
If you want to list files that match a pattern, you can write:
ls *.txt
This prints only entries whose names end with .txt in the current directory.
To list what is inside a specific directory, you give the directory path, optionally with a trailing slash:
ls /etc/
Linux treats ls /etc and ls /etc/ similarly when listing contents.
Listing One Name Per Line with `-1`
If you want to see a simple list with one entry per line, especially useful when you expect to copy and paste names, use:
ls -1This is useful when you combine output with other commands later. It can also help when lines are too long.
You can of course combine it:
ls -1a
ls -1lhInspecting a Single File or Path
ls is not only for directories. You can also ask it to show information about a specific file:
ls -l notes.txt
If the file exists, you get a single line of detailed information. If it does not exist, ls prints an error.
You can pass several paths:
ls -l notes.txt script.sh /etc/hostsEach path is listed in turn, and directories are shown with their contents. This can be useful when you want to compare a few items in one command.
When `ls` Fails
If you run ls on a directory that you are not allowed to access or that does not exist, it will print an error message. For example:
ls /rootOn a typical system, if you are a normal user, you may see a “Permission denied” message because only the administrator is allowed to read that directory.
If you mistype a path:
ls /etc/passwdxyou might see “No such file or directory.”
These messages come from ls and help you understand what went wrong. They do not change your files.
Summary
In daily terminal work, you will use ls very often to inspect the contents of directories and to understand what files are present. The basic command shows names, and options let you reveal hidden items, see more detail, view sizes in a readable format, sort in different ways, and inspect specific paths. As you gain experience, combinations like ls -lah and ls -ltr will become part of your regular habits.