Linux CLI 3 🐧 ls, file and less commands
Three essential commands for exploring and viewing files: ls (list directory contents), file (identify file types), and less (view file contents page by page).
The ls Command
List the contents of a directory with detailed information about files.
ls /var
ls /var ~
ls ~ -l
ls ~ -la
ls ~ -lt
ls --help
| Command | Description |
|---|---|
ls /var | List contents of /var |
ls /var ~ | List both /var and your home directory |
ls ~ -l | List home directory with long format |
ls ~ -la | Long format + show hidden files |
ls ~ -lt | Long format + sort by modification time |
ls --help | Show help for the ls command |
Commands, Options, and Arguments
Every command has three parts:
ls -la /var
│ │ │
│ │ └── Argument (what to operate on)
│ └──────── Option(s) — modify behavior
└────────────── Command
| Part | Example | Notes |
|---|---|---|
| Command | ls | The program to run |
| Options | -l, -lt, -la | Modify behavior. Short options use - |
| Long options | --help | Use -- before a word |
| Arguments | /var, ~ | What the command acts on |
Rule:
--always comes before a word option (e.g.,--help,--all,--human-readable).
Combining short options:
ls -la # Same as: ls -l -a
ls -ltr # Long + time + reverse
Understanding ls -l Output
-rw-r--r-- 1 kronos users 4096 Jan 15 10:30 file.txt
│ │ │ │ │ │ │
│ │ │ │ │ │ └── Filename
│ │ │ │ │ └────────────── Last modification date
│ │ │ │ └─────────────────── File size (bytes)
│ │ │ └────────────────────────── Group owner
│ │ └───────────────────────────────── File owner
│ └─────────────────────────────────── Hard links count
└─────────────────────────────────────────────── Permissions (rights)
| Column | Meaning | Example |
|---|---|---|
| 1st | Permissions (rights to the file) | -rw-r--r-- |
| 2nd | Hard links count | 1 |
| 3rd | File owner | kronos |
| 4th | Group owner | users |
| 5th | File size in bytes | 4096 |
| 6th | Last modification date | Jan 15 10:30 |
| 7th | Filename | file.txt |
First character — file type:
| Character | Meaning |
|---|---|
- | Regular file |
d | Directory |
l | Symbolic link |
c | Character device |
b | Block device |
s | Socket |
p | Pipe |
Example output:
drwxr-xr-x 5 kronos users 4096 Jan 15 10:30 Documents
-rw-r--r-- 1 kronos users 1024 Jan 15 10:30 notes.txt
lrwxrwxrwx 1 kronos users 10 Jan 15 10:30 link -> /etc
Documents→ directory (d)notes.txt→ regular file (-)link→ symbolic link (l)
The file Command
Determine the type of a file (based on content, not extension).
file *
file Downloads
file file
file file -b
file [A-D]*
file file -i
| Command | Description |
|---|---|
file * | Show file types for all files |
file Downloads | Show file type for Downloads |
file filename | Show file type for filename |
file filename -b | Brief mode — only the type, no filename |
file [ A-D ]* | Files in range A to D |
file filename -i | Show MIME type |
Example:
$ file *
notes.txt: ASCII text
photo.jpg: JPEG image data, JFIF standard
script.sh: Bourne-Again shell script
archive.tar: POSIX tar archive
Documents: directory
Brief mode:
$ file notes.txt -b
ASCII text
MIME type mode:
$ file notes.txt -i
text/plain; charset=us-ascii
$ file photo.jpg -i
image/jpeg
Wildcards for ranges:
$ file [A-D]*
apple.txt: ASCII text
banana.txt: ASCII text
cherry.txt: ASCII text
date.txt: ASCII text
# Only files starting with A, B, C, or D
Common File Types Detected
| Output | Meaning |
|---|---|
ASCII text | Plain text file |
UTF-8 Unicode text | Unicode text file |
Bourne-Again shell script | Bash script |
Python script | Python script |
JPEG image data | JPEG image |
PNG image data | PNG image |
PDF document | PDF file |
Zip archive data | ZIP archive |
directory | Folder |
empty | Empty file |
The less Command
Display file contents one page at a time — perfect for reading long files.
less /etc/passwd
Navigating in less:
| Key | Action |
|---|---|
↑ / ↓ | Scroll one line up / down |
PgUp / PgDn | Scroll one page up / down |
Home | Go to the beginning of the file |
End | Go to the end of the file |
25g + Enter | Go to the 25th line |
q | Quit less |
/pattern | Search forward for pattern |
n | Next search match |
N | Previous search match |
Example:
$ less /etc/passwd
Display:
root:x:0:0:root:/root:/bin/bash
daemon:x:1:1:daemon:/usr/sbin:/usr/sbin/nologin
bin:x:2:2:bin:/bin:/usr/sbin/nologin
sys:x:3:3:sys:/dev:/usr/sbin/nologin
sync:x:4:65534:sync:/bin:/bin/sync
games:x:5:60:games:/usr/games:/usr/sbin/nologin
man:x:6:12:man:/var/cache/man:/usr/sbin/nologin
lp:x:7:7:lp:/var/spool/lpd:/usr/sbin/nologin
mail:x:8:8:mail:/var/mail:/usr/sbin/nologin
news:x:9:9:news:/var/spool/news:/usr/sbin/nologin
:
The : at the bottom means there’s more content below. Press ↓ or PgDn to continue.
Jumping to a specific line:
25g + Enter → Go to line 25
Quitting:
q → Exit less
Complete Example Session
Let’s walk through a real exploration:
# 1. List the /var directory
kronos@olympos:~$ ls /var
backups cache crash lib local lock log mail opt run spool tmp
# 2. List both /var and home with details
kronos@olympos:~$ ls /var ~ -la
/var:
total 48
drwxr-xr-x 12 root root 4096 Jan 15 10:30 .
drwxr-xr-x 20 root root 4096 Jan 15 10:30 ..
drwxr-xr-x 3 root root 4096 Jan 15 10:30 backups
drwxr-xr-x 20 root root 4096 Jan 15 10:30 cache
...
/home/kronos:
total 32
drwxr-xr-x 5 kronos users 4096 Jan 15 10:30 .
drwxr-xr-x 3 root root 4096 Jan 15 10:30 ..
-rw-r--r-- 1 kronos users 220 Jan 15 10:30 .bash_logout
-rw-r--r-- 1 kronos users 3771 Jan 15 10:30 .bashrc
drwxr-xr-x 2 kronos users 4096 Jan 15 10:30 Documents
...
# 3. Sort by modification time (newest first)
kronos@olympos:~$ ls ~ -lt
total 32
-rw-r--r-- 1 kronos users 1024 Jan 15 12:45 notes.txt
-rw-r--r-- 1 kronos users 4096 Jan 15 10:30 .bashrc
drwxr-xr-x 2 kronos users 4096 Jan 15 10:30 Documents
# 4. Check file types
kronos@olympos:~$ file *
.bashrc: ASCII text
.bash_logout: ASCII text
Documents: directory
notes.txt: ASCII text
# 5. Check a specific file with MIME type
kronos@olympos:~$ file notes.txt -i
text/plain; charset=us-ascii
# 6. Check files in range A-D
kronos@olympos:~$ file [A-D]*
Documents: directory
Desktop: directory
Downloads: directory
# 7. View a file with less
kronos@olympos:~$ less /etc/passwd
# Press ↓, ↑, PgUp, PgDn to navigate
# Press 25g + Enter to jump to line 25
# Press q to quit
# 8. Get help for ls
kronos@olympos:~$ ls --help
Usage: ls [OPTION]... [FILE]...
List information about the FILEs (the current directory by default).
...
Quick Reference
ls Options
| Option | Description |
|---|---|
-l | Long format (detailed) |
-a | All files (including hidden) |
-la | Long + all |
-lt | Long + sort by time |
-ltr | Long + sort by time (reversed) |
-lh | Long + human-readable sizes |
--help | Show help |
ls -l Columns
| Column | Meaning |
|---|---|
| 1 | Permissions + file type |
| 2 | Hard links |
| 3 | Owner |
| 4 | Group |
| 5 | Size (bytes) |
| 6 | Modification date |
| 7 | Filename |
File Type Characters
| Char | Meaning |
|---|---|
- | Regular file |
d | Directory |
l | Symbolic link |
c | Character device |
b | Block device |
s | Socket |
p | Pipe |
file Options
| Option | Description |
|---|---|
-b | Brief mode (no filename) |
-i | Show MIME type |
* | All files |
[ A-D ]* | Range wildcard |
less Navigation
| Key | Action |
|---|---|
↑ / ↓ | Line up / down |
PgUp / PgDn | Page up / down |
Home / End | Beginning / end |
25g + Enter | Go to line 25 |
/pattern | Search |
n / N | Next / previous match |
q | Quit |
Best Practices
✅ Do This:
# Use tab completion for paths
ls /v[Tab] # Completes to /var
# Combine options for efficiency
ls -lah # Long, all, human-readable
# Use file to check before opening
file unknown.dat
# Then use the appropriate program
# Use less instead of cat for long files
less /var/log/syslog # Navigate page by page
❌ Don’t Do This:
# Don't use cat for large files
cat /var/log/syslog # ❌ Floods your screen
less /var/log/syslog # ✅ Page-by-page
# Don't guess file types by extension
# Extensions are just conventions on Linux!
file script.txt # Could be anything
# Don't forget to quit less
less file.txt
# Press q to exit — Ctrl+C also works
Common Pitfalls
| Pitfall | Problem | Solution |
|---|---|---|
Using cat on big files | Floods terminal | Use less |
| Missing hidden files | Can’t see .bashrc | Use ls -a |
| Guessing file type | Wrong program opens it | Use file |
Forgeting q in less | Stuck in pager | Press q |
| Wildcards matching too much | Unexpected results | Quote or narrow pattern |
Summary
| Command | Purpose | Example |
|---|---|---|
ls | List directory contents | ls -la ~ |
file | Identify file type | file * |
less | View file page by page | less /etc/passwd |
Key takeaways:
ls -lgives you detailed file info in 7 columns- First character of permissions = file type (
-file,ddirectory) fileidentifies actual content, not extensionslessis essential for reading long files without flooding the terminal- Options modify command behavior —
-l,-a,-t,--help
Stop using slow, ad-bloated tool sites! 🤮
🔎 Search “KandZ Tools” on Google to use many professional utilities for free.
KandZ.me is the ultimate minimalist hub for:
✅ Finance (Mortgage, Interest, Inflation)
✅ Tech (Base64, JSON, Dev Suite, IP)
✅ Health (BMI, BMR, TDEE)
✅ Productivity (Timer, Workspace, QR)
⚡️ Fast & Private
🔒 No data leaves your device
💎 100% Free
🔗 Use it now: https://tools.kandz.me
🔖 Bookmark it—you’ll need it later!