ls command is used to list files and directories in current
working directory.
Eg.
cat > test1
This is a test file.
ls
In the above set of commands we created a file called test1.
Later, we executed ls command to see the list of files and directories in pwd.
The above screenshot shows the output of ls command. We can
see that it shows the list of files and directories. The names highlighted in
blue are the names of folders while the ones in white are the files. The same
can also be verified from the file browser.
ls command can also be used to view the files and folders in
another directory.
Eg. ls Desktop
The above command will show the list of files and
directories in Desktop folder. It is shown in the below screenshot.
From above screenshot we can see that the Desktop folder has
a file named Shell Scripting - In a Nutshell.docx
If we also want to see the files and permissions for the
files and the folders then we can use ls command with -l.
Eg. ls -l
You can see from the above screenshot that the first line of
output shows "total 40".
This means that all the files and folders listed collectively
occupy 40 blocks of memory (Each block of memory is 1024 bytes).
Moreover, you can observe that some of the listing the first
column starts with d, while for some it starts with -. Please note that,
whenever the first column starts with d, then it is a directory while, if the
first column starts with - then it is a file.
Now, let us have a look at the permissions. The second,
third and the fourth columns tell about the permissions to the owner, next
three columns tell about the permissions to the group while the subsequent
three columns tell about the permissions to other user.
For example, the Desktop has read, write and execute
permissions for the owner, while only read and execute permissions for the
group and other users.
Another important extension for ls is -a where 'a' stands
for 'All'. When you use ls command with -a, it will show the list of all the
files and folders including those which are hidden.
Eg.
As seen in the above screenshot, ls -a displayed the list of
all the files and folders in present working directory including those which
are hidden. Here, all the files and folders starting with . denote hidden.
ls command can be used with many more options however, above
two are the most widely used options. As we move further in the tutorial, we
will see some more.