Linux ls Command


The ls command in Ubuntu lists files and directories in a specified directory, helping you view and organize the contents of your filesystem. By default, running ls will display the contents of the current directory, but it also comes with several options to modify the output.

Basic Usage of ls

ls [options] [directory]
  • If no directory is specified, ls lists the contents of the current directory.
  • You can use various options with ls to customize the output.

1. Basic ls Command

ls

Example output:

Desktop Documents Downloads Music Pictures Videos

In this example, ls shows the top-level folders within the current directory, each listed with a space between the names.

2. Common Options for ls

1. ls -l (Long Listing Format)

The -l option provides a detailed, long listing format that includes file permissions, ownership, size, and modification date.

ls -l

Example output:

drwxr-xr-x 2 user user 4096 Oct 27 10:24 Desktop -rw-r--r-- 1 user user 1024 Oct 27 10:25 file.txt

Explanation of the output:

  • drwxr-xr-x: File permissions and type (d means directory; - means file).
  • 2: Number of links (a count of subdirectories or references).
  • user: The owner of the file or directory.
  • user: The group associated with the file.
  • 4096: Size of the file or directory in bytes.
  • Oct 27 10:24: Date and time of the last modification.
  • Desktop: The name of the file or directory.

2. ls -a (All Files, Including Hidden)

The -a option shows all files, including hidden ones (files and directories starting with .).

ls -a

Example output:

. .. .bashrc .profile Desktop Documents
  • .: Represents the current directory.
  • ..: Represents the parent directory.
  • .bashrc, .profile: Hidden files commonly used for configuration.

3. ls -h (Human-Readable File Sizes)

The -h option, when combined with -l (ls -lh), displays file sizes in human-readable format (e.g., KB, MB, GB).

ls -lh

Example output:

drwxr-xr-x 2 user user 4.0K Oct 27 10:24 Desktop -rw-r--r-- 1 user user 1.0K Oct 27 10:25 file.txt

4. ls -R (Recursive Listing)

The -R option lists all files and directories recursively, including subdirectories.

ls -R

Example output:

Desktop: file1.txt file2.txt Documents: project1 project2 Documents/project1: file3.txt

In this example, ls -R lists the contents of each directory and any subdirectories within them.

5. ls -t (Sort by Modification Time)

The -t option sorts files and directories by modification time, with the most recently modified items listed first.

ls -lt

Example output:

-rw-r--r-- 1 user user 1024 Oct 27 11:25 recent_file.txt drwxr-xr-x 2 user user 4096 Oct 27 10:24 older_folder

6. Combining Options

You can combine options to get more tailored results. For example:

  • ls -la: Detailed listing of all files, including hidden files.
  • ls -lhS: Detailed listing with human-readable file sizes, sorted by file size.

Example with ls -la:

ls -la

Example output:

drwxr-xr-x 2 user user 4096 Oct 27 10:24 . drwxr-xr-x 10 user user 4096 Oct 27 09:00 .. -rw-r--r-- 1 user user 220 Oct 27 09:01 .bashrc drwxr-xr-x 2 user user 4096 Oct 27 10:24 Documents

Summary of Common ls Options

CommandDescription
lsLists files and directories in the current directory
ls -lLong format listing with details
ls -aIncludes hidden files (those starting with .)
ls -hHuman-readable sizes (to be used with -l)
ls -RRecursive listing (includes subdirectories)
ls -tSorts by modification time
ls -laCombines long format with all files, including hidden

The ls command with its various options is a powerful tool for viewing and managing files and directories in Ubuntu. By using the appropriate options, you can get all the information you need about file permissions, sizes, modification times, and more.