- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Crucial Linux ls Commands to Know
Introduction
When it comes to working with the Linux operating system, one of the most basic and essential tasks is navigating and managing files and directories. For this reason, it's important to be familiar with a few key ls commands, which are used to list the contents of a directory. In this article, we'll explore some of the most crucial ls commands to know, and how you can use them to make your work with Linux more efficient and effective.
ls
The ls command is one of the most basic and commonly used commands in the Linux operating system. When executed without any options, it simply lists the contents of the current working directory (cwd).
$ ls file1 file2 file3 directory1 directory2
ls -l
The ls -l option provides a detailed view of the contents of a directory. It displays information such as file permissions, ownership, timestamps, and the number of links to a file.
$ ls -l total 8 -rw-rw-r-- 1 user1 user1 18 Feb 7 23:03 file1 -rw-rw-r-- 1 user1 user1 26 Feb 7 23:03 file2 -rw-rw-r-- 1 user1 user1 23 Feb 7 23:03 file3 drwxrwxr-x 2 user1 user1 4096 Feb 7 23:04 directory1 drwxrwxr-x 2 user1 user1 4096 Feb 7 23:04 directory2
ls -a
The ls -a (or ls --all) option lists all of the contents of a directory, including hidden files and directories that are not normally displayed. This is useful for locating and managing hidden files and directories.
$ ls -a . .. .hidden_file file1 file2 file3 directory1 directory2
ls -t
The ls -t (or ls --time) option sorts the contents of a directory by modification time, displaying the most recently modified files and directories first. This is useful for quickly locating and working with recently updated files.
$ ls -t file2 file3 file1 directory2 directory
ls -r
The ls -r (or ls --reverse) option sorts the contents of a directory in reverse order, displaying the files and directories in a reversed order from what is normally seen with the ls command. This is useful for reversing the display order to find specific files and directories.
$ ls -r directory1 directory2 file1 file3 file2
ls -h
The ls -h (or ls --human-readable) option displays file sizes in a human-readable format, using units such as KB, MB, and GB instead of raw numbers. This makes it easier to understand the size of files and directories at a glance.
$ ls -h file1 (18B) file2 (26B) file3 (23B) directory1 (4.0K) directory2 (4.0K)
ls -R
The ls -R (or ls --recursive) option is used to display the contents of a directory, including all of its subdirectories and their contents. This is useful for quickly locating and managing files and directories within complex directory structures.
$ ls -R .: file1 file2 file3 directory1 directory2 ./directory1: file4 file5 file6 ./directory2: file7 file8 file9
ls --color
The ls command can be extended to display the output with color coding, making it easier to distinguish between different types of files and directories. This can be achieved using the ls --color option or by adding the following line to your “.bashrc” file −
$ alias ls='ls --color=auto'
ls With Custom Sorting
The ls command can be used in combination with other commands and utilities to sort files and directories in custom ways. For example, the following bash script sorts the contents of a directory by file size, with the largest files appearing first −
$ ls -lSr | awk '{print $5, $9}'
ls With File Type Grouping
The following bash script groups the contents of a directory by file type, displaying directories, symbolic links, and regular files separately (awk is a programming language popular for customizing text input.) −
$ ls -l | awk '{ if ($9 ~ /^.*->.*$/) print "LINK: ", $9; else if ($1 ~ /^d.*/) print "DIR: ", $9; else print "FILE: ", $9 }
ls With File Type Filtering
The following bash script only displays files of a specific type, such as only display “.txt” files. Here, grep command is used to display the lines which have “.txt” −
$ ls | grep ".txt$"
Conclusion
By mastering these crucial ls commands, you'll be well on your way to becoming a proficient Linux user. Whether you're a seasoned professional or just starting out, understanding how to effectively navigate and manage files and directories is a critical skill. With these commands at your disposal, you'll be able to work with Linux with greater efficiency and confidence.