"ls" (list directory contents) is one of the most commonly used commands in Linux. The command displays a list of all the files and directories in the current directory. It is one of the very first commands one should learn when new to Linux. The command is quite popular, works well enough, and can be used by all level users.
Anyone configuring or installing packages to view the list of contents in a particular directory must know of the command. The 'listing' of files and directories comes with different options producing different information.
The Basic Syntax is:
ls [option]... [path/to/file_or_directory]...
1. List files in current directory
Here are some of the most commonly used options with ls commands:
root@ubuntu:/etc/nginx# ls conf.d fastcgi_params koi-win modules-available nginx.conf scgi_params sites-enabled uwsgi_params fastcgi.conf koi-utf mime.types modules-enabled proxy_params sites-available snippets win-utf
2. View hidden files and directories
"ls -a" is the full command one should execute in order to display all the files and directories including the hidden files/directories.
root@ubuntu:~/mydir# ls -a . .. file1 file2 .file3 test1 test2
Notice that .file3 is actually a hidden file as it is preceded by a ".".
By only executing ls command without the "-a" option, file3 would not have been listed. This command is convenient to use when we want to view all the files in a directory, including the hidden ones.
The current directory (.) and the parent directory (..) is also displayed by "ls -a". These are not actual directories, but sort of handles to the current and parent directories.
3. View full details about files
When we run the "ls" command alone, it provides a basic listing of files and directories in the current directory. However, "ls -l" provides additional detailed information about each file and directory, including permissions, ownership, size, modification date, and more.
ls -l
upon execution produces the contents of the current directory in a long listing format; one per line.
root@ubuntu:~/mydir# ls -l total 12 -rw-r--r-- 1 root root 0 May 9 15:27 file1 -rw-r--r-- 1 root root 14 May 9 15:28 file2 drwxr-xr-x 2 root root 4096 May 9 15:28 test1 drwxr-xr-x 2 root root 4096 May 9 15:28 test2
From the above example, it is seen that the line starts with the file or directory permission, owner (root) and group name(root), file size, created/modified date and time (May 9 15:28), file/folder name as some of the attributes.
4. View files with special characters
The ls -F
command upon execution lists down files and directories in a current directory, with special characters appended to their names to indicate their type. Here's what each character appended to the file/directory name signifies.
- / → directory
- * → executable file
- @ → symbolic link
- = → socket file
- | → named pipe (FIFO)
- > → door (Solaris systems)
Nothing appended indicates a regular file.
root@ubuntu:~/mydir# ls -F file1 file2 test1/ test2/
5. View file Inode/Index Number
One may need to know the inode or the index number for a particular file in order to delete it. This can be achieved by executing the "ls -i" command.
The "ls -i" command displays the inode/index number for a particular file.
root@ubuntu:~/mydir# ls -i 1218 file1 2569 file2 24586 test1 24587 test2
Another Example:
enlightened@enlightened:/var/log/apache2$ ls -lai total 800 3807960 drwxr-x--- 2 root adm 4096 May 15 09:41 . 3413169 drwxrwxr-x 18 root syslog 4096 May 14 01:50 .. 3801161 -rw-r----- 1 root adm 0 Nov 17 11:10 access.log 3801179 -rw-r----- 1 root adm 643420 Nov 16 21:01 access.log.1 3801396 -rw-r----- 1 root adm 7131 May 21 2022 access.log.10.gz 3801322 -rw-r----- 1 root adm 2205 May 20 2022 access.log.11.gz 3801318 -rw-r----- 1 root adm 4131 May 19 2022 access.log.12.gz 3801109 -rw-r----- 1 root adm 9760 May 18 2022 access.log.13.gz 3801101 -rw-r----- 1 root adm 4177 May 17 2022 access.log.14.gz 3801167 -rw-r----- 1 root adm 228 Aug 23 2022 access.log.2.gz 3801127 -rw-r----- 1 root adm 861 Jul 13 2022 access.log.3.gz 3801130 -rw-r----- 1 root adm 881 Jul 12 2022 access.log.4.gz 3801122 -rw-r----- 1 root adm 2280 Jul 11 2022 access.log.5.gz 3801276 -rw-r----- 1 root adm 692 May 25 2022 access.log.6.gz 3801096 -rw-r----- 1 root adm 3728 May 24 2022 access.log.7.gz 3801157 -rw-r----- 1 root adm 8675 May 23 2022 access.log.8.gz 3801258 -rw-r----- 1 root adm 19342 May 22 2022 access.log.9.gz 3801126 -rw-r----- 1 root adm 241 May 15 09:41 error.log 3801128 -rw-r----- 1 root adm 529 May 15 09:41 error.log.1 3801116 -rw-r----- 1 root adm 343 May 6 09:13 error.log.10.gz 3801166 -rw-r----- 1 root adm 588 May 5 00:08 error.log.11.gz 3801111 -rw-r----- 1 root adm 422 May 4 06:33 error.log.12.gz 3801097 -rw-r----- 1 root adm 343 May 3 10:23 error.log.13.gz 3801168 -rw-r----- 1 root adm 343 May 2 08:59 error.log.14.gz 3801155 -rw-r----- 1 root adm 342 May 14 01:50 error.log.2.gz 3801131 -rw-r----- 1 root adm 343 May 13 06:18 error.log.3.gz 3801106 -rw-r----- 1 root adm 343 May 12 05:47 error.log.4.gz 3801102 -rw-r----- 1 root adm 343 May 11 05:54 error.log.5.gz 3801104 -rw-r----- 1 root adm 346 May 10 08:12 error.log.6.gz 3801239 -rw-r----- 1 root adm 343 May 9 07:29 error.log.7.gz 3801112 -rw-r----- 1 root adm 339 May 8 08:54 error.log.8.gz 3801207 -rw-r----- 1 root adm 342 May 7 14:03 error.log.9.gz 3808336 -rw-r----- 1 root adm 0 Feb 19 2017 other_vhosts_access.log enlightened@enlightened:/var/log/apache2$
6. View last edited file
The last created or modified file can be viewed by executing the "ls -t" command.
ls -t | head -1 root@ubuntu:~/mydir# ls -t | head -1 file2
7. Print user-friendly size format
Upon executing ls -lh
, the output indicates the file or the directory size in a human readable format. Here, -h stands for human readable, i.e., it prints the size of the file in kilobytes (K), megabytes (M), gigabytes (G), etc., suffix depending upon the file/ directory size rather than bytes.
root@ubuntu:~/mydir# ls -lh total 12K -rw-r--r-- 1 root root 0 May 9 15:27 file1 -rw-r--r-- 1 root root 14 May 9 15:28 file2 drwxr-xr-x 2 root root 4.0K May 9 15:28 test1 drwxr-xr-x 2 root root 4.0K May 9 15:28 test2
ls -sh yields the following output. Unlike ls -lh, ls -sh prints only the file size and the file name.
root@ubuntu:~/mydir# ls -sh total 12K 0 file1 4.0K file2 4.0K test1 4.0K test2
In summary, "ls -sh" provides a simplified output with only the sizes in human-readable format, while "ls -lh" produces a detailed listing with additional information along with the sizes.
8. View reverse output order by date
So far we've learned that l argument is used for long listing format, t argument sorts all the files and directories based on its modified time and lists the latest first, and r argument is used to reverse the sorting order.
Combining all three, the "ls -ltr" command lists all the directories and file names by sorting the modified date in reverse order.
root@ubuntu:~/mydir# ls -ltr total 12 -rw-r--r-- 1 root root 0 May 9 15:27 file1 drwxr-xr-x 2 root root 4096 May 9 15:28 test2 drwxr-xr-x 2 root root 4096 May 9 15:28 test1 -rw-r--r-- 1 root root 14 May 9 15:28 file2
By default, the "ls" command lists files and directories in alphabetical order. However, when you use the "-r" option, it reverses the order of the listing, displaying the entries in reverse alphabetical order.
Here's what the output of "ls -r" looks like
root@ubuntu:~/mydir# ls -r test2 test1 file2 file1
9. View UID and GID of files & directories
In Linux, UID stands for User Identifier, and GID stands for Group Identifier. They are numerical identifiers assigned to users and groups, respectively, in the operating system.
"ls -n" command prints the UID and GID of each file and sub directory present in the current directory. This way, we are able to view the IDs respectively.
root@ubuntu:~/mydir# ls -n total 12 -rw-r--r-- 1 0 0 0 May 9 15:27 file1 -rw-r--r-- 1 0 0 14 May 9 15:28 file2 drwxr-xr-x 2 0 0 4096 May 9 15:28 test1 drwxr-xr-x 2 0 0 4096 May 9 15:28 test2
When a user creates a file or directory in Linux, the file gets associated with the UID and GID of the user who created it. This information is used to determine the file's ownership and permissions. File permissions, such as read, write, and execute, can be granted or restricted based on the UID and GID of the user and group associated with the file.
Another Example:
enlightened@enlightened:/var/log/apache2$ ls -lain total 800 3807960 drwxr-x--- 2 0 4 4096 May 15 09:41 . 3413169 drwxrwxr-x 18 0 108 4096 May 14 01:50 .. 3801161 -rw-r----- 1 0 4 0 Nov 17 11:10 access.log 3801179 -rw-r----- 1 0 4 643420 Nov 16 21:01 access.log.1 3801396 -rw-r----- 1 0 4 7131 May 21 2022 access.log.10.gz 3801322 -rw-r----- 1 0 4 2205 May 20 2022 access.log.11.gz 3801318 -rw-r----- 1 0 4 4131 May 19 2022 access.log.12.gz 3801109 -rw-r----- 1 0 4 9760 May 18 2022 access.log.13.gz 3801101 -rw-r----- 1 0 4 4177 May 17 2022 access.log.14.gz 3801167 -rw-r----- 1 0 4 228 Aug 23 2022 access.log.2.gz 3801127 -rw-r----- 1 0 4 861 Jul 13 2022 access.log.3.gz 3801130 -rw-r----- 1 0 4 881 Jul 12 2022 access.log.4.gz 3801122 -rw-r----- 1 0 4 2280 Jul 11 2022 access.log.5.gz 3801276 -rw-r----- 1 0 4 692 May 25 2022 access.log.6.gz 3801096 -rw-r----- 1 0 4 3728 May 24 2022 access.log.7.gz 3801157 -rw-r----- 1 0 4 8675 May 23 2022 access.log.8.gz 3801258 -rw-r----- 1 0 4 19342 May 22 2022 access.log.9.gz
Note: UID = 0 means ROOT user
10. View the files & directories separated by comma
The ls -m
command is used to serve the purpose. It prints all the files and directories of the current directory separated by a comma.
Sample output
root@ubuntu:~/mydir# ls -m file1, file2, test1, test2
11. Viewing files and directories without the owner details
"-g" option is basically used to skip the owner details associated with the file or directory.
"ls -g" yields the following output
root@ubuntu:~/mydir# ls -g total 12 -rw-r--r-- 1 root 0 May 9 15:27 file1 -rw-r--r-- 1 root 14 May 9 15:28 file2 drwxr-xr-x 2 root 4096 May 9 15:28 test1 drwxr-xr-x 2 root 4096 May 9 15:28 test2
As it is clearly visible from above, the group information (which is usually displayed in the third column when using "ls -l") is omitted. The remaining columns provide details, such as file permissions, the number of hard links, the owner, the file size, the modification date and time, and the file or directory name.
So, in summary, "ls -g" is similar to "ls -l", but it excludes the group information from the listing.
12. Viewing only the sub-directories
The ls -d */
command prints only the sub-directories without printing the files in that particular directory. This command can be useful when you want to just view the sub-directories in a directory that contains lots of files.
root@ubuntu:~/mydir# ls -d */ test1/ test2/
Another example
enlightened@enlightened:/usr$ ls -ld */ drwxr-xr-x 3 root root 4096 Jul 15 2018 aarch64-linux-gnu/ drwxr-xr-x 4 root root 4096 Dec 14 2021 arm-linux-gnueabi/ drwxr-xr-x 5 root root 4096 Dec 14 2021 arm-linux-gnueabihf/ drwxr-xr-x 2 root root 139264 May 12 06:39 bin/ drwxr-xr-x 2 root root 4096 May 4 21:20 games/ drwxr-xr-x 5 root root 4096 Jul 15 2018 i686-w64-mingw32/ drwxr-xr-x 124 root root 20480 May 4 21:50 include/ drwxr-xr-x 172 root root 20480 May 4 21:50 lib/ drwxr-xr-x 3 root root 12288 May 4 20:50 lib32/ drwxr-xr-x 2 root root 4096 May 4 20:43 lib64/ drwxr-xr-x 24 root root 12288 May 4 21:50 libexec/ drwxr-xr-x 3 root root 4096 May 4 20:50 libx32/ drwxr-xr-x 10 root root 4096 Oct 13 2016 local/ drwxr-xr-x 3 root root 4096 Jul 15 2018 mips64el-linux-gnuabi64/ drwxr-xr-x 3 root root 4096 Jul 15 2018 mipsel-linux-gnu/ drwxr-xr-x 7 root root 4096 Jul 11 2017 NX/ drwxr-xr-x 2 root root 24576 May 12 06:39 sbin/ drwxr-xr-x 560 root root 20480 May 11 21:34 share/ drwxr-xr-x 14 root root 4096 May 4 21:50 src/ drwxr-xr-x 3 root root 4096 May 25 2022 x86_64-w64-mingw32/ enlightened@enlightened:/usr$
13. View contents of another directory
By default, ls command shows the contents of the current directory. If you want to view the contents of another directory without navigating, or changing the current directory then use the following command:
enlightened@enlightened:~$ ls -la /etc/apache2/ total 104 drwxr-xr-x 8 root root 4096 May 4 21:35 . drwxr-xr-x 197 root root 16384 May 12 06:39 .. -rw-r--r-- 1 root root 7178 Mar 8 22:02 apache2.conf drwxr-xr-x 2 root root 4096 May 4 21:35 conf-available drwxr-xr-x 2 root root 4096 Sep 10 2021 conf-enabled -rw-r--r-- 1 root root 1782 Dec 8 2016 envvars -rw-r--r-- 1 root root 31063 Mar 19 2016 magic drwxr-xr-x 2 root root 16384 May 4 21:35 mods-available drwxr-xr-x 2 root root 4096 Jul 11 2022 mods-enabled -rw-r--r-- 1 root root 274 Jan 18 16:24 ports.conf drwxr-xr-x 2 root root 4096 May 4 21:43 sites-available drwxr-xr-x 2 root root 4096 Jul 11 2017 sites-enabled enlightened@enlightened:~$
Checking the version of "ls" command
We can check the version of any package deployed and installed on linux by using the --version
after the package name.
$ ls --version ls (GNU coreutils) 9.1 Copyright (C) 2022 Free Software Foundation, Inc. License GPLv3+: GNU GPL version 3 or later <https://gnu.org/licenses/gpl.html>. This is free software: you are free to change and redistribute it. There is NO WARRANTY, to the extent permitted by law. Written by Richard M. Stallman and David MacKenzie. enlightened@enlightened:/usr$
For example, the version of "ls" can be identified by executing the command "ls –version".
Please note that the ls command typically does not have a built-in "--version" option in most Linux distributions, including Ubuntu.
To determine the version of the core utilities package, including ls, you can use the following command:
dpkg -l coreutils
This command will display information about the "coreutils" package, including various essential command-line utilities such as ls. The output will include the version number of the package.
The command yields the following output:
||/ Name Version Architecture Description +++-=================-=============-=============-======================================== ii coreutils 8.28-1ubuntu1 amd64 GNU core utilities
Help page of "ls" command
We do get a guide or a help page for any utility Linux command, and it can be viewed by using -–help
.
For example, to display the guide or the help page for "ls", we use "ls –help".
enlightened@enlightened:/usr$ ls --help Usage: ls [OPTION]... [FILE]... List information about the FILEs (the current directory by default). Sort entries alphabetically if none of -cftuvSUX nor --sort is specified. Mandatory arguments to long options are mandatory for short options too. -a, --all do not ignore entries starting with . -A, --almost-all do not list implied . and .. --author with -l, print the author of each file -b, --escape print C-style escapes for nongraphic characters --block-size=SIZE with -l, scale sizes by SIZE when printing them; e.g., '--block-size=M'; see SIZE format below -B, --ignore-backups do not list implied entries ending with ~ -c with -lt: sort by, and show, ctime (time of last modification of file status information); with -l: show ctime and sort by name; otherwise: sort by ctime, newest first ... (truncated)
The Bottom Line
This article demonstrated various ls
command examples and their uses in a typical Linux environment. The ls command in Linux is used to list files and directories in a specified directory.
It also provides different options to display information like file details, sizes, permissions, and supports various formatting options for convenient file management and navigation.
It is one of the most basic commands every beginner should know when learning linux.