To list the files and directories in the current working directory, we use the ls
command.
If on the Windows operating system, when you move to a specific folder, the files and subdirectories in it is automatically displayed for you to view. But for the Linux operating system (for the server) you need to use the ls command
Meaning of the command “ls”
Table of Contents
The ls
command lets you list files or subdirectories contained in a directory. The use is quite simple, on the command line of linux, you just type ls
if you are standing in the directory that you need to list.
For example, as shown below, i’m in the /etc
directory of the system, i type ls
and all files or directories listed.
If you want to list the contents of a directory without looking at where you are, then add the path to the directory immediately after the ls command.
ls /etc
A few advanced uses of the “ls” command
The first is often used to list the contents of a folder and display the results in the latest time stamp at the bottom. Type the command as follows:
Command below with the parameters as follows:
- -l: use a long listing format.
- -t: sort by modification time, newest first.
- -r: reverse order while sorting.
ls -ltr
The second case is to list the contents of the folder and display the results according to the latest timestamp at the top. Type the command as follows:
The -a
parameter: do not ignore entries starting with .
If you look at the image below will see there are several folders with the directory name is a dot.
ls -alt
The third case is to list the contents of the folder and display the results by the size of the file or folder with the largest size at the top. Type the command as follows:
The -S
(s uppercase character) parameter: sort by file size, largest first.
ls -l -S
For more options of the ls command then you type the command below:
ls --help
Conclusion
With this article, I hope you can understand how to use this command to list the contents of a certain folder, as well as how to use the parameters of the this command.
(This is an article from my old blog that has been inactive for a long time, I don’t want to throw it away so I will keep it and hope it helps someone).