Download as pdf or txt
Download as pdf or txt
You are on page 1of 9

Essential ls Commands.

md 2024-01-16

Essential ls Command Examples in Linux


ls Command Options in Linux
The ls command takes the following syntax:

ls [ options ] /path/to/directory

The options section represents the command-line arguments that can be passed to manipulate the output of
the command.

In this tutorial, we will cover the following ls command arguments.

Options Description

ls -m Lists directory contents separated by a comma.

ls -Q Displays directory contents enclosed by quotation marks.

ls -l Displays files in a long-list format.

ls -lh Display file size in a human-readable format.

ls -g Omits group ownership column.

ls -F Adds a forward slash to directories.

ls -i Display inode number of files and directories.

ls -a Display all files including hidden files.

ls * Filters files according to the file extension.

ls -la Displays all files and directories in long list format.

ls -R Display files and directories recursively.

ls -r Sort Files in reverse.

ls -X Sort files alphabetically by file extension.

ls -tl Display files according to file creation date and time.

ls -n List UIDs and GIDs.

1. List Files and Directories in Linux


Running ls command without passing any command-line options or arguments, the ls command simply lists
the directory contents in alphabetical order. Here we won’t be able to view details like file types, size, modified
date and time, permission and links, etc.

1/9
Essential ls Commands.md 2024-01-16

ls

2. Long Listing of Files in Linux


The -l command option lets you print out detailed information about the directory contents in a columnar
format that includes size, modified date and time, file or directory name and owner of the file, and its
permission.

ls -l

total 36
drwxr-xr-x 2 linuxlab linuxlab 4096 Nov 26 15:53 Desktop
drwxr-xr-x 3 linuxlab linuxlab 4096 Dec 17 13:08 Documents
drwxr-xr-x 2 linuxlab linuxlab 4096 Nov 26 15:53 Downloads
drwxr-xr-x 2 linuxlab linuxlab 4096 Nov 26 15:53 Music
drwxr-xr-x 2 linuxlab linuxlab 4096 Nov 26 17:55 Pictures
drwxr-xr-x 2 linuxlab linuxlab 4096 Nov 26 15:53 Public
drwxr-xr-x 2 linuxlab linuxlab 4096 Nov 26 15:53 Templates
drwxr-xr-x 2 linuxlab linuxlab 4096 Nov 26 15:53 Videos
drwxrwxr-x 2 linuxlab linuxlab 4096 Nov 26 15:53 Warpinator

Starting from the far left, we have:

1. 1st column – File/directory permissions.


2. 2nd column – Number of links.
3. 3rd column – Name of the owner.
4. 4th column – Name of the group that the file belongs to.
5. 5th column – File size in bytes.
6. 6th column to 8th column – Last modification date.
7. 9th column – File / Directory name.

3. List Hidden Files and Directories


Hidden files are special files that store user settings and configuration files, which are used by running
programs and services for reading and storing information.

For example, the .bashrc file is a script that contains user settings and configurations of the currently
logged-in user, which include command aliases, shell history, the coloring of the terminal font, etc.

The .bash_logout file is executed when you log out of your bash sessions. It’s mainly used for cleanup
purposes i.e. carrying out any operations that need to be performed once you exit the bash shell.

To list hidden files, pass the -a option as shown, which displays both hidden files and directories.

ls -a

. Pictures
2/9
Essential ls Commands.md 2024-01-16

.. .profile
.bash_history Public
.bash_logout .sudo_as_admin_successful
.bashrc Templates
.cache Desktop
.config .dmrc
Documents Downloads
.face .gnupg
.gtkrc-2.0 .gtkrc-xfce
.ICEauthority Music
.librewolf .linuxmint
.local .Xauthority

4. List All Files in Linux


As you have noticed the -a option not only lists hidden files but all the files and directories. For better
viewing, you can use the -la option.

ls -la

total 464
drwxr-x--- 17 linuxlab linuxlab 4096 Jan 16 13:15 .
drwxr-xr-x 3 root root 4096 Nov 26 09:10 ..
-rw------- 1 linuxlab linuxlab 5349 Jan 16 14:00 .bash_history
-rw-r--r-- 1 linuxlab linuxlab 220 Nov 26 09:10 .bash_logout
-rw-r--r-- 1 linuxlab linuxlab 6284 Dec 17 13:11 .bashrc
drwx------ 15 linuxlab linuxlab 4096 Jan 16 14:00 .cache
drwxr-xr-x 17 linuxlab linuxlab 4096 Nov 26 17:53 .config
drwxr-xr-x 2 linuxlab linuxlab 4096 Nov 26 15:53 Desktop
-rw-r--r-- 1 linuxlab linuxlab 23 Nov 26 15:53 .dmrc
drwxr-xr-x 3 linuxlab linuxlab 4096 Dec 17 13:08 Documents
drwxr-xr-x 2 linuxlab linuxlab 4096 Nov 26 15:53 Downloads
-rw-rw-r-- 1 linuxlab linuxlab 284300 Nov 26 17:56 .face
drwx------ 3 linuxlab linuxlab 4096 Nov 26 15:53 .gnupg
-rw-r--r-- 1 linuxlab linuxlab 22 Nov 26 09:10 .gtkrc-2.0
-rw-r--r-- 1 linuxlab linuxlab 516 Nov 26 09:10 .gtkrc-xfce
-rw------- 1 linuxlab linuxlab 0 Nov 26 15:53 .ICEauthority
drwx------ 4 linuxlab linuxlab 4096 Nov 26 17:58 .librewolf
drwxrwxr-x 4 linuxlab linuxlab 4096 Nov 27 12:22 .linuxmint
drwxrwxr-x 4 linuxlab linuxlab 4096 Nov 26 16:25 .local
drwxr-xr-x 2 linuxlab linuxlab 4096 Nov 26 15:53 Music
drwxr-xr-x 2 linuxlab linuxlab 4096 Nov 26 17:55 Pictures
-rw-r--r-- 1 linuxlab linuxlab 807 Nov 26 09:10 .profile
drwxr-xr-x 2 linuxlab linuxlab 4096 Nov 26 15:53 Public
-rw-r--r-- 1 linuxlab linuxlab 0 Nov 26 15:57 .sudo_as_admin_successful
drwxr-xr-x 2 linuxlab linuxlab 4096 Nov 26 15:53 Templates

5. Display File Size in a Human-readable Format

3/9
Essential ls Commands.md 2024-01-16

To present the output in a better format, add the -h flag to print the file size in a human-readable format.
From the output, the file size is displayed in Kilobytes, Megabytes, and Gigabytes. By all means, this looks
more presentable.

ls -lh

total 36K
drwxr-xr-x 2 linuxlab linuxlab 4.0K Nov 26 15:53 Desktop
drwxr-xr-x 3 linuxlab linuxlab 4.0K Dec 17 13:08 Documents
drwxr-xr-x 2 linuxlab linuxlab 4.0K Nov 26 15:53 Downloads
drwxr-xr-x 2 linuxlab linuxlab 4.0K Nov 26 15:53 Music
drwxr-xr-x 2 linuxlab linuxlab 4.0K Nov 26 17:55 Pictures
drwxr-xr-x 2 linuxlab linuxlab 4.0K Nov 26 15:53 Public
drwxr-xr-x 2 linuxlab linuxlab 4.0K Nov 26 15:53 Templates
drwxr-xr-x 2 linuxlab linuxlab 4.0K Nov 26 15:53 Videos
drwxrwxr-x 2 linuxlab linuxlab 4.0K Nov 26 15:53 Warpinator

6. Distinguish Directories and Files in Linux


When running the ls command, it’s not always easy to make a clear distinction between files and directories.
The -F option adds a forward slash (/) to directories, making it easier for them to stand out from the rest of
the files.

ls -F

Desktop/ Downloads/ Pictures/ Templates/ testfile2 Videos/


Documents/ Music/ Public/ testfile1 testfile3 Warpinator/

7. Sorting Files in Reverse Order


By default, the ls command sorts files and directories alphabetically (From A – Z). You can opt to sort the
directory contents in reverse order using the -r option.

ls -lr

total 36
drwxrwxr-x 2 linuxlab linuxlab 4096 Nov 26 15:53 Warpinator
drwxr-xr-x 2 linuxlab linuxlab 4096 Nov 26 15:53 Videos
-rw-rw-r-- 1 linuxlab linuxlab 0 Jan 16 14:03 testfile3
-rw-rw-r-- 1 linuxlab linuxlab 0 Jan 16 14:03 testfile2
-rw-rw-r-- 1 linuxlab linuxlab 0 Jan 16 14:03 testfile1
drwxr-xr-x 2 linuxlab linuxlab 4096 Nov 26 15:53 Templates
drwxr-xr-x 2 linuxlab linuxlab 4096 Nov 26 15:53 Public
drwxr-xr-x 2 linuxlab linuxlab 4096 Nov 26 17:55 Pictures
drwxr-xr-x 2 linuxlab linuxlab 4096 Nov 26 15:53 Music
drwxr-xr-x 2 linuxlab linuxlab 4096 Nov 26 15:53 Downloads

4/9
Essential ls Commands.md 2024-01-16

drwxr-xr-x 3 linuxlab linuxlab 4096 Dec 17 13:08 Documents


drwxr-xr-x 2 linuxlab linuxlab 4096 Nov 26 15:53 Desktop

In addition, you can sort the file extensions alphabetically using the -X flag.

ls -X

Desktop Documents Downloads Music Pictures Public Templates


testfile1 testfile2 testfile3 Videos Warpinator

8. List Files Recursively in Linux


The -R flag lists files recursively. First, the command lists all the files and directories in your current directory,
then proceeds to display files contained in individual directories and subdirectories.

ls -R

.:
Desktop Downloads Pictures Templates testfile2 Videos
Documents Music Public testfile1 testfile3 Warpinator

./Desktop:

./Documents:
'Python Workfiles'

'./Documents/Python Workfiles':
venv

'./Documents/Python Workfiles/venv':
bin include lib lib64 pyvenv.cfg
(...)

In the following example, the files in individual directories have been listed as well.

9. Sort Files By Modification Time in Linux


The ls -ltr command shows the files in the long listing format in reverse sorted by modification time, which
means it will display detailed information about each file or directory in reverse order based on their last
modified date/time stamp.

ls -ltr

total 36
drwxr-xr-x 2 linuxlab linuxlab 4096 Nov 26 15:53 Videos
drwxr-xr-x 2 linuxlab linuxlab 4096 Nov 26 15:53 Templates

5/9
Essential ls Commands.md 2024-01-16

drwxr-xr-x 2 linuxlab linuxlab 4096 Nov 26 15:53 Public


drwxr-xr-x 2 linuxlab linuxlab 4096 Nov 26 15:53 Music
drwxr-xr-x 2 linuxlab linuxlab 4096 Nov 26 15:53 Downloads
drwxr-xr-x 2 linuxlab linuxlab 4096 Nov 26 15:53 Desktop
drwxrwxr-x 2 linuxlab linuxlab 4096 Nov 26 15:53 Warpinator
drwxr-xr-x 2 linuxlab linuxlab 4096 Nov 26 17:55 Pictures
drwxr-xr-x 3 linuxlab linuxlab 4096 Dec 17 13:08 Documents
-rw-rw-r-- 1 linuxlab linuxlab 0 Jan 16 14:03 testfile3
-rw-rw-r-- 1 linuxlab linuxlab 0 Jan 16 14:03 testfile2
-rw-rw-r-- 1 linuxlab linuxlab 0 Jan 16 14:03 testfile1

10. Sort Files By Newest to Oldest in Linux


You can sort files by time and date using the -t option, which sorts the files in order starting from the newest
to the oldest.

ls -tl

-rw-rw-r-- 1 linuxlab linuxlab 0 Jan 16 14:03 testfile1


-rw-rw-r-- 1 linuxlab linuxlab 0 Jan 16 14:03 testfile2
-rw-rw-r-- 1 linuxlab linuxlab 0 Jan 16 14:03 testfile3
drwxr-xr-x 3 linuxlab linuxlab 4096 Dec 17 13:08 Documents
drwxr-xr-x 2 linuxlab linuxlab 4096 Nov 26 17:55 Pictures
drwxrwxr-x 2 linuxlab linuxlab 4096 Nov 26 15:53 Warpinator
drwxr-xr-x 2 linuxlab linuxlab 4096 Nov 26 15:53 Desktop
drwxr-xr-x 2 linuxlab linuxlab 4096 Nov 26 15:53 Downloads
drwxr-xr-x 2 linuxlab linuxlab 4096 Nov 26 15:53 Music
drwxr-xr-x 2 linuxlab linuxlab 4096 Nov 26 15:53 Public
drwxr-xr-x 2 linuxlab linuxlab 4096 Nov 26 15:53 Templates
drwxr-xr-x 2 linuxlab linuxlab 4096 Nov 26 15:53 Videos

11. Sort Files by File Size in Linux

ls -lS

total 36
drwxr-xr-x 2 linuxlab linuxlab 4096 Nov 26 15:53 Desktop
drwxr-xr-x 3 linuxlab linuxlab 4096 Dec 17 13:08 Documents
drwxr-xr-x 2 linuxlab linuxlab 4096 Nov 26 15:53 Downloads
drwxr-xr-x 2 linuxlab linuxlab 4096 Nov 26 15:53 Music
drwxr-xr-x 2 linuxlab linuxlab 4096 Nov 26 17:55 Pictures
drwxr-xr-x 2 linuxlab linuxlab 4096 Nov 26 15:53 Public
drwxr-xr-x 2 linuxlab linuxlab 4096 Nov 26 15:53 Templates
drwxr-xr-x 2 linuxlab linuxlab 4096 Nov 26 15:53 Videos
drwxrwxr-x 2 linuxlab linuxlab 4096 Nov 26 15:53 Warpinator
-rw-rw-r-- 1 linuxlab linuxlab 0 Jan 16 14:03 testfile1
-rw-rw-r-- 1 linuxlab linuxlab 0 Jan 16 14:03 testfile2
-rw-rw-r-- 1 linuxlab linuxlab 0 Jan 16 14:03 testfile3

6/9
Essential ls Commands.md 2024-01-16

12. List File Inode Number in Linux


You can display the files and directories’ inode numbers using the -i option as shown.

ls -i

1106004 Desktop 1106009 Music 1106006 Templates 1078678 testfile3


1106008 Documents 1106010 Pictures 1078676 testfile1 1106011 Videos
1106005 Downloads 1106007 Public 1078677 testfile2 1106030 Warpinator

13. List Files and Directories Separated by Commas


The -m flag lists the directory contents one after the other separated by a comma.

ls -m

Desktop, Documents, Downloads, Music, Pictures, Public, Templates, testfile1,


testfile2, testfile3, Videos, Warpinator

With the -Q flag, all the directory contents are enclosed by double quotation marks as shown.

ls -Q

"Desktop" "Downloads" "Pictures" "Templates" "testfile2" "Videos"


"Documents" "Music" "Public" "testfile1" "testfile3" "Warpinator"

14. Omit Group Ownership in a Long-List Format


When used with the -l command option, the ls command prints both user and group ownership of the file.
You can opt to omit the group column by passing the -g option.

ls -g

total 36
drwxr-xr-x 2 linuxlab 4096 Nov 26 15:53 Desktop
drwxr-xr-x 3 linuxlab 4096 Dec 17 13:08 Documents
drwxr-xr-x 2 linuxlab 4096 Nov 26 15:53 Downloads
drwxr-xr-x 2 linuxlab 4096 Nov 26 15:53 Music
drwxr-xr-x 2 linuxlab 4096 Nov 26 17:55 Pictures
drwxr-xr-x 2 linuxlab 4096 Nov 26 15:53 Public
drwxr-xr-x 2 linuxlab 4096 Nov 26 15:53 Templates
-rw-rw-r-- 1 linuxlab 0 Jan 16 14:03 testfile1
-rw-rw-r-- 1 linuxlab 0 Jan 16 14:03 testfile2
-rw-rw-r-- 1 linuxlab 0 Jan 16 14:03 testfile3

7/9
Essential ls Commands.md 2024-01-16

drwxr-xr-x 2 linuxlab 4096 Nov 26 15:53 Videos


drwxrwxr-x 2 linuxlab 4096 Nov 26 15:53 Warpinator

15. List Specific File Types or Extensions


For example, to display all files with a .jpg extension, run the command:

ls *.jpg

21.jpg mi.jpg osx.jpg linuxwall.jpg

Similarly, to list all PDF files, run the command:

ls *.pdf

Breaking Things With Linux.pdf

16. List the UID and GID of Files


To display the UID and GID of files and directories, use the -n option as shown.

ls -n

total 36
drwxr-xr-x 2 1000 1000 4096 Nov 26 15:53 Desktop
drwxr-xr-x 3 1000 1000 4096 Dec 17 13:08 Documents
drwxr-xr-x 2 1000 1000 4096 Nov 26 15:53 Downloads
drwxr-xr-x 2 1000 1000 4096 Nov 26 15:53 Music
drwxr-xr-x 2 1000 1000 4096 Nov 26 17:55 Pictures
drwxr-xr-x 2 1000 1000 4096 Nov 26 15:53 Public
drwxr-xr-x 2 1000 1000 4096 Nov 26 15:53 Templates
-rw-rw-r-- 1 1000 1000 0 Jan 16 14:03 testfile1
-rw-rw-r-- 1 1000 1000 0 Jan 16 14:03 testfile2
-rw-rw-r-- 1 1000 1000 0 Jan 16 14:03 testfile3
drwxr-xr-x 2 1000 1000 4096 Nov 26 15:53 Videos
drwxrwxr-x 2 1000 1000 4096 Nov 26 15:53 Warpinator

17. Check ls Command Version


If you are a little curious and want to check the version of the ls command, you can do so as follows:

ls --version

8/9
Essential ls Commands.md 2024-01-16

From the output, you can see that we are running ls version 9.1.

18. Show ls Command Help Page


The ls program provides a wealth of command-line options. What we have covered are just some of the
commonly used ones. For a comprehensive list of all the command options, run the following command:

ls --help

Optionally, you can visit the man pages by running:

man ls

19. List Directory Information in Linux


With the ls -l command list files under directory /tmp. Wherein with -ld options, it will display information
of the /tmp directory.

ls -l /tmp
ls -ld /tmp/

20. Create ls Command Alias


We have made an alias for the ls command when we execute the ls command it will take the -l option by
default and display a long listing as mentioned earlier.

alias ls="ls -l"

To view the number of aliases available in your system, use the below alias command and the same can be
unaliased as shown below example.

alias

To remove an alias previously defined, just use the unalias command.

unalias ls

9/9

You might also like