The `ls` Command in Linux
The ls
command is one of the most frequently used command in Linux. It stands
for 'list', and as the name suggests, it's used to list the contents of a
directory. This tutorial will walk you through the basic usage of ls
command
as well as some of its powerful options.
Basic ls
Command
The simplest form of ls
command is to run it without any options. Open your
terminal and type:
ls --options arguments
This command will list the contents of the current directory.
ls
Command Options Table
Option | Long Option | Description |
---|---|---|
-a | --all | Do not ignore entries starting with . |
-A | --almost-all | Do not list implied [Current Directory] and .. [Parent Directory] |
-l | --format=long | Use a long listing format to display the information |
-h | --human-readable | Display file size in human readable format (KB, MB, etc.) |
-t | --sort=time | Sort by modification time, newest first |
-r | --reverse | Reverse order while sorting |
-R | --recursive | List subdirectories recursively |
-p | --indicator-style=slash | Append / indicator to directories |
-F | --classify | Append indicator (one of */=>@ ) to entries |
-S | --sort=size | Sort by file size, largest first |
-d | --directory | List directories themselves, not their contents |
Again, please note that the long options may not work in some systems, but the short options are universally supported in all Unix-like systems.
Listing Contents of Other Directories
You can also use ls
to list contents of any other directory. To do so, you
just need to provide the path of that directory:
ls /path/to/directory
Using ls
Command Options
The ls
command has a number of options that can be used to modify the output.
-l
Option
The -l
(lowercase 'L') option stands for 'long format', which includes
additional information such as permissions, number of links, owner, group, size,
and time of last modification:
ls -l
-a
Option
The -a
option stands for 'all'. By default, files that start with a dot (.)
are hidden. If you want to display these files, use the -a
option:
ls -a
-h
Option
The -h
option stands for 'human-readable'. It displays file size in KB, MB,
etc., which is easier to comprehend than bytes:
ls -lh
Sorting ls
Output
-t
Option
The -t
option sorts the output by modification time, newest first:
ls -lt
-r
Option
The -r
option reverses the order of sort:
ls -ltr
This will list the files and directories sorted by modification time, oldest first.
Displaying Directories Recursively
The -R
(uppercase 'R') option stands for 'recursive'. It displays the contents
of directories recursively:
ls -R
Listing Files by File Type
-p
Option
The -p
option appends a character to each entry indicating the file type. For
instance, a slash (/) is appended to directories:
ls -p
-F
Option
The -F
option classifies files by outputting a character at the end of each.
This will add a /
at the end of directories, *
at the end of executable
files, @
at the end of symbolic links, etc.:
ls -F
Listing Files Based on Pattern
You can list files based on a specific pattern. For example, if you want to list all the text files, you can do so by:
ls *.txt
In summary, ls
is a powerful command with numerous options. The examples above
only scratch the surface. To learn more, you can always refer to the ls
manual
page by typing man ls
in your terminal. The manual page provides a
comprehensive list of all available options.
Time To Transition From JavaScript To TypeScript
Level Up Your TypeScript And Object Oriented Programming Skills. The only complete TypeScript course on the marketplace you building TypeScript apps like a PRO.
SEE COURSE DETAILSWhat Can You Do Next 🙏😊
If you liked the article, consider subscribing to Cloudaffle, my YouTube Channel, where I keep posting in-depth tutorials and all edutainment stuff for ssoftware developers.