Objectives & prerequisites
- Show the contents of a directory.
- Use the appropriate option to show the output as a detailed list.
- Use the appropriate option to include hidden files.
None.
Introduction
The ls command shows a list of the files and folders inside the current directory.
Example
sarah@laptop ~/documents$ ls
file1.pdf file2.pdf file3.pdf drafts research→ The output shows 3 pdf files and two directories, though from this listing you can't tell that drafts and research are directories.
Activity 1
Open a terminal and use ls to see the contents of your home directory.
Basic options
-lshow as long, detailed list-Alshow as long, detailed list, including hidden files
Listing all files as a detailed list, including hidden files
sarah@laptop ~/documents$ ls -Al
total 6
-rw-rw-r-- 1 sarah sarah 0 Oct 18 16:08 file1.pdf
-rw-rw-r-- 1 sarah sarah 0 Oct 18 14:04 file2.pdf
-rw-rw-r-- 1 sarah sarah 0 Oct 18 14:07 file3.pdf
-rw-rw-r-- 1 sarah sarah 0 Oct 18 14:07 .passwords.txt
drwxrwxr-x 3 sarah sarah 4096 Dec 31 2022 drafts
drwxrwxr-x 19 sarah sarah 4096 Dec 3 2022 researchFor some items, the first column starts with the letter d. This indicates that the item is a directory. In this example, drafts and research are directories.
Hidden files
Notice the file .passwords.txt:
- it was not listed in the output of the first
lsexample - But it was listed in the output of
ls -Al. - Its name starts with a dot (
.).
In Linux, files with a name that starts with a dot are considered hidden files.
Hidden files behave exactly the way non-hidden files behave, with the only difference that hidden files are not shown by default when listing the contents of a directory. That's why they're called hidden files.
Activity 2
Open a terminal and use ls with the appropriate option(s) to see a detailed list of the contents of your home directory.
Activity 3
Open a terminal and use ls with the appropriate option(s) to see a detailed list of the contents of your home directory, including hidden files.
Compare this output with the output from the previous activity.
Do you now see files that were not shown in the previous output?
Listing the contents of a different directory
ls can do more than list the contents of the current directory. You can also use it to list the contents of a different directory.
Example 1
sarah@laptop ~/documents$ ls research
→ lists the contents of the research directory, which is a subdirectory of the documents directory.
Example 2
sarah@laptop ~/documents$ ls /usr/bin
→ lists the contents of the /user/bin directory
Activity 4
Inspect the output of the previous command (the contents of your home directory) and identify the directories.
From your home directory (~), run the ls command in such a way that it returns the contents of one of the subdirectories.
Summary
- Use
lsto show the contents of a directory. - Use the
-loption to show the output as a detailed list. Entries with a d in the first column are directories. - Use the
-Aoption to show hidden files as well. - Combine the options into
-Alfor a detailed list that includes hidden files.