LS: List Items in a Directory

How to use the ls command in linux to list files and directories.

624 views
d

By. Jacob

Edited: 2022-06-03 08:38

Linux tutorial

The ls command is used to list the items in a directory. Typing it without supplying any arguments will list the files in the current directory.

If a directory path argument is supplied, the items located at the given path will be listed.

To list the files in a specific directory:

ls /var/www/ -l

The -l argument shows the result in a list format.

To only list files of a specific type, we can use the * asterisk wildcard. For example, to only list .mp3 files:

ls *.mp3 -l

The ls command can be combined with the watch command, the following will update a file list every 2nd second:

watch ls -s

You can even open the result in nano if you feel like having some fun with someone:

ls -l > /tmp/ls_out && nano /tmp/ls_out

Note. Files in /tmp/ are automatically deleted periodically, typically on boot.

Note. You can always view the manual page in your terminal for more information. To do so, simply type: man [command_name]

Arguments

Here are some other useful arguments.

Argument Description
-R Will also list contents of subdirectories—recursively.
-S Sorts the list by file-size, beginning with the largest file.
-t Sorts by the last-modified time, beginning with the file most recently modified.

Tell us what you think: