Introduction
The ls
is a basic command that every Linux user should be familiar with.
The ls
command displays detailed information about files and directories within the file system. It's included in the GNU core utility package, which comes standard with all Linux distributions.
This tutorial will show you how to use the ls
command using practical examples and extensive explanations of the most frequent ls
options. We will also address a few FAQs on ls command in Linux.
How to Use the ls
Command
The ls
command has the following syntax:
ls [OPTIONS] [FILES]
ls
displays a list of the names of all files in the current working directory when used without any options or arguments:
ls
The files are listed alphabetically in as many columns as your terminal will allow:
cache db empty games lib local lock log mail opt run spool tmp
To list files in a single directory, use the ls
command with the directory path as an input. To list the contents of the /etc
directory, for example, type:
ls /etc
You can also use space to separate several directories and files:
ls /etc /var /etc/passwd
You'll get a notice claiming that ls
can't open the directory because the user you're logged in with doesn't have read permissions:
ls /root
Output
ls: cannot open directory '/root': Permission denied
There are several options for the ls
command. We'll go over the most popular solutions in the sections below.
Long Listing Format
The ls
command's default output simply provides the names of the files and directories, which isn't particularly useful.
ls
will print files in a long listing format if you provide the -l
(lowercase L) option.
When the long listing format is utilized, the following file information is displayed:
- The file format.
- The permissions of the file.
- Number of hard links.
- File Owner.
- File group.
- The size of the file.
- Date and time.
- The name of the file.
Here's an example:
ls -l /etc/hosts
Output
-rw-r--r-- 1 root root 337 Feb 22 18:31 /etc/hosts
Let's go over the output's most significant columns.
The file type is indicated by the first character. The initial character in this example is -, which denotes a normal file. Other file formats have the following values:
-
- This is a regular file.b
- Block special file.c
- A separate file for characters.d
- Directory.l
- Symbolic link.n
- Network file.p
- First-in, first-out.s
- Socket.
The file permissions are shown in the next nine characters. The user gets the first three characters, the group gets the next three, and the others get the last three. The chmod
command can be used to modify file permissions. The following values are possible for the permission character:
r
- Read permission for the file.w
- Write permission to the file.x
- Execution permission for the file.s
- Thesetgid
bit.t
-sticky
bit.
rw-r--r--
means that the user can read and write to the file, while the group and others can only read it. After the authorization characters, the number 1
represents the number of hard links to this file.
The file owner and group are displayed in the next two fields root root
, followed by the file size (337
), which is displayed in bytes. If you want to print sizes in a human-readable format, use the -h
option. The chown
command can be used to change the file owner.
The date and time of the most recent file modification is Feb 22nd, 18:31.
The file's name appears in the last column.
Show Hidden Files
The ls
command does not display hidden files by default. A hidden file in Linux is any file that starts with a dot (.
).
Use the -a
option to show all files, even hidden ones:
ls -la ~/
Output
drwxr-x--- 10 linuxize linuxize 4096 Feb 12 16:28 .
drwxr-xr-x 18 linuxize linuxize 4096 Dec 26 09:21 ..
-rw------- 1 linuxize linuxize 1630 Nov 18 2017 .bash_history
drwxr-xr-x 2 linuxize linuxize 4096 Jul 20 2018 bin
drwxr-xr-x 2 linuxize linuxize 4096 Jul 20 2018 Desktop
drwxr-xr-x 4 linuxize linuxize 4096 Dec 12 2017 .npm
drwx------ 2 linuxize linuxize 4096 Mar 4 2018 .ssh
Sorting the Output
As previously stated, the ls
command lists files in alphabetical order by default.
You can sort the output by extension, size, time, and version with the --sort
option:
--sort=extension
(or-X
) - sort extension alphabetically.--sort=size
(or-S
) - sort by file size.--sort=time
( or-t
) - sort by modification time.--sort=version
(or-v
) - Natural sort of version numbers.
Using the -r
option, you can retrieve the results in reverse sort order.
To sort the files in the /var
directory by modification time in reverse order, for example, type:
ls -ltr /var
It's worth noting that the ls
command does not display the total amount of space taken up by the contents of the directory. Use the du
command to find out how big a directory is.
List Subdirectories Recursively
The -R
option instructs the ls
program to recursively display the contents of the subdirectories:
ls -R
FAQs on ls command in Linux
How do I use the "ls" command?
To use "ls," simply type "ls" followed by the desired options and the directory or location you want to list. For example, "ls -l" lists files and directories in long format.
Can "ls" display hidden files and directories?
Yes, you can use the "-a" option with "ls" to display all files and directories, including hidden ones that start with a dot (.) in their name.
How can I list files in a specific directory?
By providing the path or directory location as an argument to the "ls" command, it will list the files and directories within that specific directory. For example, "ls /home/myuser" lists the contents of the "/home/myuser" directory.
Is it possible to sort the output of "ls" by various criteria?
Yes, you can use options like "-t" to sort by modification time, "-S" to sort by file size, or "-r" to reverse the sorting order. For example, "ls -lt" lists files sorted by modification time in descending order.
How can I list files recursively, including subdirectories?
By using the "-R" option with "ls," it will list files and directories recursively, displaying the contents of subdirectories as well.
Can "ls" display file sizes in a human-readable format?
Yes, you can use the "-h" option to display file sizes in a human-readable format, such as kilobytes (KB), megabytes (MB), or gigabytes (GB), instead of just in bytes.
Can "ls" display file permissions and other metadata?
Yes, using the "-l" option with "ls" will display detailed information about files and directories, including file permissions, ownership, file size, and modification timestamp.
Conclusion
The ls
command outputs a list of files and folders.
Visit the GNU Coreutils page or type man ls in your terminal for more information about ls.
If you have any queries, please leave a comment below and we’ll be happy to respond to them.