Linux uses the
ls keyword as a command shortcut to list files. When executed in a Linux console it will list, print out to the console, the files of the current working directory and any sub-directories. By passing the
ls command utility various flags we can control the output and level of detail.
List a directory’s files with
You see the contents of a specific directory or folder in Linux simply passing the ls command the directory location as a parameter as follows.
The output will be printed to the console (also known as standard output or std out for short) listing the files and directories in alphabetical order.
List hidden files and folders with
We can also view any hidden files and folders which will not be shown when
ls is called in its standard form by default. To do this we just need to append
-a when we call ls in a console as follows.
ls -a /usr
List files types with
It is also possible to get more information that just a file’s extension with the
ls command, we can get it to give a little indication about what a file is used for, the file’s type too. To do this all we need to do is pass the
-F flag as follows.
Note that the types are denoted by an extra character in the filename as follows: backslash,
/, for a directory; an at-sign,
@ for a symbolic link; a pipe,
|, for a FIFO;an equals character,
=, for a socket; and,
> is for a door.
List detailed file information with
Often you will want more information than is shown by default. To get extended information we can pass
-l flag which will cause it to print a single list of files and sub-directories with extended information about the files in the right-hand columns. Just enter it in a console as follows.
List files by size using
To sort a directory listing by name pass the
-S option. In the following example this is combined with the
-l option to show a long listing.
List files sorted by modification time with
As well as all the other options discussed above we can sort the output from ls too. By passing in the
-t option ls will print out the files by modification time.
Of course this can be combined with other options and flags too, for example we can use
-lt to get detailed information too.
List files sorted by access time with
We can also sort the listed files by access time using the
-u flag as follows.
And with the
-l option more detailed file information will be printed out.
List files with file size in human readable format using
It is also useful often to see the size of files in human readable format sometimes too in Linux. To do this all we need to do is to pass the