30 linux commands with examples

Are you tired of often typing “dir” in Linux terminal only to remember the right command is “ls“?. In this comprehensive guide, we cover in detail the usage of ls command which in your gateway to interacting with with files and directories in a Linux system. We will use real-world examples that will empower you to:

  • List, sort and filter files and directories more efficiently.
  • Navigate Linux complex directory structures without struggle.
  • Gain better insights into Linux file ownership and permissions.
  • Elevate your skills to a Linux power user.

The ls command is used to list the contents of directories in Linux. There are several options used to customize the output from ls. Here are 30 most useful ls commands for beginners and seasoned Linux pro uses.

1. Basic ls Command

$ ls
archive1.tar.gz  archive3.tar.gz  archive5.tar.gz  dir2  file1.txt  file3.txt  file5.txt   image2.jpg  image4.jpg  script1.sh  script3.sh  script5.sh
archive2.tar.gz  archive4.tar.gz  dir1             dir3  file2.txt  file4.txt  image1.jpg  image3.jpg  image5.jpg  script2.sh  script4.sh

Lists files and directories in the current directory.

2. List with Detailed Information -l

If you want to display detailed information about the files including permissions, owner, group, number of links, size, and modification date use -l option.

$ ls -l
total 92
-rw-r--r-- 1 root root  565 Jul 20 19:26 archive1.tar.gz
-rw-r--r-- 1 root root  336 Jul 20 19:26 archive2.tar.gz
-rw-r--r-- 1 root root  669 Jul 20 19:26 archive3.tar.gz
-rw-r--r-- 1 root root   80 Jul 20 19:26 archive4.tar.gz
-rw-r--r-- 1 root root  457 Jul 20 19:26 archive5.tar.gz
drwxr-xr-x 5 root root 4096 Jul 20 19:26 dir1
drwxr-xr-x 5 root root 4096 Jul 20 19:26 dir2
drwxr-xr-x 5 root root 4096 Jul 20 19:26 dir3
-rw-r--r-- 1 root root  562 Jul 20 19:26 file1.txt
-rw-r--r-- 1 root root  280 Jul 20 19:26 file2.txt
-rw-r--r-- 1 root root  809 Jul 20 19:26 file3.txt
-rw-r--r-- 1 root root  479 Jul 20 19:26 file4.txt
-rw-r--r-- 1 root root  169 Jul 20 19:26 file5.txt
-rw-r--r-- 1 root root  626 Jul 20 19:26 image1.jpg
-rw-r--r-- 1 root root  461 Jul 20 19:26 image2.jpg
-rw-r--r-- 1 root root  461 Jul 20 19:26 image3.jpg
-rw-r--r-- 1 root root  841 Jul 20 19:26 image4.jpg
-rw-r--r-- 1 root root   33 Jul 20 19:26 image5.jpg
-rw-r--r-- 1 root root  835 Jul 20 19:26 script1.sh
-rw-r--r-- 1 root root  545 Jul 20 19:26 script2.sh
-rw-r--r-- 1 root root  409 Jul 20 19:26 script3.sh
-rw-r--r-- 1 root root  208 Jul 20 19:26 script4.sh
-rw-r--r-- 1 root root  943 Jul 20 19:26 script5.sh

3. List All Files (Including Hidden) -a

By using -a command option you will see hidden files (those starting with a dot) as seen in the following example.

$  ls  -a
.   archive1.tar.gz  archive3.tar.gz  archive5.tar.gz  dir2  file1.txt  file3.txt  file5.txt      .hidden2.conf  .hidden4.conf  image1.jpg  image3.jpg  image5.jpg  script2.sh  script4.sh
..  archive2.tar.gz  archive4.tar.gz  dir1             dir3  file2.txt  file4.txt  .hidden1.conf  .hidden3.conf  .hidden5.conf  image2.jpg  image4.jpg  script1.sh  script3.sh  script5.sh

4. List with Human-Readable Sizes -h

If you want to show the file sizes in human-readable format (e.g., KB, MB), then use -h command option.

$ ls -lh ~/goharbor-prepare.tar
-rw-r--r-- 1 root root 203M Jul 20 17:38 /root/goharbor-prepare.tar

5. List Files by Modification Time -t

Sorts the files by modification time, with the newest showing first by using -t

$ ls -lht ~/
total 835M
drwxr-xr-x 5 root root 4.0K Jul 20 19:27 linux-ls
drwxr-xr-x 3 root root 4.0K Jul 20 18:44 myapp
drwxr-xr-x 3 root root 4.0K Jul 20 18:10 myproject
drwxr-xr-x 2 root root 4.0K Jul 20 18:05 django-app
-rw-r--r-- 1 root root 203M Jul 20 17:38 goharbor-prepare.tar
drwxr-xr-x 3 root root 4.0K Jul 20 16:51 harbor
drwxr-xr-x 5 root root 4.0K Jul 20 00:02 quay
-rw-r--r-- 1 root root 629M Jun  6 09:39 harbor-offline-installer-v2.11.0.tgz
-rw-r--r-- 1 root root  12K Jun  6 09:39 harbor-online-installer-v2.11.0.tgz
-rw-r--r-- 1 root root 3.8M May 15 17:19 starship-x86_64-unknown-linux-gnu.tar.gz
-rw-r--r-- 1 root root   65 May 15 17:19 starship-x86_64-unknown-linux-gnu.tar.gz.sha256

6. Reverse Order

To reverse the display order of the list, use -r command option:

$ ls -lhr ~/
total 835M
-rw-r--r-- 1 root root   65 May 15 17:19 starship-x86_64-unknown-linux-gnu.tar.gz.sha256
-rw-r--r-- 1 root root 3.8M May 15 17:19 starship-x86_64-unknown-linux-gnu.tar.gz
drwxr-xr-x 5 root root 4.0K Jul 20 00:02 quay
drwxr-xr-x 3 root root 4.0K Jul 20 18:10 myproject
drwxr-xr-x 3 root root 4.0K Jul 20 18:44 myapp
drwxr-xr-x 5 root root 4.0K Jul 20 19:27 linux-ls
-rw-r--r-- 1 root root  12K Jun  6 09:39 harbor-online-installer-v2.11.0.tgz
-rw-r--r-- 1 root root 629M Jun  6 09:39 harbor-offline-installer-v2.11.0.tgz
drwxr-xr-x 3 root root 4.0K Jul 20 16:51 harbor
-rw-r--r-- 1 root root 203M Jul 20 17:38 goharbor-prepare.tar
drwxr-xr-x 2 root root 4.0K Jul 20 18:05 django-app

7. List Files Recursively

Lists all files and directories recursively.

$ ls -R ~/
/root/quay:
config  postgres  storage

/root/quay/config:
config.yaml

/root/quay/postgres:
userdata

8. List Only Directories

You can limit the output by only listing the directories in your current directory.

$ ls -d */
dir1/  dir2/  dir3/

9. Long Listing of Specific Directory

Show detailed information for files in a specified directory by running the following commands.

ls -l /path/to/directory

10. List Files with Wildcards

If you wish to list files matching a specific pattern (e.g., all .txt files), run:

$ ls *.txt
file1.txt  file2.txt  file3.txt  file4.txt  file5.txt

$ ls *.jpg
image1.jpg  image2.jpg  image3.jpg  image4.jpg  image5.jpg

11. List with File Type Indicators

Appends a character to each entry to indicate the file type (/ for directories, * for executables).

ls -F

12. List Files by Size

Sort files by size, with the largest first using the following commands.

ls -lS

13. Classify Files by Type

This works similar to -F, classifies files by appending indicators.

ls --classify

14. List Directory Tree

Get a tree view of the directory structure using the following commands.

ls -R | grep ":$"

15. List with Inode Numbers

Get the inode number count for each file by using -i option.

ls -i

16. List Files in Columns

Lists entries by column using -C commands.

ls -C

17. List Files with Access Time

To sort and display files by the by last access time use -u

$ ls -ltu
total 92
drwxr-xr-x 5 root root 4096 Jul 21 22:15 dir3
drwxr-xr-x 5 root root 4096 Jul 21 22:15 dir2
drwxr-xr-x 5 root root 4096 Jul 21 22:15 dir1
-rw-r--r-- 1 root root  457 Jul 20 19:26 archive5.tar.gz
-rw-r--r-- 1 root root   33 Jul 20 19:26 image5.jpg
-rw-r--r-- 1 root root  943 Jul 20 19:26 script5.sh
-rw-r--r-- 1 root root  169 Jul 20 19:26 file5.txt
-rw-r--r-- 1 root root   80 Jul 20 19:26 archive4.tar.gz
...

18. List Files with Change Time

Sort the output by and displays the last change time of file status information.

ls -lc

19. List with Colors

Use the --color command option to enable colorized output for better readability.

ls --color

20. List Only Names

Pass -1 to the ls command to list one file per line.

ls -1

21. List Files in Reverse by Size

You can also sort files by size, you will see the smallest first.

$  ls -lhSr ~/
total 835M
-rw-r--r-- 1 root root   65 May 15 17:19 starship-x86_64-unknown-linux-gnu.tar.gz.sha256
drwxr-xr-x 5 root root 4.0K Jul 20 00:02 quay
drwxr-xr-x 3 root root 4.0K Jul 20 18:10 myproject
drwxr-xr-x 3 root root 4.0K Jul 20 18:44 myapp
drwxr-xr-x 5 root root 4.0K Jul 20 19:27 linux-ls
drwxr-xr-x 3 root root 4.0K Jul 20 16:51 harbor
drwxr-xr-x 2 root root 4.0K Jul 20 18:05 django-app
-rw-r--r-- 1 root root  12K Jun  6 09:39 harbor-online-installer-v2.11.0.tgz
-rw-r--r-- 1 root root 3.8M May 15 17:19 starship-x86_64-unknown-linux-gnu.tar.gz
-rw-r--r-- 1 root root 203M Jul 20 17:38 goharbor-prepare.tar
-rw-r--r-- 1 root root 629M Jun  6 09:39 harbor-offline-installer-v2.11.0.tgz

22. List Files in Reverse by Time

Let’s sort the files by modification time, and get the oldest as the first one.

ls -ltr

23. List Files by Extension

Sort alphabetically by file extension

ls -X

24. List Files with Context

See the security context for each file using --context

ls --context

25. List with Quoting Styles

Get the output with quotes in the names of files.

ls -Q

26. List Files in Human-Readable Format with SI Units

List using the SI units for file sizes (e.g., 1K = 1000).

ls -lh --si

27. List Files in Reverse by Extension

If you wish to sort files by extension, in reverse order, run the commands below.

ls -Xr

28. List Files and Show Only Directory Names

Displays only the directories in the current directory.

$ ls -d */
dir1/  dir2/  dir3/

29. List Files in Reverse Alphabetical Order

To reverse the order of the list, showing files in reverse alphabetical order, use -r command option:

ls -r

30. Combine Multiple Options

You can easily combine multiple options: example is to show all files, in long format, with human-readable sizes, and classifies file types.

ls -alhF

Below is a table summarizing the 30 ls command usage examples:

CommandDescription
lsLists files and directories in the current directory.
ls -lDisplays detailed information including permissions, number of links, owner, group, size, and modification date.
ls -aIncludes hidden files (those starting with a dot).
ls -lhShows file sizes in human-readable format (e.g., KB, MB).
ls -ltSorts by modification time, newest first.
ls -lrReverses the order of the list.
ls -RLists all files and directories recursively.
ls -d */Lists only directories in the current directory.
ls -l /path/to/directoryShows detailed information for files in a specified directory.
ls *.txtLists files matching a pattern (e.g., all .txt files).
ls -FAppends a character to each entry to indicate the file type (/ for directories, * for executables).
ls -lSSorts files by size, largest first.
ls --classifySame as -F, classifies files by appending indicators.
`ls -Rgrep “:$”`
ls -iDisplays the inode number for each file.
ls -CLists files in columns.
ls -luSorts by and displays the last access time.
ls -lcSorts by and displays the last change time of file status information.
ls --colorEnables colorized output for better readability.
ls -1Lists one file per line.
ls -lSrSorts files by size, smallest first.
ls -ltrSorts files by modification time, oldest first.
ls -XSorts files by extension.
ls --contextDisplays security context for each file.
ls -QQuotes the names of files.
ls -lh --siUses SI units for file sizes (e.g., 1K = 1000).
ls -XrSorts files by extension, in reverse order.
ls -d */Displays only the directories in the current directory.
ls -rReverses the order of the list, showing files in reverse alphabetical order.
ls -alhFCombines multiple options: shows all files, in long format, with human-readable sizes, and classifies file types.

Conclusion

In this article we’ve provided a comprehensive usage of the ls command with examples. By mastering the common lscommand options, you will start to navigate and manipulate the Linux filesystem easily.

LEAVE A REPLY

Please enter your comment!
Please enter your name here