The ls command is used to list all files and directories in the specified directory. Apart from that, it will also show you the properties of the files and directories, like their permissions, ownership, size, and modification date.
Tutorial Details
Description | ls |
Difficulty Level | Low |
Root or Sudo Privileges | No |
OS Compatibility | Ubuntu, Manjaro, Fedora, etc. |
Prerequisites | ls, grep |
Internet Required | No |
Syntax of the ls Command
The ls command takes two arguments as input, and both are optional.
$ ls [OPTION] [LOCATION]
Listing All Files in the Present Directory
The following command, without specifying any flag or path, will list the files and directories in the current directory in a single row.
$ ls
Output:
Listing All Files in the Home Directory
If your PWD is not your home directory, you can use the following command to list all files and directories in your home directory.
$ ls ~
Output:
Listing All Files in the Parent Directory
The following command will list the parent directory files.
$ ls ../
Output:
Listing All Files with Trailing Slashes Added to Directory Names
The “-F
” flag will add the trailing “/
” slashes at the end of the directory name that make them easy to spot.
$ ls -F
Output:
Listing One File Per Line with their Properties
The “-l
” flag is used to list one file per line, including their properties like permissions, ownership, size, and modification date.
$ ls -l
Output:
Listing the Hidden Files
The following command will list the hidden files in your current working directory.
$ ls -a
Output:
Listing the Hidden Files without the Current and Parent Directories
The following command will list all the hidden files and directories except “.
” (current directory) and “..
” (parent directory).
$ ls -A
Output:
Listing Files Recursively
The following command will recursively list all the files and directories within the current directory tree.
$ ls -R
Output:
Listing All Files with their Properties
The following command will display a long format list for all files, including their permissions, ownership, size, and modification date.
$ ls -la
Output:
Listing All Files with Size Displayed Using Human Readable Units
The following command will display all the files in a long format list with human-readable units (KiB, MiB, GiB).
$ ls -lh
Output:
To view the hidden files as well, add the “-a
” flag to your present command, like this: “ls -lha
“.
Listing All Files Sorted By Size (Descending)
The following command will list all the files in your current working directory based on their size, from descending to ascending.
$ ls -lS
Output:
You can see above that “file.txt
” with 0 bytes ended up at the end.
Listing All Files Sorted By Modification Date (Oldest First)
The following command will list all the files in your current working directory based on their modification dates, from oldest to newest.
$ ls -ltr
Output:
Only List Files
The following command will ignore all the directories by listing only the files in your current working directory using the grep command.
$ ls -p | grep -v /
Output:
Only List Directories
The following command will only list the directories in your current working directory.
$ ls -d */
Output:
Only List Hidden Files and Directories
The following command will only list the hidden files and directories in your current working directory using the grep command.
$ ls -a | grep "^\."
Output:
Alternatively, you can also use the “ls -ld .*
” command to list them with their properties.
$ ls -ld .*
Output:
Listing All Files by Inode Number
The following command will list the inode number for all files and directories in your current working directory that can be later used for internal maintenance.
$ ls -i
Output:
Groups the Files with Command Extensions
The following command will group files with the same extension together in the list.
$ ls -lX
Output:
That was the end; I tried to include all the necessary examples in one article so you wouldn’t have to worry.
Join The Conversation
Users are always welcome to leave comments about the articles, whether they are questions, comments, constructive criticism, old information, or notices of typos. Please keep in mind that all comments are moderated according to our comment policy.