ls Command in Linux for Listing Files

If you are a regular Linux user, there must hardly be a day when you haven't used ls command. A very simple yet powerful command used for listing files and directories. By default, it will list the content of the current directory.

In this article, I will show you ls command in Linux to list files with examples.

1) Run ls without parameter

Running ls without parameter will display a list of files and or directories only. No other information can be seen from the output

$ ls

Ls default

2) Use long listing format

Using -l character (small L letter), will display a long listing of the content of current directory ( i.e. not only prints the name of the file, but also some attributes such as owner, group owner, link count, permissions).

$ ls -l

ls long listing format

Here’s how to read the output :

1st column

The first letter d does the content is directory or file. On the screenshot above, Desktop, Documents, Downloads and lynis-1.3.8 are directories. If it - (minus sign) it means that the content is file. While if it equal with l (small L character), means the content is link file.

The next 9 character is about file permission. With the first 3 rwx characters are for Owner of the file, the second 3 characters are for Group owner of the file and the last 3 characters are for worldwide access to the file.

2nd column
This tells us about how many link to this file

3rd column
This tells us about who is the owner of the file/directory

4th column
This tells us about who the group owner of the file/directory

5th column
This tells us about the size of the file/directory in bytes unit. Except for directories, the size will always count as 4096 bytes

6th column
This tells us about the last time and date the file is modified

7th column
This tells us the filename or directory name

3) Show the size of file

Reading size in bytes unit can confuse us. To read 6,5 M is easier compare with reading 6727680 byte. To do this, we can use -h combine with -l parameter. -h parameter means human readable

$ ls -lh

List by size

Another parameter that can do this is --si parameter. This parameter is similar with -h parameter, but --si powers 1000 while -h powers 1024.

$ ls -si

List using --si

4) Sort file size

After we can show the file size, we may want to sort it by file size. We can use -S parameter to to this. The list will be sort by the largest file size first.

$ ls -lhS

Sort by size

5) Scaling size

Ls can scale size by before printing them using --block-size=SIZE. Where SIZE are :

  • K = Kilobyte
  • M = Megabyte
  • G = Gigabyte
  • T = Terabyte
  • P = Petabyte
  • E = Exabyte
  • Z = Zettabyte
  • Y = Yottabyte

For example, we want to scale size using Megabyte units. So the syntax will be like this :

$ ls -l --block-size=M

List block size

6) Show hidden files

In Linux, a file begins with “.” (dot sign) is a hidden file. To show it on ls command, we can use -a parameter.

$ ls -a

Show hidden files

7) List directory entries only

If we want to list directory entries only, we can use -d parameter.

$ ls -d */

List directories only

8) Print entries without owner information

To do this, we can use -g parameter.

$ ls -g

List without owner

9) Print entries without group information

While -g is to suppress owner information, -G will suppress group information

$ ls -lG

List without group info

10) Print UID and GID

If we want to know the UID and GID of owner and group owner, we can do it using ls command with -n parameter. Here’s a sample.

$ ls -n

List UID and GID

From the example above, we know that user pungki has UID = 1000 and GID = 1000. While root group has GID = 0

11) Print with no color

Some Linux distribution is enable color options to ls command. This will make ls print the list in color. If you don’t want it, you can use --color=never parameter.

$ ls --color=never

List no color

12) Print the index number of each file

To print the index number or known as inode number, we can use -i parameter. The index number will appear at first column.

$ ls -li

List with inode number

13) Add / (slash sign) to mark directory

Appends forward slash to the directories. To do this, use -p parameter.

$ ls -p

Add slash sign

14) Reverse order while sorting

You may also want to list entries in reverse order. To do this, we can use -r parameter.

$ ls -r

Reverse order

15) List subdirectories recursively

With -R parameter, you can list directory including its subdirectories.

$ ls -R

List recursive

16) Sort by extension

You can sort the list by extension using -X parameter or --sort=extension.

$ ls -lX

Or

$ ls --sort=extension

Sort by extension

17) List by modification time

Using -t parameter will sort the list by modification time which the newest first.

$ ls -lt

List by modification time

18) List your home directory

For listing your home directory, we can short the directory using "~" (tilde sign). So you don’t have to type your full directory name. Let say if the home directory name is /home/pungki, then ~ sign have the meaning with /home/pungki.

$ ls ~

Ls using tilde sign

19) List parent directory

Whenever you are inside a directory, you can also list the parent directory without need to type directory full name. Here’s a sample.

$ ls ../

This will list the contents of the directory one level above.

$ ls ../../

This will list the contents of the directory two level above.

List parent directory

20) File Search

a) Search files ending with letter 'l"

$ ls *l

b) Search files ending with 5 characters

Note - The directory should have files with 5 characters in length otherwise it wont give any outputs.

:~/test$ ls welcome*
welcome@12345  welcome@34567  welcome@65467  welcome@65678  welcome@657878  welcome@65789
:~/test$ ls welcome@?????
welcome@12345  welcome@34567  welcome@65467  welcome@65678  welcome@65789

In the above examples, it didn't display one file because it had 6 character

Recursively search for the same

ls -R */welcome@?????
test/welcome@12345  test/welcome@34567  test/welcome@65467  test/welcome@65678  test/welcome@65789

c) Search using regex

Regex examples

:~/test$ ls | egrep '^welcome@[[:digit:]]{5}$'
welcome@12345
welcome@34567
welcome@65467
welcome@65678
welcome@65789
:~/test$ ls | egrep '^welcome@[[:digit:]]*$'
welcome@12345
welcome@34567
welcome@65467
welcome@65678
welcome@6578789
welcome@65789

21) Print the version of ls command

To print it, use --version parameter

$ ls --version

List version

That’s some parameters which can be used on day-to-day operation. Of course, you can always consult with ls manual page by typing man ls or ls --help on your console to explore more detail.

Pungki Arianto 8:00 pm

Comments

Your email address will not be published. Required fields are marked *

All comments are subject to moderation.

1 Comment

  1. Thanks for this. ls has 56 options, this is helpful in exploring some of them.
    I use a bash alias to select my favorite options as well as invoke a pager:
    /bin/ls -lgsI"1*" | less
    which works fine except if I invoke it with a parameter like
    ll test.txt
    which displays the content of test.txt instead of the directory listing.

    My solution is to have a second alias:
    /bin/ls -lgsI"1*"
    but it would be better to have one command that comes out of my fingeres easier.

    Suggestions?