ls Linux command is used to list files and directories in the current directory. When you type
ls and press Enter in the terminal, it will display all the files and folders in the directory you are currently in. It’s a fundamental command that helps users navigate and manage their file system by providing an overview of the contents.
You can also use various options with the
ls command to view additional details, such as file permissions, ownership, file size, and modification dates. In this post, we explore all possible uses of the
General syntax for
$ ls [OPTION...] [FILE...]
1. Display hidden files
This option will display the entire list of directories, including hidden files. Hidden files start with a dot (.)
. .. .config .bashrc file1.txt folder1 .hiddenfolder picture.jpg
In this example, the single dot (
.) represents the current directory, and the double dot (
..) represents the parent directory. The
.bashrc files, along with the
.hiddenfolder, are hidden files and directories that would not be shown with the plain
ls command but are displayed here because of the
-a option. The other items are regular, non-hidden files and directories.
2. Sorting by file size
ls -S command to sort files and directories by size in descending order, and then print them to the terminal.
file_large.mp4 image1.jpg document.pdf textfile.txt folder1/ folder2/
In this example,
file_large.mp4 is the largest file, and
textfile.txt is the smallest file. The directories
folder2/ are also listed, but the
-S option doesn’t consider their sizes in the sorting. If you want to see the sizes along with the files, you can combine the
-S option with the
-l option, like ls
3. Display files in long format
-l option displays the contents of the directory in a more detailed way. Displaying the file’s owner and group, last modified time, and more.
total 48 drwxr-xr-x 5 user user 4096 Aug 6 10:30 Documents -rw-r--r-- 1 user user 123 Aug 6 10:20 file.txt drwxr-xr-x 2 user user 4096 Aug 6 10:15 Music -rwxr-xr-x 1 user user 2048 Aug 6 10:10 script.sh drwxr-xr-x 3 user user 4096 Aug 6 10:05 Pictures
Here’s a breakdown of what each part means:
drwxr-xr-x: The file permissions (e.g.,
dindicates a directory,
rwxmeans read, write, and execute permissions for the owner).
5: The number of hard links to the file or directory.
user: The owner of the file or directory (listed twice, once for the owner and once for the group).
4096: The file size in bytes.
Aug 6 10:30: The date and time the file or directory was last modified.
Documents: The name of the file or directory.
4. Sort by date and time
This command sorts files by last modified time. The most recently edited files will appear at the top of the output, making them easy to find.
report.txt image.png project/ notes.docx old_data.csv
In this example,
report.txt is the most recently modified file, while
old_data.csv is the oldest. If you were to run the command in a different directory or at a different time, the output would vary based on the files and their modification times in that specific directory.
5. Display only directories
ls -d */
Use this command to list the sub-directories excluding all other files.
Here’s a sample output for the command, assuming you have three directories named
Music in the current directory:
Documents/ Pictures/ Music/
Each directory name is followed by a slash (
/), indicating that it is a directory.
6. List files and save results to a file
ls > [filename]
ls > [filename] command allows you to save the output of the preceding command to a file.
ls > filename.txt doesn’t display any output in the terminal. Instead, it redirects the output of the
ls command to a file named
If you were to run the
ls command in a directory containing files and folders, the names of those files and folders would be written to
filename.txt. You wouldn’t see anything in the terminal itself.
Here’s an example of what might be inside
filename.txt if the directory contained three files and one folder:
file1.txt file2.jpg folder1 file3.pdf
The exact contents would depend on the files and directories present in the current directory where the command was run.
7. List file owners with their ID
drwxr-xr-x 2 1001 1001 4096 Apr 1 12:34 directory1 -rw-r--r-- 1 1001 1001 0 Apr 1 12:34 file1.txt -rwxr-xr-x 1 1002 1002 123 Apr 1 12:34 script.sh
In this example, the first column shows the file permissions, the second column shows the number of hard links, the third and fourth columns show the user and group IDs, the fifth column shows the file size in bytes, and the sixth and seventh columns show the date and time of the last modification. The last column shows the name of the file or directory.
More Linux commands:
|File System Operations||
|Search and Text Processing||
|System Information and Management||
|User and Session Management||