How to List Files in Linux: Simple Commands Explained
To list files in Linux, use the
ls command in the terminal. This command shows files and directories in the current folder by default, and you can add options like -l for detailed info or -a to include hidden files.Syntax
The basic syntax of the ls command is:
ls [options] [path]
Here:
lslists files and directories.[options]modify the output (like showing hidden files or detailed info).[path]specifies the folder to list; if omitted, it lists the current directory.
bash
ls [options] [path]
Example
This example shows how to list all files, including hidden ones, with detailed information in the current directory.
bash
ls -la
Output
total 48
drwxr-xr-x 6 user user 4096 Apr 27 10:00 .
drwxr-xr-x 3 user user 4096 Apr 26 09:00 ..
-rw-r--r-- 1 user user 220 Apr 27 09:59 .bash_logout
-rw-r--r-- 1 user user 3771 Apr 27 09:59 .bashrc
-rw-r--r-- 1 user user 807 Apr 27 09:59 .profile
-rw-r--r-- 1 user user 0 Apr 27 10:00 example.txt
Common Pitfalls
Beginners often forget that hidden files start with a dot (.) and are not shown by default. Also, using ls without options may not show detailed info like file size or permissions.
Another common mistake is not specifying the correct path, which can lead to listing files in the wrong directory.
bash
ls # Wrong: does not show hidden files or details ls -la # Right: shows all files with details
Quick Reference
| Option | Description |
|---|---|
| -l | List files with detailed info (permissions, size, date) |
| -a | Include hidden files (those starting with a dot) |
| -h | Show file sizes in human-readable format (e.g., KB, MB) |
| -R | List files recursively in all subdirectories |
| -t | Sort files by modification time, newest first |
Key Takeaways
Use
ls to list files in the current directory by default.Add
-a to include hidden files and -l for detailed info.Specify a path after
ls to list files in a different directory.Remember hidden files start with a dot and are not shown without
-a.Combine options like
ls -lah for detailed, human-readable file lists.