ls

Basic Usage of ls - Listing Files and Directories

The ls command is one of the most commonly used Linux commands. It is used to display files and directories inside the current working directory.

ls

Example output:

Documents  Downloads  Pictures  script.sh  notes.txt
  • ls stands for “list”
  • Displays files and directories
  • Used constantly in Linux administration
  • One of the first commands every Linux user learns

Long Listing Format

To display detailed information about files:

ls -l

Example output:

-rw-r--r-- 1 user user  1200 May 12 18:20 notes.txt
drwxr-xr-x 2 user user  4096 May 12 18:10 Documents
-rwxr-xr-x 1 user user   850 May 12 18:25 script.sh

The columns represent:

Column Meaning
-rw-r--r-- File permissions
1 Number of hard links
user File owner
user Group owner
1200 File size in bytes
May 12 18:20 Last modification time
notes.txt File name

Understanding File Types

The first character in the permission field indicates file type.

Examples:

Character Meaning
- Regular file
d Directory
l Symbolic link
c Character device
b Block device

Example:

drwxr-xr-x
  • d means directory

Example:

-rw-r--r--
  • - means regular file

Human Readable File Sizes

The default long format displays file sizes in bytes.

To display human-readable sizes:

ls -lh

Example output:

-rw-r--r-- 1 user user 1.2K May 12 18:20 notes.txt
-rwxr-xr-x 1 user user 850B May 12 18:25 script.sh
  • -h means “human readable”
  • Displays KB, MB, GB instead of raw bytes
  • Makes large files easier to analyze

Displaying Hidden Files

Linux hidden files begin with a dot (.).

To display hidden files:

ls -a

Example output:

.  ..  .bashrc  .profile  notes.txt
  • . represents current directory
  • .. represents parent directory
  • Hidden files are commonly used for configuration

Examples of hidden files:

  • .bashrc
  • .gitconfig
  • .ssh

Combining Long Format with Hidden Files

Administrators often combine options together.

ls -la

This displays:

  • detailed information
  • hidden files
  • permissions
  • ownership
  • timestamps

This is one of the most commonly used combinations in Linux.


Sorting Files by Modification Time

To sort files by newest first:

ls -lt

Example output:

-rw-r--r-- 1 user user 1200 May 12 19:00 report.txt
-rw-r--r-- 1 user user  850 May 12 18:45 notes.txt
  • -t sorts files by modification time
  • Newest files appear first
  • Useful during troubleshooting

Reversing Sort Order

To reverse sorting order:

ls -ltr
  • -r reverses sorting order
  • Oldest files appear first
  • Commonly used when analyzing logs

Recursive Directory Listing

To list files recursively:

ls -R

Example:

project:
file1.txt  src

project/src:
main.py  config.py
  • -R means recursive
  • Displays subdirectories and their contents
  • Useful for inspecting directory structures

Displaying File Sizes

To sort files by size:

ls -lhS

Example output:

-rw-r--r-- 1 user user 2.1G backup.img
-rw-r--r-- 1 user user 120M database.sql
-rw-r--r-- 1 user user 1.2K notes.txt
  • -S sorts by file size
  • Largest files appear first
  • Useful for disk usage analysis

Displaying Inode Numbers

Every file in Linux has an inode number.

To display inode numbers:

ls -i

Example output:

183920 file1.txt
183921 file2.txt
  • Inodes store filesystem metadata
  • Useful for troubleshooting hard links
  • Helpful during filesystem analysis

To inspect symbolic links:

ls -l

Example output:

lrwxrwxrwx 1 user user 11 May 12 18:00 latest -> releases/v2
  • l at beginning means symbolic link
  • -> shows link destination
  • Common in deployments and system administration

Colorized Output

Most modern Linux distributions support colored output automatically.

Example:

ls --color=auto

Colors help distinguish:

  • directories
  • executables
  • symbolic links
  • compressed files

This improves readability significantly.


Listing One File Per Line

To display one file per line:

ls -1

Example output:

Documents
Downloads
notes.txt
script.sh
  • Useful in scripts
  • Easier to parse automatically
  • Cleaner for long outputs

Combining Multiple Options

Linux commands allow multiple options to be combined together.

Example:

ls -lah

This combines:

  • -l → long format
  • -a → hidden files
  • -h → human-readable sizes

Another example:

ls -lhtr

This displays:

  • detailed information
  • human-readable sizes
  • sorted by time
  • reversed order

Practical Administration Examples

Check newest log files:

ls -lt /var/log

Check hidden SSH configuration:

ls -la ~/.ssh

Find largest files quickly:

ls -lhS

Inspect deployment symlinks:

ls -l /var/www

Practical Script Example (Step-by-Step Explanation)

Script

#!/bin/bash

TARGET="/var/log"

echo "Largest files in $TARGET"

ls -lhS $TARGET | head -5

Step 1: Shebang

#!/bin/bash
  • Defines Bash interpreter
  • Ensures script runs consistently

Step 2: Variable definition

TARGET="/var/log"
  • Stores directory path in variable
  • Makes script easier to modify later

Example:

TARGET=/var/log

Step 3: Printing informational message

echo "Largest files in $TARGET"
  • Displays readable output for user
  • $TARGET expands variable value

Result:

Largest files in /var/log

Step 4: Listing files sorted by size

ls -lhS $TARGET

Breakdown:

  • -l = long format
  • -h = human readable sizes
  • -S = sort by size

This displays files from largest to smallest.


Step 5: Limiting output

head -5
  • Displays first 5 lines only
  • Prevents overwhelming output
  • Useful for large directories

What this script does

Step-by-step flow:

  1. Defines target directory
  2. Prints informational message
  3. Lists files sorted by size
  4. Displays only top 5 entries

Real-world usage

This type of script is useful for:

  • disk cleanup
  • identifying large log files
  • troubleshooting storage issues
  • server maintenance
  • monitoring storage growth

Summary

In this guide, you learned:

  • basic file listing with ls
  • long listing format
  • file permissions
  • hidden files
  • sorting by time and size
  • recursive listings
  • inode display
  • symbolic links
  • combining multiple options
  • practical scripting with ls

These skills are essential for:

  • Linux administration
  • filesystem navigation
  • troubleshooting
  • server maintenance
  • shell scripting

Additional ls parameters not covered in this guide include:

-d: Display directories themselves instead of contents
-F: Append indicators to file names
-n: Display numeric UID and GID
-p: Append / indicator to directories
-Q: Surround file names with quotes
-X: Sort alphabetically by extension
--full-time: Display full timestamps
--help: Display help information