Skip to main content

ls command

ls - List

Shows or lists the contents i.e., files and directories of the current working directory.

Usage: ls [OPTION]... [FILE]...

List information about the FILEs (the current directory by default). Sorts entries alphabetically if none of -cftuvSUX nor --sort is specified.

$ ls --help
Usage: ls [OPTION]... [FILE]...
List information about the FILEs (the current directory by default).
Sort entries alphabetically if none of -cftuvSUX nor --sort is specified.

Mandatory arguments to long options are mandatory for short options too.
-a, --all do not ignore entries starting with .
-A, --almost-all do not list implied . and ..
--author with -l, print the author of each file
-b, --escape print C-style escapes for nongraphic characters
--block-size=SIZE with -l, scale sizes by SIZE when printing them;
e.g., '--block-size=M'; see SIZE format below
-B, --ignore-backups do not list implied entries ending with ~
-c with -lt: sort by, and show, ctime (time of last
modification of file status information);
with -l: show ctime and sort by name;
otherwise: sort by ctime, newest first
-C list entries by columns
--color[=WHEN] colorize the output; WHEN can be 'always' (default
if omitted), 'auto', or 'never'; more info below
-d, --directory list directories themselves, not their contents
...
...
... contd..

To read the complete documentation about how to use ls use man pages

$ man ls

Some of the most used ls command options are listed below

  • ls - Just using without any options lists the files and directories in the current working directory, often color-coded to distinguish between file types (directories, executables, etc.).

    $ ls
    file1.txt file2.pdf folder1 folder2
  • ls -r or ls --reverse - Lists content in reverse order while sorting, best used in a scenario where there are huge results for ls to avoid scrolling for the last

    $ ls -r
    folder2 folder1 file2.pdf file1.txt
  • ls -l - Long listing format with file type and permissions, number of hard links, owner name, group name, file size(bytes), last modification time, file/directory name.

    $ ls -l
    total 8
    -rw-r--r-- 1 ubuntu ubuntu 0 Feb 18 17:00 file1.txt
    -rw-r--r-- 1 ubuntu ubuntu 0 Feb 18 17:00 file2.pdf
    drwxr-xr-x 2 ubuntu ubuntu 4096 Feb 18 17:00 folder1
    drwxr-xr-x 2 ubuntu ubuntu 4096 Feb 18 17:00 folder2

    The first part of the long listing (-rw-r--r--) represents file permissions. It's broken down into three sets of three characters:

    • First character: File type (- for file, d for directory, l for symbolic link, etc.)
    • Next three (owner permissions): r (read), w (write), x (execute). - means permission is denied.
    • Next three (group permissions): r (read), w (write), x (execute). - means permission is denied.
    • Last three (others permissions): r (read), w (write), x (execute). - means permission is denied.
  • ls -lh - -h or --human-readable - Print the file and directory sizes as kilobytes (kB), Megabytes (MB) or Gigabytes (GB) like 1K 234M 2G etc.

    $ ls -lh
    total 8.0K
    -rw-r--r-- 1 ubuntu ubuntu 0 Feb 18 17:00 file1.txt
    -rw-r--r-- 1 ubuntu ubuntu 0 Feb 18 17:00 file2.pdf
    drwxr-xr-x 2 ubuntu ubuntu 4.0K Feb 18 17:00 folder1
    drwxr-xr-x 2 ubuntu ubuntu 4.0K Feb 18 17:00 folder2
  • ls -a - Lists all hidden files, directories without ignoring entries starting with .. Hidden files are usually system files that begin with a full stop or a period

    $ ls -la
    total 20
    drwxr-xr-x 5 ubuntu ubuntu 4096 Feb 18 17:44 .
    drwxr-x--- 24 ubuntu ubuntu 4096 Feb 18 17:00 ..
    drwxr-xr-x 2 ubuntu ubuntu 4096 Feb 18 17:44 .git
    -rw-r--r-- 1 ubuntu ubuntu 0 Feb 18 17:00 file1.txt
    -rw-r--r-- 1 ubuntu ubuntu 0 Feb 18 17:00 file2.pdf
    drwxr-xr-x 2 ubuntu ubuntu 4096 Feb 18 17:00 folder1
    drwxr-xr-x 2 ubuntu ubuntu 4096 Feb 18 17:00 folder2
  • ls -R - List subdirectories recursively

    $ ls -R
    .:
    file1.txt file2.pdf folder1 folder2

    ./folder1:
    docs

    ./folder1/docs:
    node

    ./folder1/docs/node:

    ./folder2:

  • ls -n - -n or --numeric-uid-gid like -l, but lists numeric user and group IDs

    $ ls -n
    total 8
    -rw-r--r-- 1 1000 1000 0 Feb 18 17:00 file1.txt
    -rw-r--r-- 1 1000 1000 0 Feb 18 17:00 file2.pdf
    drwxr-xr-x 3 1000 1000 4096 Feb 18 18:14 folder1
    drwxr-xr-x 2 1000 1000 4096 Feb 18 17:00 folder2