This chapter covers the essential command for seeing what files and directories exist within your current location or any other path: ls.
1. The ls Command
The ls (list) command lists the files and subdirectories contained within a specified directory. If no path is provided, it lists the contents of the current working directory.
Basic Execution
ls
2. Essential ls Options (Flags)
The power of ls comes from its options, which allow you to change the format and amount of information displayed.
| Option | Command | Purpose |
| Long Format | -l | Displays detailed information about each item, including permissions, owner, group, size, and modification date. |
| All Files | -a | Displays all files, including hidden files (those starting with a dot, e.g., .bashrc). |
| Human Readable | -h | Used with -l. Displays file sizes in human-readable units (e.g., 1K, 23M, 1G) instead of bytes. |
| Reverse Order | -r | Reverses the sort order (e.g., sorts Z-A instead of A-Z). |
| Sort by Size | -S | Sorts the output by file size, from largest to smallest. |
| Recursive List | -R | Lists the contents of all subdirectories recursively. |
3. Combining Options
Options can often be combined after a single hyphen for efficiency.
Execution Examples
To list all files (-a) in long format (-l) with sizes displayed clearly (-h):
ls -alh
To list contents, sorted by size (-S), in reverse order (smallest first, -r):
ls -Sr
4. Interpreting the Long Format (ls -l)
When you use the long format option (-l), the output is divided into several columns, providing critical metadata about the files.
The first column is the most complex, defining the type and permissions of the file:
| Character | Meaning |
| 1st Char | File Type (- for file, d for directory, l for symbolic link). |
| 2nd-4th Char | Owner Permissions (rwx). |
| 5th-7th Char | Group Permissions (rwx). |
| 8th-10th Char | Others Permissions (rwx). |
Example Output:
-rw-r--r-- 1 alex developers 15K Dec 1 10:30 config.yaml
-rw-r--r--: A regular file (-). The owner can read and write (rw-), the group can only read (r--), and others can only read (r--).1: Number of hard links.alex: File owner.developers: File group.15K: File size.Dec 1 10:30: Last modification timestamp.
