Linux ls Command Tutorial To List Files and Directories – POFTUT

Linux ls Command Tutorial To List Files and Directories


ls is base command provided by all Linux distributions. We can use ls command to list files, folders or directories. In this tutorial we will look different use cases about ls.

List Files and Directories

We will start with a simple example. We will use ls command without an option. This will list regular files and directories of the current working path.

$ ls
List Files and Directories
List Files and Directories

List Detailed Information

While listing files and directories we may have need more detailed information. We can list information like

  • User, group, other read, write,execute permissions
  • User and group owner information
  • Size
  • Last edit time

We will use -l option to list detailed information

List Detailed Information
List Detailed Information

List Relative Path

There is two type of path definition. Relative path means according to current working path. For example if I want to list “current working path+Downloads” I can use only Downloads like below.

$ ls Downloads

List Relative Path
List Relative Path

List Absolute Path

Absolute path definition means we will provide the whole directory structure from the / root. In this example we will list /home/ismail/Downloads .

$ ls /home/ismail/Downloads/
List Absolute Path
List Absolute Path

List Parent Directory

ls command and bash provides some shortcuts about specifying the parent directory. We can use .. to specify parent directory and list files and folders in the parent directory.

$ ls ..
List Parent Directory
List Parent Directory

List As Colored

If there is no specific configuration the ls command output will be black and white which is boring for the most of the users. We can list files and directories in a meaningful color with --color option.

$ ls --color
List As Colored
List As Colored

List Only Directories

We have the ability to list only directories. We can use -d option for this.

$ ls -d

List All Files or Hidden Files

By default ls command will list only files, directories and links. But there is a special file type named hidden file. Hidden files generally used to store configuration or temporary data or cache. We can list these files with -a option which means all.

$ ls -a
List All Files or Hidden Files
List All Files or Hidden Files

Sort By Date and Time

Files and directories created, changed during time. These date and time information is stored as attribute. We can list file and directories according to data time with the -t option like below.

$ ls -t -l
Sort By Date and Time
Sort By Date and Time

Sort By Size

Another sort parameter is the size. We can sort files and folders according to their size. But keep in mind that all directories  will be the same size which is 4096.

$ ls -s

List Recursively

We can also list directories contents recursively. Recursively means looking child directories and files of all given path for multilevel. We will us -R option for this.

$ ls -R
List Recursively
List Recursively

List Specific File Extension

ls command also accept file and directory names to filter. We generally use * glob as file name or the dynamic side of the file. In this example we list files ends with tar.gz .

$ ls *.tar.gz

LEARN MORE  How To Navigate, List Files and Directories In Windows Command Line With Dir?

Leave a Comment