|

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
CommandDescription
ls /varList contents of /var
ls /var ~List both /var and your home directory
ls ~ -lList home directory with long format
ls ~ -laLong format + show hidden files
ls ~ -ltLong format + sort by modification time
ls --helpShow 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
PartExampleNotes
CommandlsThe program to run
Options-l, -lt, -laModify behavior. Short options use -
Long options--helpUse -- 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)
ColumnMeaningExample
1stPermissions (rights to the file)-rw-r--r--
2ndHard links count1
3rdFile ownerkronos
4thGroup ownerusers
5thFile size in bytes4096
6thLast modification dateJan 15 10:30
7thFilenamefile.txt

First character — file type:

CharacterMeaning
-Regular file
dDirectory
lSymbolic link
cCharacter device
bBlock device
sSocket
pPipe

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
CommandDescription
file *Show file types for all files
file DownloadsShow file type for Downloads
file filenameShow file type for filename
file filename -bBrief mode — only the type, no filename
file [A-D]*Files in range A to D
file filename -iShow 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

OutputMeaning
ASCII textPlain text file
UTF-8 Unicode textUnicode text file
Bourne-Again shell scriptBash script
Python scriptPython script
JPEG image dataJPEG image
PNG image dataPNG image
PDF documentPDF file
Zip archive dataZIP archive
directoryFolder
emptyEmpty file

The less Command

Display file contents one page at a time — perfect for reading long files.

less /etc/passwd

Navigating in less:

KeyAction
/ Scroll one line up / down
PgUp / PgDnScroll one page up / down
HomeGo to the beginning of the file
EndGo to the end of the file
25g + EnterGo to the 25th line
qQuit less
/patternSearch forward for pattern
nNext search match
NPrevious 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

OptionDescription
-lLong format (detailed)
-aAll files (including hidden)
-laLong + all
-ltLong + sort by time
-ltrLong + sort by time (reversed)
-lhLong + human-readable sizes
--helpShow help

ls -l Columns

ColumnMeaning
1Permissions + file type
2Hard links
3Owner
4Group
5Size (bytes)
6Modification date
7Filename

File Type Characters

CharMeaning
-Regular file
dDirectory
lSymbolic link
cCharacter device
bBlock device
sSocket
pPipe

file Options

OptionDescription
-bBrief mode (no filename)
-iShow MIME type
*All files
[A-D]*Range wildcard

less Navigation

KeyAction
/ Line up / down
PgUp / PgDnPage up / down
Home / EndBeginning / end
25g + EnterGo to line 25
/patternSearch
n / NNext / previous match
qQuit

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

PitfallProblemSolution
Using cat on big filesFloods terminalUse less
Missing hidden filesCan’t see .bashrcUse ls -a
Guessing file typeWrong program opens itUse file
Forgeting q in lessStuck in pagerPress q
Wildcards matching too muchUnexpected resultsQuote or narrow pattern

Summary

CommandPurposeExample
lsList directory contentsls -la ~
fileIdentify file typefile *
lessView file page by pageless /etc/passwd

Key takeaways:

  • ls -l gives you detailed file info in 7 columns
  • First character of permissions = file type (- file, d directory)
  • file identifies actual content, not extensions
  • less is 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!