Skip to main content

ls — List Directory Contents

The ls command is the primary way to inspect files and directories in Linux.
If cd moves you and pwd tells you where you are, ls shows you what exists there.

Before editing, deleting, backing up, or debugging anything on a VPS, you should inspect the directory contents with ls.

For WordPress administrators and DevOps engineers, ls is used constantly to:

  • Verify plugin and theme directories
  • Check file permissions
  • Inspect logs
  • Confirm backup files
  • Detect suspicious or recently modified files

Core Purpose​

ls lists files and directories in:

  • The current directory
  • A specified path
  • Multiple paths

Basic usage:

ls

Lists contents of the current directory.

Specify a path:

ls /var/www/html

Core Syntax​

ls [OPTIONS] [FILE|DIRECTORY]

You can combine multiple options:

ls -lah

3. Most Important Options (Practical First)

While ls has many flags, these are the ones used daily in real VPS work.

Long Listing (-l)​

ls -l

Example output:

-rw-r--r-- 1 root root 405 Oct 2 09:12 index.php
drwxr-xr-x 5 root root 4096 Oct 2 09:12 wp-content

Shows:

  • File permissions
  • Number of links
  • Owner
  • Group
  • File size
  • Modification date
  • Name

Essential for debugging WordPress permission issues.

Human Readable Sizes (-h)​

ls -lh

Displays sizes in KB/MB/GB instead of raw bytes.

Show Hidden Files (-a)​

ls -a

Shows:

  • .htaccess
  • .env
  • .git
  • .bashrc

Critical for debugging hidden WordPress configuration files.

Combine Common Flags​

ls -lah

Most commonly used inspection command on Linux systems.

Sort by Modification Time (-t)​

ls -lt

Newest files first.

Useful for:

  • Detecting hacked files
  • Checking recently uploaded backups
  • Debugging logs

Reverse Order (-r)​

ls -ltr

Oldest files first.

Sort by Size (-S)​

ls -lhS

Largest files first.

Helpful for:

  • Finding large backups
  • Detecting disk space usage

Recursive Listing (-R)​

ls -R

Lists directory and all subdirectories recursively.

Useful for:

  • Auditing full WordPress structure
  • Quick structure overview (small projects only)

Show Only Directory Itself (-d)​

ls -ld wp-content

Shows info about the directory, not its contents.

Show Inode Number (-i)​

ls -i

Useful for filesystem-level debugging.

Numeric IDs (-n)​

ls -ln

Shows numeric UID and GID instead of names.

Helpful in containerized or multi-user environments.

4. Output Formatting Options

One Entry Per Line (-1)​

ls -1

Good for scripting:

ls -1 | wc -l

Comma-Separated (-m)​

ls -m

Useful for quick documentation.

Full Timestamp​

ls -l --full-time

Shows exact modification time including seconds and nanoseconds.

Important for forensic analysis after security incidents.

Force Format​

ls --format=long

Ensures consistent formatting (useful in scripts).

5. Sorting & Time Control

Sort by Extension (-X)​

ls -X

Groups .php, .log, .txt files together.

Natural Version Sort (-v)​

ls -v

Sorts like:

file1
file2
file10

Instead of lexicographical sorting.

No Sorting (-U)​

ls -U

Displays entries as stored in directory (fastest).

Control Time Display​

ls -l --time=ctime

Options:

  • atime → Last access time
  • ctime → Metadata change time
  • birth → Creation time (if supported)

6. File Selection & Filtering

Show All Except . and .. (-A)​

ls -A
ls -lL
ls -lP

Ignore Pattern​

ls --ignore=*.log

7. Color & Visual Enhancements

Enable Colors​

ls --color=auto

Most systems already alias this.

Group Directories First​

ls --group-directories-first

Improves readability in WordPress root directories.

Append File Type Indicators​

ls -F

Adds:

  • / for directories
  • * for executables

8. WordPress VPS Practical Examples

1. Inspect WordPress root​

ls -lah /var/www/html

2. Check plugins​

ls -lah /var/www/html/wp-content/plugins

3. Detect recently modified files​

ls -lt /var/www/html

4. Find large backups​

ls -lhS /backups

5. Inspect Nginx logs​

ls -lh /var/log/nginx

6. Check hidden config​

ls -la /var/www/html | grep htaccess

7. Count files​

ls -1 | wc -l

8. Recursive structure​

ls -R wp-content

9. Check directory permissions​

ls -ld wp-content

10. Investigate suspicious files​

ls -lt --full-time

9. Troubleshooting Matrix

ProblemCauseFix
---
Hidden file not visibleMissing -aUse ls -a
Wrong orderSorting option activeAdd/remove -t, -S, -r
Permission deniedNo access rightsUse sudo or change directory
Output messyToo many filesUse -1 or pipe to less

10. Cheat Sheet

ls # List current directory
ls -l # Long format
ls -a # Show hidden files
ls -lah # Long + all + human-readable
ls -lt # Sort by time
ls -lhS # Sort by size
ls -R # Recursive
ls -ld dir # Directory info only
ls --color # Colored output

11. Final Perspective

ls is more than a listing tool. It is your inspection and verification command.

Before:

  • Editing WordPress files
  • Changing permissions
  • Deleting backups
  • Restarting services
  • Investigating hacks

Always inspect the directory with ls.

In Linux administration, visibility prevents mistakes.

Mini Knowledge Check​

  1. What does ls -lah show?
  2. How do you list files sorted by newest first?
  3. Which flag shows hidden files?
  4. How do you display directory permissions without listing its contents?

If you'd like next:

- A full navigation module (`pwd`, `cd`, `ls`, `tree`)
- A WordPress VPS filesystem map
- A permissions deep dive (`chmod`, `chown`, `stat`)
- Or a security-focused file inspection guide

Tell me the direction.