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.
Options
Options | Explanation |
---|---|
-l | A long listing of Files & Directories |
–author | Print authors of the Content |
~ | Display the contents of the Home Directory |
-a | List Hidden Files & Directories |
-A | do not list implied (.) and (..) |
-R | Print Files & Directories Recursively |
-d | List the directory itself and not their content |
-h | Display size in Human Readable format |
-1 | Display one File/Directory per line |
-s | Print the allocated size of each File & Directory |
-F | Display files & directories with different indicators at the end |
-i | List Inode numbers |
-x | Display the contents in a line pattern |
-m | List contents in a comma-separated format |
-Q | Display content names within Quotes |
-n | Display UID and GID of Files & Directories |
-g | Only list Group names in a long listing format |
-G | Only list Owner names in a long listing format |
-u | List Files & Directories as per Access Time |
-r | List files & directories in Reverse order |
-S | Sort contents as per their Size |
-t | Sort contents as per their Modification Time |
–hide | Exclude/Hide file types from the List |
–time-style=”+%Y-%m-%d $newline%m-%d %H:%M” | Display contents in different styled modified date format |
-X | Sort files as per their Extension |
–color=auto –color=yes –color=no –color=never | Enable/Disable colors in the Output |
–help man | Help/Manual page access |
–version | Check the version of the ls command |
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.
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
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
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
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
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
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