Linux ls Command: List Files and Directories with Details
The ls command is undoubtedly one of the most used. It basically serves to list files and directory contents.
Its most common options are:
- -a, --all Show all files, including hidden files that start with "."
- -A, --almost-all Show almost all files, excluding the "." and ".." entries
- -b, --escape Show files with escape, for non-graphic characters
- -c Show and sort by file modification date
- -C Show in columns
- -d Show only directories
- -1 Show one column only with file names
- -h Show file sizes in human-readable format
- -l Show a long list of files
- -m List files separated by comma
- -S Sort by size
- -R List subdirectories recursively
- -x List in lines instead of columns
- --color=auto Differentiate file types by color
Examples:
$ ls -m /boot/*138*/boot/config-4.14.138-114.102.amzn2.x86_64, /boot/initramfs-4.14.138-114.102.amzn2.x86_64.img, /boot/symvers-4.14.138-114.102.amzn2.x86_64.gz, /boot/System.map-4.14.138-114.102.amzn2.x86_64, /boot/vmlinuz-4.14.138-114.102.amzn2.x86_64
$ ls -Sl ~/
-rw-rw-r-- 1 ec2-user ec2-user 221427 dez 7 2018 mysqltuner.pl
-rw-rw-r-- 1 ec2-user ec2-user 35487 mai 21 2017 vfsync-2017-05-21.tar.gz
![]() | On Linux, files that start with "." are hidden, and are only shown with the -a option of ls The file "." is a pointer to the current directory The file ".." is a pointer to the directory above (or previous) All directories have the files "." and ".." File and directory names are case-sensitive |
The following ls options are very useful for programmers:
Use the "-lt" option to show files by modification order:
$ ls -lt-rw-r--r-- 1 ec2-user root 16172 mai 6 22:19 List.php-rw-r--r-- 1 ec2-user root 6279 jan 20 13:47 SubForm.php-rw-r--r-- 1 ec2-user root 21195 jan 20 13:30 CapForm.php-rw-r--r-- 1 ec2-user root 16050 jan 20 13:30 CapList.php-rw-r--r-- 1 ec2-user root 7937 jan 20 13:30 Form.php-rw-r--r-- 1 ec2-user root 12598 jan 20 13:30 List.php
The "-lS" option is useful for showing files by size order:
$ ls -lS-rw-r--r-- 1 ec2-user root 21195 jan 20 13:30 CapForm.php-rw-r--r-- 1 ec2-user root 16172 mai 6 22:19 List.php-rw-r--r-- 1 ec2-user root 16050 jan 20 13:30 CapList.php-rw-r--r-- 1 ec2-user root 12598 jan 20 13:30 List.php-rw-r--r-- 1 ec2-user root 7937 jan 20 13:30 Form.php-rw-r--r-- 1 ec2-user root 6279 jan 20 13:47 SubForm.php
The following command is also useful for making an inventory of files. In this case, the file names are sent to the Vi text editor:
$ ls -XR | vim -
Another text editor can be used, such as nano:
$ ls -XR | nano -
Learn much more about Linux in our online course. You can enroll here. If you already have an account, or want to create one, just log in or create your user here.
Liked it? Share
That was just one piece. The course teaches the whole path — from your first command to exam day.
