Skip to content
Home » Make the Most of the ls Command

Make the Most of the ls Command

The ls command is one of the most frequently used commands in Bash, and for good reason – it allows you to easily view the contents of a directory. In this blog post, we’ll cover everything you need to know about the ls command, including its basic usage, options, and advanced features.

Basic Usage

The basic syntax for the ls command is as follows:

ls [options] [files or directories]

When used without any options or arguments, ls will display the contents of the current working directory. For example, if you’re in the /home directory and run ls, you’ll see a list of all the files and subdirectories in that directory.

ls
Desktop  Documents  Downloads  Music  Pictures  Public  Templates  Videos

You can also use ls to display the contents of a specific directory. For example, to view the contents of the /usr/local directory, you would run ls /usr/local.

ls /usr/local
bin  include  lib  share

Options

The ls command has a number of options that allow you to customize its behavior. Some of the most commonly used options include:

  • a: Show all files, including hidden files (those that begin with a .).
  • l: Display the contents of a directory in a long format, showing additional information such as file permissions, owner, and size.
ls -l
total 64
drwxrwxr-x 2 user group  4096 Jan 15 21:22 Desktop
drwxrwxr-x 2 user group  4096 Jan 15 21:22 Documents
drwxrwxr-x 2 user group  4096 Jan 15 21:22 Downloads
drwxrwxr-x 2 user group  4096 Jan 15 21:22 Music
drwxrwxr-x 2 user group  4096 Jan 15 21:22 Pictures
drwxrwxr-x 2 user group  4096 Jan 15 21:22 Public
drwxrwxr-x 2 user group  4096 Jan 15 21:22 Templates
drwxrwxr-x 2 user group  4096 Jan 15 21:22 Videos
  • t: Sort the contents of a directory by modification time (with the most recently modified files appearing first).
ls -t
Desktop  Documents  Downloads  Music  Pictures  Public  Templates  Videos
  • h: Show file sizes in “human-readable” format (e.g., “1K” instead of “1024”).
ls -lh
total 64K
drwxrwxr-x 2 user group 4.0K Jan 15 21:22 Desktop
drwxrwxr-x 2 user group 4.0K Jan 15 21:22 Documents
drwxrwxr-x 2 user group 4.0K Jan 15 21:22 Downloads
drwxrwxr-x 2 user group 4.0K Jan 15 21:22 Music
drwxrwxr-x 2 user group 4.0K Jan 15 21:22 Pictures
drwxrwxr-x 2 user group 4.0K Jan 15 21:22 Public
drwxrwxr-x 2 user group 4.0K Jan 15 21:22 Templates
drwxrwxr-x 2 user group 4.0K Jan 15 21:22 Videos
  • You can also combine multiple options together. For example, ls -alt will show all files in a directory, sorted by modification time, and displayed in a long format.
ls -alt
total 64
drwxrwxr-x 2 user group 4096 Jan 15 21:22 .
drwxrwxr-x 7 user group 4096 Jan 15 21:22 ..
-rw-rw-r-- 1 user group 21 Jan 15 21:22 .hiddenfile
drwxrwxr-x 2 user group 4096 Jan 15 21:22 Desktop
drwxrwxr-x 2 user group 4096 Jan 15 21:22 Documents
drwxrwxr-x 2 user group 4096 Jan 15 21:22 Downloads
drwxrwxr-x 2 user group 4096 Jan 15 21:22 Music
drwxrwxr-x 2 user group 4096 Jan 15 21:22 Pictures
drwxrwxr-x 2 user group 4096 Jan 15 21:22 Public
drwxrwxr-x 2 user group 4096 Jan 15 21:22 Templates
drwxrwxr-x 2 user group 4096 Jan 15 21:22 Videos

Colorized Output

The ls command can be configured to display output in color, making it easier to distinguish between different types of files. By default, this feature is enabled in most modern shells.

To enable colorized output in ls, you can use the --color option.

ls --color
Desktop Documents Downloads Music Pictures Public Templates Videos

You can also configure the colors used by ls by modifying the LS_COLORS environment variable.

LS_COLORS='di=1;35'

Recursive Listing

The ls command also has the ability to recursively list the contents of a directory and all of its subdirectories. You can use the -R option to enable this feature.

ls -R
Desktop:
file1 file2 file3

Desktop/subdir1:
file4 file5

Desktop/subdir1/subdir2:
file6

File Globbing

The ls command also supports file globbing, which allows you to use wildcard characters to match multiple files at once. For example, the command ls *.txt will display all files in the current directory that have the .txt file extension.

ls *.txt
file1.txt file2.txt file3.txt

Leave a Reply

Your email address will not be published. Required fields are marked *

eleven − 7 =