List Files in Terminal: Commands & Examples
In this article
Many people assume listing files in a terminal is complicated, but it’s actually quite straightforward. The primary command for listing files and directories is `ls` (short for "list"). This command provides a basic listing of the files and directories in your current location.
The `ls` Command Basics
The simplest use of `ls` just displays the names of files and directories in the current working directory. Each file or directory name is separated by a space or newline. To change your working directory, use the `cd` command (change directory). For example, `cd Documents` will change your directory to the "Documents" folder, if it exists in your current location. Then `ls` will show the contents of that folder.
Common `ls` Options
The `ls` command has numerous options to customize the output. These options are typically preceded by a hyphen (`-`). Here are some of the most frequently used:
`-l` (Long Listing)
The `-l` option provides a detailed, long listing format. It displays the file permissions, number of links, owner, group, file size, modification date and time, and filename. For example:
-rw-r--r-- 1 user group 1024 Jan 15 10:00 myfile.txt
The first character indicates the file type (`-` for regular file, `d` for directory, `l` for symbolic link). The following nine characters represent the file permissions (read, write, execute for owner, group, and others). The number following the permissions is the number of hard links to the file. The next two fields are the owner and group. The file size is displayed in bytes. The modification date and time indicate when the file was last modified. Finally, the filename is displayed.
`-a` (All Files)
By default, `ls` hides files and directories that begin with a dot (`.`), which are typically considered hidden files. The `-a` option displays all files, including these hidden ones. This is useful for seeing configuration files or other files that are not normally visible.
`-h` (Human-Readable Sizes)
When used with `-l`, the `-h` option displays file sizes in a more human-readable format (e.g., 1K, 234M, 2G) instead of bytes. This makes it easier to understand the size of your files.
`-t` (Sort by Modification Time)
The `-t` option sorts the files and directories by their modification time, with the most recently modified files listed first. This is useful for quickly finding the files you've been working on recently.
`-r` (Reverse Order)
The `-r` option reverses the order of the listing. When used with `-t`, it will show the oldest files first. When used without another sorting option, it reverses the alphabetical order.
`-R` (Recursive Listing)
The `-R` option lists the contents of all subdirectories recursively. This can generate a very long listing if you have a deeply nested directory structure. Use with caution!
Combining Options
You can combine multiple options to achieve the desired output. For example, `ls -lha` will display a long listing, including hidden files, with human-readable file sizes.
Filtering with Wildcards
You can use wildcards to filter the files listed by `ls`. The most common wildcards are:
`*` (Matches any characters)
The asterisk (`*`) matches any sequence of characters. For example, `ls *.txt` will list all files with the `.txt` extension in the current directory.
`?` (Matches a single character)
The question mark (`?`) matches any single character. For example, `ls file?.txt` will list files like `file1.txt`, `file2.txt`, but not `file12.txt`.
Examples
Here are some practical examples of using the `ls` command:
- List all files in the current directory: `ls`
- List all files, including hidden files: `ls -a`
- List files with detailed information and human-readable sizes: `ls -lha`
- List files sorted by modification time (newest first): `ls -lt`
- List files ending with ".pdf": `ls *.pdf`
- List files starting with "report": `ls report*`
History and Origin
The `ls` command dates back to the early days of Unix, appearing in Version 1 in 1971. Its purpose has remained consistent: to provide a way to list the contents of directories. Over time, the command has been extended with numerous options to enhance its functionality and provide more control over the output. The portability of Unix meant `ls` quickly became a standard tool across many operating systems.
Current Practice and Variations
Today, `ls` is a fundamental command in Unix-like operating systems (Linux, macOS, BSD) and is also available in environments like Windows Subsystem for Linux (WSL). While the core functionality remains the same, minor variations in options and output formatting may exist across different implementations. Refer to the `man ls` command in your terminal for the definitive list of options and their behavior on your specific system; the `man` pages are the system's documentation. The options described above are the most common and widely supported.
A Common Misconception
Many new users mistakenly believe that `ls -l` automatically sorts the files by name. While it displays detailed information, it doesn't sort the output alphabetically by default. You need to use the `-t` or `-S` (sort by size) options for sorting.
Signs to Look For
Here are some signs you're using `ls` correctly:
- You see a list of files and directories in the current working directory.
- When using `-l`, you see file permissions, owner, group, size, and modification date.
- The `-a` option shows hidden files (those starting with a dot).
- Wildcards correctly filter the files displayed.
Checking the Output
Always double-check the output of `ls` to ensure you are working in the correct directory and seeing the expected files. Use the `pwd` (print working directory) command to confirm your current location. If you are unsure about the correct syntax of an option, use the `man ls` command to view the manual page for `ls` and see all available options and their descriptions. Also, remember that case matters; `ls -A` is different than `ls -a` in some systems.