CIS120Linux Fundementals
ls Command
The ls
command in Linux is used to list directory contents. It is one of the most frequently used commands and provides various options to customize the output to meet different needs. The command displays information about files and directories, including their names, sizes, permissions, and more.
What the ls Command Does
The basic usage of the ls
command without any options lists the files and directories in the current directory. By adding various options to the command, you can control the amount and type of information displayed, as well as the format of the output.
Common Options for the ls
Command
Here is a table of some of the most popular options for the ls
command and their descriptions:
Option | Description |
---|---|
-l |
Use a long listing format |
-a |
Include directory entries whose names begin with a dot (.) |
-h |
With -l , print sizes in human-readable format (e.g., 1K, 234M, 2G) |
-R |
List subdirectories recursively |
-t |
Sort by modification time, newest first |
-r |
Reverse order while sorting |
-S |
Sort by file size, largest first |
-1 |
List one file per line |
-d |
List directories themselves, not their contents |
-i |
Print the index (inode) number of each file. An inode number in Linux is a unique identifier for a file or directory in a file system. Inodes are data structures that store metadata about files, such as their permissions, size, and owner. |
-F |
Append indicator (one of */=>@) |
-G |
Inhibit display of group information |
--color |
Colorize the output (when output is to a terminal) |
-p |
Append a slash (/) to directory names |
-Q |
Enclose entry names in double quotes |
--full-time |
Use a full time format for listings |
-v |
Natural sort of (version) numbers within text |
Examples
Basic ls command:
Displays a list of files and directories in the current location.
ls
Output:
file1.txt file2.txt dir1 dir2
Long listing format:
Provides detailed information such as file permissions, owner, and size.
ls -l
Output:
total 16
-rw-r--r-- 1 user group 1234 Jun 4 12:34 file1.txt
-rw-r--r-- 1 user group 5678 Jun 4 12:34 file2.txt
drwxr-xr-x 2 user group 4096 Jun 4 12:34 dir1
drwxr-xr-x 2 user group 4096 Jun 4 12:34 dir2
Including hidden files:
Shows all files, including those that start with a dot (.), which are hidden by default.
ls -a
Output:
. .. .hiddenfile file1.txt file2.txt dir1 dir2
Human-readable file sizes:
Displays file sizes in KB, MB, or GB instead of bytes.
ls -lh
Output:
total 16K
-rw-r--r-- 1 user group 1.2K Jun 4 12:34 file1.txt
-rw-r--r-- 1 user group 5.6K Jun 4 12:34 file2.txt
drwxr-xr-x 2 user group 4.0K Jun 4 12:34 dir1
drwxr-xr-x 2 user group 4.0K Jun 4 12:34 dir2
Recursive listing:
Lists the contents of all subdirectories as well.
ls -R
Output:
.:
file1.txt file2.txt dir1 dir2
./dir1:
file3.txt
./dir2:
file4.txt
Sorting by modification time:
Displays files in order of their last modification, newest first.
ls -lt
Output:
total 16
-rw-r--r-- 1 user group 5678 Jun 4 12:34 file2.txt
-rw-r--r-- 1 user group 1234 Jun 4 12:34 file1.txt
drwxr-xr-x 2 user group 4096 Jun 4 12:34 dir1
drwxr-xr-x 2 user group 4096 Jun 4 12:34 dir2
Reverse order sorting:
Lists files in reverse order, with the oldest files displayed first.
ls -lr
Output:
total 16
drwxr-xr-x 2 user group 4096 Jun 4 12:34 dir2
drwxr-xr-x 2 user group 4096 Jun 4 12:34 dir1
-rw-r--r-- 1 user group 5678 Jun 4 12:34 file2.txt
-rw-r--r-- 1 user group 1234 Jun 4 12:34 file1.txt
Conclusion
The ls
command is a powerful tool for navigating and managing files and directories in Linux. By understanding and using the various options available, you can customize the output to suit your needs and efficiently work with your filesystem. Experiment with the different options and combinations to get comfortable with this essential command.