Categories: How-To

ls command on Linux (list files) [Basic Guide]

The ls command is undoubtedly one of the most used. It basically serves to list files and directory content.

Your most common options are:

  • -a, –all Show all files, including hidden files that begin with “.”
  • -A, –almost-all Shows almost all files, except the entries “.” and “..”
  • -b, –escape Show files with escape, for non-graphic characters
  • -c Show and sort by the date the files were modified
  • -C Show by columns

  • -d Show only -1 directories
  • ; Shows a column with file names only
  • -h Shows file sizes in human format
  • -l Show a long list of the files
  • -m Lists the files separated by a comma
  • -S ; Sort by size
  • -R List subdirectories recursively
  • -x List in rows 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.gz, /boot/System.Map-4.14.138-114.102.amzn2.x86_64.gz, /boot/System.Map-4.14.138-114.102.amzn2.x86_64.gz, /boot/System.Map-4.14.138-114.102.amzn2.x86_64.gz, /boot/System.Map-4.14.138-amzn2.x86_64, /boot/vmlinuz-4.14.138-114.102.amzn2.x86_64

$ ls -Sl ~/
-rw-rw-r-- 1 ec2-user ec2-user 221427 Dec 7 2018 mysqltuner.pl
-rw-rw-r-- 1 ec2-user ec2-user 35487 May 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 earlier)
All directories contain 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 the files in order of modification:

$ ls -lt 
-rw-r--r-- 1 ec2-user root 16172 May 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 16050 jan 20 13:30 -rw-r--r-- 1 ec2-user root 16050 jan 20 13:30
-rw-r--r-- 1 ec2-user root 16050 jan 20 13:30
-rw-r--r-- 1 ec2-user root 16050 jan 20 13:30 -rw-r--r-- 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 in order of size:

$ ls -lS 
-rw-r--r-- 1 ec2-user root 21195 jan 20 13:30 CapForm.php -rw-r--r-- 1 ec2-user root
16172 May 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 12598 jan 20 13:30 -rw-r--r-- 1 ec2-user root 12598 jan 20 13:30
-rw-r--r-- 1 ec2-user root 12598 jan 20 13:30 -rw-r--r-- 1 ec2-user root 12598 jan 20 13:30
-rw-r--r-- 1 ec2-user root 12598 jan 20 13:30 -rw-r--r-- 1 ec 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 the files. In this case, the file names are sent to the text editor Vi:

$ 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 register here. If you already have an account, or want to create one, just log in or create your user here.

Did you like it?

Share

Uirá Endy Ribeiro

Uirá Endy Ribeiro is a Software Developer and Cloud Computing Architect with a 23-year career. He has master's degrees in computer science and fifteen IT certifications and is the author of 11 books recognized in the IT world market. He is also Director at Universidade Salgado de Oliveira and Director of the Linux Professional Institute - LPI Director's Board.

Uirá Endy Ribeiro

Uirá Endy Ribeiro is a Software Developer and Cloud Computing Architect with a 23-year career. He has master's degrees in computer science and fifteen IT certifications and is the author of 11 books recognized in the IT world market. He is also Director at Universidade Salgado de Oliveira and Director of the Linux Professional Institute - LPI Director's Board.

Share
Published by
Uirá Endy Ribeiro

Recent Posts

Sudo command on Linux (privilege scale) [Basic Guide]

The sudo command on Linux executes a given command as if it were another user.…

2 years ago

SS command on Linux (investigate the network) [Basic Guide]

The ss command on Linux is extremely useful for investigating sockets, providing various information about…

2 years ago

Free Linux command (memory usage) [Basic Guide]

Free Linux command shows the amount of total memory in use and available, as well…

2 years ago

Linux while command (loop – while) [Basic Guide]

The shell has structures for testing conditions and executing certain program sequences several times (loop),…

2 years ago

Linux fstab file (disk mount setup) [Basic Guide]

The /etc/fstab file stores the configuration of which devices should be mounted and what is…

2 years ago

Netcat command on Linux (Swiss network knife) [Basic Guide]

The Netcat Command in Linux or nc is a utility used to do “almost anything”…

2 years ago

This website uses cookies.