The ls command lists files and directories within the file system, and shows detailed information about them.
The syntax for the ls command is as follows:
$ pwd
When used with no options and arguments, ls displays a list of the names of all files in the current working directory:
Example:
$ ls
The files are listed in alphabetical order in as many columns as can fit across our terminal:
Output:
cache db empty games lib local lock log mail opt run
To list files in a specific directory, we pass the directory path as an argument to the ls command. For example, to list the contents of the /etc directory, we would type:
Output:
$ ls /etc
If the user we are logged in with doesn’t have read permissions to the directory, we will get a message saying that ls can’t open the directory:
Example:
$ ls /root
ls: cannot open directory '/root': Permission denied
The -l option tells ls to print files in a long listing format:
Example:
$ ls -l /etc/hosts
-rw-r--r-- 1 root root 337 Oct 4 11:31 /etc/hosts
In the long listing format, we can see the following file information: