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 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 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 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 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 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 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

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 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 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