Listing Files in Linux
Linux provides powerful commands for listing files and directories, allowing you to view and manage your filesystem efficiently. This guide covers the most commonly used commands for listing files, their options, and practical examples.
ls
- Lists files and directories in the current directory.
- By default, output is sorted alphabetically.
Sample Output
Desktop Documents Downloads file.txt script.sh
Common ls
Options
Command | Description |
---|---|
ls -l | Long listing (detailed info) |
ls -a | Includes hidden files (those starting with . ) |
ls -lh | Human-readable sizes (K , M , G ) |
ls -lt | Sort by modification time |
ls -R | Recursively list subdirectories |
ls -lS | Sort by file size |
ls --color=auto | Enables colored output (default in most distros) |
Example
ls -lh
Sample Output:
-rw-r--r--. 1 user user 12K Jul 22 09:00 file.txt
-rwxr-xr-x. 1 user user 2.1K Jul 22 08:59 script.sh
drwxr-xr-x. 2 user user 4.0K Jul 21 17:23 Documents
Explanation of Fields in ls -l
-rwxr-xr-x. 1 user user 2.1K Jul 22 08:59 script.sh
│ │ │ │ │ │ └── File name
│ │ │ │ │ └── Last modified date/time
│ │ │ │ └── File size
│ │ │ └── Group owner
│ │ └── Owner
│ └── Number of hard links
└── File type and permissions
Here’s a detailed explanation of file types in Linux as shown by the first character in ls -l
output:
File Types in Linux (1st Character of ls -l
Output)
Symbol | File Type | Description | Example |
---|---|---|---|
- | Regular file | A standard file that contains text, data, code, etc. | -rw-r--r-- 1 user user 123 file.txt |
d | Directory | A folder that contains files or other directories | drwxr-xr-x 2 user user 4096 folder/ |
l | Symbolic link | A shortcut or reference to another file | lrwxrwxrwx 1 user user 11 link -> file.txt |
b | Block device | Hardware device that handles data in blocks (e.g., hard drives) | brw-rw---- 1 root disk 8, 1 /dev/sda1 |
c | Character device | Device that handles data as a stream of bytes (e.g., keyboard, serial ports) | crw-rw---- 1 root tty 4, 64 /dev/ttyS0 |
p | Named pipe (FIFO) | A method for inter-process communication | prw-r--r-- 1 user user 0 pipe |
s | Socket | File used for inter-process communication via a network or local socket | srwxr-xr-x 1 user user 0 socket |
How to View File Types
You can use:
ls -l
Or, to see file types clearly:
ls -lF
The -F
option appends special symbols to filenames:
/
→ directory*
→ executable@
→ symlink|
→ FIFO=
→ socket
Example
$ ls -l
total 0
-rw-r--r-- 1 user user 0 Jul 22 10:00 file.txt
drwxr-xr-x 2 user user 4096 Jul 22 10:00 mydir
lrwxrwxrwx 1 user user 10 Jul 22 10:01 link -> file.txt
prw-r--r-- 1 user user 0 Jul 22 10:01 mypipe
srwxr-xr-x 1 user user 0 Jul 22 10:01 mysocket
Understanding ls
Output Colors
If ls
shows colors (enabled by default on most Linux systems), here’s what they mean:
Color | File Type |
---|---|
Blue | Directory |
Green | Executable |
Cyan | Symbolic Link |
Red | Archive or Broken Symlink |
Yellow | Block or Character Device |
Magenta | Image or Media File |
Grey/Dull | Hidden or Unreadable File |
White | Regular File |
You can force colored output using:
ls --color=always
Or disable it:
ls --color=never
touch
– Creating Files
Basic Usage
touch filename
- Creates a new empty file.
- If the file already exists, it updates the timestamp (modification time).
Example
touch hello.txt
Creates an empty file called hello.txt
.
Verify with ls -l
:
ls -l hello.txt
-rw-r--r--. 1 user user 0 Jul 22 10:30 hello.txt
- File is empty (0 bytes).
- Permissions:
rw-
for owner,r--
for group and others.
Creating Multiple Files
touch file1.txt file2.txt file3.txt
Updating Timestamps Only
touch existing_file.txt
This does not change the content — only the modification time.
Summary Table
Command | Purpose | Example | Sample Output / Behavior | Explanation | |
---|---|---|---|---|---|
ls | Lists files/directories in current directory | ls | Desktop Documents Downloads | Basic alphabetical listing | |
ls -l | Long listing format with detailed info | ls -l | -rw-r--r-- 1 user user 2.1K Jul 22 08:59 file.txt | Includes permissions, links, owner, group, size, date, and name | |
ls -a | Shows hidden files | ls -a | .bashrc .config Documents | Lists files starting with . | |
ls -lh | Human-readable sizes | ls -lh | 12K, 2.1K instead of bytes | File sizes in KB, MB, etc. | |
ls -lt | Sorts by modification time | ls -lt | Newest files appear first | Useful for recent changes | |
ls -lS | Sorts by file size | ls -lS | Largest files appear first | Helpful for disk usage tracking | |
ls -R | Recursively lists directories | ls -R | Includes contents of all subdirectories | Useful for full tree views | |
ls --color=auto | Enables colored output | ls --color=auto | Files are color-coded by type | Blue: dir, Green: executable, Cyan: symlink, etc. | |
ls -lF | Appends symbol to indicate file type | ls -lF | / for dirs, * for executables, @ for symlinks, ` | ` for FIFO | Helps visually distinguish file types |
touch | Creates a new file or updates timestamp | touch hello.txt | Creates empty file, or updates timestamp if it exists | File shown as: -rw-r--r-- 1 user user 0 Jul 22 10:30 hello.txt | |
touch file1.txt file2.txt | Creates multiple empty files | touch file1 file2 | Files created instantly | No content inside, size is 0 |
Last updated on