The ls command is one of the most frequently used Linux commands. Just like File explorer for GUI, ls command is a CLI version of it.
ls command is generally used to list files and directories in the file system, other useful features are to list hidden files, list file size, list files by date and time, list files in reverse, and list files UID and GID.
By default ls command list files in alphabetical order.
ls command on your terminal to display the contents of the current working directory.
Now almost all Linux terminals display files and directories in colors to differentiate each other. If you wish you can change the color of ls.
ls [options] [file-name/directory-name]
The main ls command options are:
|Display all files including hidden files.|
|List long format|
|List in human-readable file format.|
|List size of files and directories|
|List in reverse order.|
|Sort by time, showing the most recently modified files first.|
|List subdirectories recursively.|
|List files and directories name appended with their types using special characters. For example '/' will be appended to directory name to indicate that it is directory.|
|Display files and directories as comma separated list.|
|List files and directories with quotation marks.|
|List directories and files with its inode number.|
|-||Display alphabetically by file extension.|
Let's look into some basic usage of ls command.
Listing files and directories
The default behavior of
ls command is to display files and directories under the current directory
You may use pwd command to verify the absolute path of the current working directory.
Listing specific directory
ls <path/to/directory> to list files and directories in that directory:
$ ls Pictures
Here ls list all the names of files and directories within the directory named Pictures. However, if you include a trailing slash ("/") after the directory name, it will still work correctly.
You can recursively list all files and directory inside a specific directory using * (wildcard):
$ ls sample *
List content in the parent directory
ls .. command to list files and directories in the parent directory:
$ ls ..
The ".." represents the parent directory itself. This way you can easily interact with files and directories one level above your current working directory. You may use
ls ../.. to list files two levels above.
Common Options with Examples
Let's look into some of the common options of ls command with its examples.
List files in the user’s home directory
You can combine ~ with ls to quickly view files and directories of the current user's home directory.
$ ls ~
If your home directory is /home/user, ls ~ allows to view contents of it without having to specify the full path.
List files in long format (ls -l)
The ls -l command list files and directories in along format - that includes additional information about each file and directory.
ls -l ls -l /path/to/directory ls -l /path/to/filename
ls -l output format:
-rw-r--r-- 1 user group size date time filename drwxr-xr-x 2 user group size date time directory
Each column in the output:
|1||File Permissions||Contains 10 characters. The first character denotes the file type and the remaining 9 character represents the file permissions.|
|2||Number of links||shows the number of hard links the content file or directory has|
|3||Owner||The owner of the file or directory|
|4||Group||Group owner of file or directory|
|5||Size||Content size, by default in bytes|
|6||Date||The last modification date of the file or directory.|
|7||Time||The modification time of the file or directory.|
|8||Filename/Directory name||The actual name of the file or directory.|
By default ls -l shows all symbolic links and its linked file or directory name. This is indicated by link-filename -> target-file-or-directory-name. You can use ls -L to dereference the symbolic link (not show the linked targets).
You can add --full-time option if you want ls -l to include the seconds and the time zone information.
If you are interested in a more user-friendly and feature-rich listing of files and directories - you can refer to exa.
List file size in human readable format
ls -lh to print out a long listing format of files and directories with size in a human readable format. The h option adjusts the file sizes to a more readable format, such as using kilobytes (KB), megabytes (MB), or gigabytes (GB) instead of raw byte counts.
$ ls -lh
List just directories
You can use -d option with ls to list only directories. For example to list only directories in the current directory , type:
$ ls -d */
*/ pattern matches any directory name because
* is a wildcard that represents any characters, and
/ indicates that it should match directories.
List only files
To list only files - there is no straight forward commad using ls. We can combine with egrep to exclude line start with 'd'.
$ ls -l | egrep -v '^d'
List files with their sizes
Use -s option with ls to list files and directories displaying its size in blocks
To list files and directories with their sizes, type
ls -s command:
$ ls -s
Remember the sizes displayed are the allocation sizes in blocks, not the actual sizes of the files or directories in terms of the number of bytes they occupy. A block is equals to a KiloByte ie 4 indiciates 4 x 64KB.
Display hidden files
In Linux, a hidden file is any file that starts with a dot. By default, ls command won't show hidden files. To view hidden files run ls command with
-a the option. This will list all files including hidden files.
$ ls -a
$ ls -al
Hidden files or directories in Linux are generally used for configuration file ( eg .bashrc), user specific settings (eg .bash_history) or version control (eg .git directory) requirements.
Ls command - Sort
Use ls with -X or --sort=extension to list file and directories, sort alphabetically by extension.
$ ls -X or $ ls --sort=extension
Use -S option with ls to sort by size. Example
$ ls -lS or $ ls --sort=size -l
Use ls with -t or --sort=time option to list file and directories by modification time. By default most recently modified files appear first.
$ ls -lt or $ ls --sort=time -l
On that note to sort by modifcation time in ascending order - add -r option to reverse the sorting order. Example:
$ ls -ltr
Redirect ls output to a File
To redirect output to a file we can use output redirection operator '>". For example to redirect the output of ls -l to a file named results.txt, type:
$ ls -l results.txt
You can open the "results.txt" file to view the directory listing using a text editor or display the contents using a command like cat results.txt.
List UID and GID of Files
Use ls with -n option to list and directories with numeric user (UID) and group IDs (GID) . By default ls shows user name group names.
$ ls -n
List files in all directories and subdirectories
To recursively list all files and directories in tree formart use -R with ls command. This would be in a hierarchical format.
$ ls -R
The subdirectories will be indicated by colon (:) at the end of their names. And its contents will be shown just below it.
ls ** is an alternative to ls - R. But shows list files and directories recursively. The pattern ** is known as "globstar" or recursive globbing. This feature depends on shell and its versions.
For more detailed information and additional options, you can refer to man ls or type the same from the terminal.