Cantech Knowledge Base

Your Go-To Hosting Resource

How to Use the ls Command in Linux?

The ls command is a core utility within Linux and Unix-like operating systems and serves as one of the most basic yet essential tools for file and directory management. It stands for “list” and is used to display the contents of a specified directory or the current working directory if no directory is specified. In this blog we will walk you through the steps on how to use the ls command in Linux to efficiently operate through a file system.

Is Command Syntax

The ls command in Linux follows a specific syntax that allows users to customize how directory contents are displayed. Here’s a breakdown of the ls command syntax:

ls [OPTIONS] [FILE...]

In this command, [options] are flags that change how the command works, while [file/directory] names what to list; if omitted, ls shows the current directory’s contents.

Here’s the information about the ls command options, specifically for Basic Options, Color

Format, and File Type Indicators:

Category Option Description
Basic Options -a Lists all entries, including hidden files (those starting with .).
-A Similar to -a, but does not list . and ...
-l Long listing format, showing detailed file information.
-h With -l, shows human-readable sizes (e.g., 1K, 2M, 3G).
-t Sort by modification time, newest first.
-r Reverse the order while sorting.
-S Sort by file size, largest first.
-R Recursively list subdirectories.
-d List directories themselves, not their contents.
-1 List one file per line.
Color Format –color Colorize the output. Use –color=auto for automatic color in terminals.
File Type Indicators -F Appends indicator characters (e.g., / for directories, * for executables).
-p Appends / to directory entries.

This table provides a quick reference for some of the most commonly used options when working with the ls command in Linux. Remember, the actual options and their behaviors can vary slightly between different versions of ls or different Linux distributions.

Examples of the ls Command

Here are some practical examples of the ls command along with what their outputs might look like. Note that the exact output can vary based on your system’s configuration, file permissions, and the actual content of the directories.

1. Basic Listing (ls)

Lists the contents of the current directory, showing files and subdirectories.

ls

Output

Documents  Downloads  Music  Pictures  Videos

This lists the contents of the current directory in a simple format.

2. Long Listing (ls -l)

The ls -l command in Linux is used to list directory contents in a long format, providing detailed information about each file or directory.

ls -l

Here, you get detailed information including permissions, number of links, owner, group, size, and modification time.

Output

drwxr-xr-x  2 user user 4096 Feb 11 10:30 Documents
drwxr-xr-x  3 user user 4096 Feb 11 09:45 Downloads
-rw-r--r--  1 user user 1234 Feb 11 08:15 file.txt

3. List All Entries Including Hidden (ls -a)

The ls -a command in Linux is used to list all entries in a directory, including hidden files.

ls -a

Output

.  ..  .bashrc  .profile  Documents  Downloads

4. Sorted by Modification Time (ls -lt)

The ls -lt command in Linux is used to list directory contents in a long format, sorted by modification time

ls -lt

Output

-rw-r--r--  1 user user 1.2K Feb 11 10:45 recent_file.txt
drwxr-xr-x  3 user user 4.0K Feb 11 09:45 Downloads

5. Human-Readable Sizes (ls -lh)

The ls -lh command in Linux is used to list directory contents in a long format with human-readable file sizes.

ls -lh

Output

drwxr-xr-x  2 user user 4.0K Feb 11 10:30 Documents
-rw-r--r--  1 user user 1.2K Feb 11 08:15 file.txt

This is similar to -l but with human-readable file sizes.

6. Recursive Listing (ls -R)

The ls -R command in Linux is used to list directory contents recursively.

ls -lr

Output

.:
Documents  Downloads  file.txt

./Documents:
report.pdf

./Downloads:
music.mp3

7. Viewing Inode Numbers (ls -i)

The ls -i command in Linux is used to display the inode numbers of files and directories along with their names.

ls -i

Output

1234567  Documents  1234568  Downloads  1234569  file.txt

8. Listing group ownership of files and directories (ls -g)

The ls -g command in Linux is used to list directory contents in a format similar to the long listing (-l) but without showing the owner’s name.

ls -g

Output

drwxr-xr-x  2  group  4096 Feb 11 10:30 Documents
drwxr-xr-x  3  group  4096 Feb 11 09:45 Downloads
-rw-r--r--  1  group  1234 Feb 11 08:15 file.txt

9. Listing Only Directories (ls -d */)

The ls -d */ command is used to list only the directories within the current directory, without showing their contents.

ls -d */

Output

Documents/  Downloads/  Music/  Pictures/  Videos/

10. List files with combined options to view more details (ls -lhS)

The ls -lhS command in Linux is used to list directory contents with specific formatting and sorting options.

ls -lhS

Output

-rw-r--r--  1 user user  2.5G Feb 11 10:30 large_video.mp4
-rw-r--r--  1 user user  850M Feb 11 09:45 archive.zip
-rw-r--r--  1 user user  15M  Feb 11 08:15 document.pdf
-rw-r--r--  1 user user  4.2K Feb 11 07:00 notes.txt
drwxr-xr-x  2 user user  4.0K Feb 11 06:30 Documents

11. Customize colorized output (ls –color)

The ls –color command in Linux is used to enable or customize colorized output for the ls command.

ls --color=auto

Output

Documents/  Downloads/  script.sh*  file.txt

12. List all files in the current directory (ls *.txt)

The command ls *.txt is used in Linux to list all files in the current directory that end with the .txt extension.

ls *.txt

Output

notes.txt  report.txt  todo.txt

13. Display the disk usage ( du -sh */)

The du -sh */ command in Linux is used to display the disk usage of directories in a human-readable format.

du -sh */

Output

1.2G  Documents/
850M  Downloads/
15M   Music/
4.0K  Pictures/

Examples with Combined Use Cases

1. List files with the permissions and timestamps ( ls -alt )

The ls -alt command in Linux is used to list directory contents with specific options for formatting and sorting.

ls -alt

Output

-rw-r--r--  1 user user  1.2K Feb 11 11:00 .bash_history
drwxr-xr-x  2 user user  4.0K Feb 11 10:30 Documents
-rw-r--r--  1 user user  850M Feb 11 09:45 archive.zip
drwxr-xr-x  3 user user  4.0K Feb 11 09:00 .config
-rw-r--r--  1 user user  4.2K Feb 11 07:00 notes.txt

2. List files with inodes and human-readable sizes. (ls -lih)

The ls -lih command in Linux combines several options to provide a detailed listing of files and directories with specific formatting.

ls -lih

Output

1234567 drwxr-xr-x  2 user user 4.0K Feb 11 10:30 Documents
1234568 drwxr-xr-x  3 user user 4.0K Feb 11 09:45 Downloads
1234569 -rw-r--r--  1 user user 1.2K Feb 11 08:15 notes.txt

3. Filter and display specific files (ls -l | grep ‘filename’)

The command ls -l | grep ‘filename’ in Linux combines the ls command with grep to filter and display only the lines from the ls -l output that contain the word ‘filename’.

ls -l | grep 'filename'

Output

-rw-r--r--  1 user user  1.2K Feb 11 08:15 filename.txt

4. List directories in the current directory that match the pattern. ( ls -d */ | grep ‘pattern’ )

The command ls -d */ | grep ‘pattern’ in Linux is used to list only directories in the current directory that match a specific pattern.

ls -d */ | grep 'pattern

Output

project-folder/
test-folder/

5. Count the number of files ( ls | wc -l)

The command ls | wc -l in Linux is used to count the number of files or directories in the current directory.

ls | wc -l

Output

3

6. List directory contents in a long format (ls -lS)

The command ls -lS in Linux is used to list directory contents in a long format sorted by file size.

ls -lS

Output

-rw-r--r--  1 user user  2.5G Feb 11 10:30 large_video.mp4
-rw-r--r--  1 user user  850M Feb 11 09:45 archive.zip
-rw-r--r--  1 user user  15M  Feb 11 08:15 document.pdf
-rw-r--r--  1 user user  4.2K Feb 11 07:00 notes.txt
drwxr-xr-x  2 user user  4.0K Feb 11 06:30 Documents

7. List only the files (not directories) ( ls -p | grep -v / )

The command ls -p | grep -v / in Linux combines ls with grep to list only the files (not directories) in the current directory.

ls -p | grep -v /

Output

document.pdf
notes.txt
script.sh
image.jpg

8. Files modified within the last 7 days ( find . -type f -mtime -7 | xargs ls -l )

The command find . -type f -mtime -7 | xargs ls -l in Linux is used to find all files modified within the last 7 days in the current directory and its subdirectories, then list them in long format.

find . -type f -mtime -7 | xargs ls -l

Output

-rw-r--r--  1 user user  2.5M Feb 11 10:30 report.pdf
-rw-r--r--  1 user user  850K Feb 10 18:45 log.txt
-rwxr-xr-x  1 user user  4.2K Feb 09 12:15 script.sh

By using the ls command in Linux, you’ve managed to view directory contents, navigate through file systems, and organize files and directories. For further details on available options, you can check out the manual by typing man ls.

March 20, 2025