In this tutorial, you’ll explore how to use the ls command in Linux. The ls command is used to list all of the content of the current working directory. It can be used with various flags to display detailed information about the files, list them in a different order, and show hidden files.

ls command provides the following information

  • The Linux ls command provides detailed information about the files and directories in a system.
  • It displays the permission details, such as user and group ownership, file size, date, and time of modification.
  • The size of files and directories is usually displayed in Bytes, but it can be displayed in a human-readable format.
Note: In Microsoft Windows, we call a folder but in Linux is called a directory.

Options

You can use these options with its main command by using the hyphen (-).

For example:

ls -l

Here ls is the main command and -l is the option.

Note: You can also refer to options as flags.

Syntax:

You have to follow the below syntax to use the ls command.

ls [OPTION]... [FILE]...

A simple listing of Files & Directories

Run the following command to list the files and directories in the current directory in a simple format.

ls

A simple listing of Files & Directories

The long listing of Files and Directories

You can use the -l (It’s lowercase L) option to list contents in a long listing format.

ls -l

The long listing of Files and Directories

Show Hidden Files and Directories

In Linux, a file or directory beginning with a dot (.) is hidden. So to list hidden content, use the -a option with ls:

ls -a

Show Hidden Files and Directories Using ls Command

Display size in Human Readable format

Usually, the ls command shows the size of a file or directory in bytes, which is difficult to understand. But it would be easier to understand if it showed the file size in human-readable format (GB, MB, KB), which is possible using the ls command.

  • Kb (K) – kilobytes
  • MB (M) – megabytes
  • GB (G) – gigabytes

To print the size in human-readable format, use the following command:

ls -lh

Display size in Human Readable format Using ls Command

tlt@ubuntu:~$ ls -lh /
total 2.1G
lrwxrwxrwx   1 root root    7 Feb  5 09:06 bin -> usr/bin
drwxr-xr-x   4 root root 4.0K Feb  5 09:34 boot
drwxrwxr-x   2 root root 4.0K Feb  5 09:15 cdrom
drwxr-xr-x  19 root root 4.1K Feb  5 12:14 dev
drwxr-xr-x 130 root root  12K Feb  5 09:36 etc
drwxr-xr-x   3 root root 4.0K Feb  5 09:16 home
lrwxrwxrwx   1 root root    7 Feb  5 09:06 lib -> usr/lib
lrwxrwxrwx   1 root root    9 Feb  5 09:06 lib32 -> usr/lib32
lrwxrwxrwx   1 root root    9 Feb  5 09:06 lib64 -> usr/lib64
lrwxrwxrwx   1 root root   10 Feb  5 09:06 libx32 -> usr/libx32
drwx------   2 root root  16K Feb  5 09:06 lost+found
drwxr-xr-x   2 root root 4.0K Aug  9 17:18 media
drwxr-xr-x   2 root root 4.0K Aug  9 17:18 mnt
drwxr-xr-x   2 root root 4.0K Aug  9 17:18 opt
dr-xr-xr-x 352 root root    0 Feb  5 09:39 proc
drwx------   4 root root 4.0K Feb  5 09:25 root
drwxr-xr-x  36 root root  940 Feb  6 06:46 run
lrwxrwxrwx   1 root root    8 Feb  5 09:06 sbin -> usr/sbin
drwxr-xr-x  11 root root 4.0K Aug  9 17:25 snap
drwxr-xr-x   2 root root 4.0K Aug  9 17:18 srv
-rw-------   1 root root 2.0G Feb  5 09:06 swapfile
dr-xr-xr-x  13 root root    0 Feb  5 09:39 sys
drwxrwxrwt  22 root root 4.0K Feb  6 07:22 tmp
drwxr-xr-x  14 root root 4.0K Aug  9 17:18 usr
drwxr-xr-x  14 root root 4.0K Aug  9 17:24 var
tlt@ubuntu:~$

Check the version of ls command

Check the ls command version using the following command.

ls --version

ls Command Version

Conclusion

We show you how to use the ls command in Linux. The ls command is used to list all of the content of the current working directory, see the man page for the cat command.

If you have any questions please leave them in the comments below

Leave a Reply

Your email address will not be published. Required fields are marked *