in

LinuxLinux

ls Command: How To List Files in Linux Using ls Command

Different ways to list files in Linux!

ls Command

L

ist Files in Linux: ls command is one of the basic command in Linux. It is mainly used to list the files which created inside any directories in Linux. In this tutorial, we explained the multiple ways to list the files using ls command. Lets get into the article.

Jump To:

ls Command Basic use in Linux:

The basic syntax of the ls command is,

syntax
ls {OPTIONS} {FILES/DIRECTORIES}

To list all the files and directories which is currently working using ls command,

$ls

The following output will appear on your terminal.

ls command output
ls command output

The image shows that the ls command prints the current working directories list in the Terminal. The output of the list may differ and depends on the user usage in Linux.

If you see the above image closely, The output list is in the alphabetical order. By default, ls command will list the files and directories in alphabetical order.

To print the files in a specific directory:

It is possible to print the files in a specific directory using the ls command, Here we used ls command to print the files in the /etc directory using ls command.

$ls /etc

The output of the following command is,

ls /etc command output
ls /etc command output

The above image shows the usage to ls command to print the files in a specific directory. The output files are listed in the alphabetical order.

Check this too  Change & Reset User Password In Linux!

Print Files of Multiple Directories Using ls Command:

It is possible to print the files in multiple directories using ls command, It is similar to the above step additionally you need to add another directory with space in the same command. Use the following command to print files in multiple directories.

$ls /home /var
Listing Multiple directories files
Listing Multiple directories files

The above image shows the list of files in the multiple directories has been listed using ls command.

ls Command Won’t Allow To Print Root Files:

It is not possible to print the files in the root directories using the ls command. If you want to try to print the files of root directory using ls command, use the following command:

$ls /root

The output of the following command is,

list root files
list root files

If you try to print the root directory files using the ls command and then warning message will appear on your Terminal as shown in the above image. “Permission Denied“.

Show Long listing files format using ls command:

By default, ls command will print the files alone. It is possible to print the files with its format and all details about the file. To do this you need to add the -l along with ls command to print the file with complete details. If you use the -l with ls command the following lists of details will be printed.

  • The file type {Directory or Regular File}
  • The file permissions
  • Number of hard links to the file
  • File owner {e.g root}
  • Filegroup
  • File size
  • Date and Time {Created Time}
  • Filename
Check this too  Basic Linux Commands & Codes for Beginners[Ubuntu & Mint & More]

Let us consider, to print the files in the directory /etc/hosts using ls command with -l,

$ls -l /etc/hosts
Output
-rw-r--r-- 1 root root 231 Apr  6 18:15 /etc/hosts

As you see from the above output, -l with ls command print the complete details of the files which are present inside the /etc/hosts directory. The output begins with  ““, which means the listed one is file. The output options are explained below. rw-r--r-- is the read and write permission of the file. The time & date mentioned in the output is last modification date.

Output
- - Regular file
b - Block special file
c - Character special file
d - Directory
l - Symbolic link
n - Network file
p - FIFO {First in First out}
s - Socket

To print the Directory in Long Listed Format:

It is possible to print the directory in Long Listed format using the ls command along with -l,

$ls -l /home

The output of the above command is,

Output
total 4
drwxr-xr-x 27 tecrobust tecrobust 4096 Jul 23 15:11 tecrobust

The output begins with “d“, which means it is directory.

To Show Hidden files using ls command:

It is possible to show the hidden files using ls command. By default, ls command will not print the hidden files. To print the hidden files, you need to use the (a) command along with ls command. The hidden files will begin with (.).

$ls -la /home

The output of the above command is,

Output
total 12
drwxr-xr-x  3 root      root      4096 Apr  6 18:16 .
drwxr-xr-x 20 root      root      4096 Jun 29 06:04 ..
drwxr-xr-x 27 tecrobust tecrobust 4096 Jul 23 15:11 tecrobust

In the same way, you can use the ls command to print all the hidden files,

$ls -la ~/

The output of the above command is,

Check this too  How to Delete Line in Vim Editor on Linux Using Terminal Command!
list all hidden files
list all hidden files

Sorting ls command list output:

By default, ls command will sort the output in alphabetical order. You can customize the output list in specific sort.

Commands
(-X) - sort alphabetically 
(-S) - sort by file size of the file
(-t) - sort by modification time of the file
(-v) - sort by version of the file
(-r) - reverse sorting the list

Sample command to sort the output files using ls command is,

$ls -lt /var
The output of the above command is,

output
output

ls command to list Subdirectories repeatedly:

It is possible to display the files inside the directories & subdirectories using -R along with ls command. Use the following command,

$ls -R

The above command will print the files inside the directories and subdirectories in Linux.

Verdict:

That’s it. This is the simple and important ways to use the ls command to print the files in Linux. If you have any queries and then feel free to comment us below.

What do you think?

points
Upvote Downvote
Article Review
  • Article Explanation
  • Commands Provided
  • Alternate Commands
4.8

Summary

For more article, Kindly visit our HomePage

One Ping

  1. Pingback:

Leave a Reply

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

Written by Julian

Hello Folks! I'm Julian, Promoted to the Content writer for the category of Commands & Web Apps in Tec Robust. Enjoy my content & Learn more Linux Commands!

Contact me:
tecrobust [@] gmail.com
Sub: Message to Julian

Eclipse Ubuntu: How To Install Eclipse for Ubuntu 18.04 LTS and 19.04

Add Two Numbers Using Shell Script

Add Two Numbers in Shell Script Bash Linux!