LS Command Complete Tutorial

Ls Command in Linux/Unix with Examples(Complete details)

 

When You start learning and using Linux and Unix Operating system. You will understand that the LS command is most frequently used in Linux and Unix systems. 

What you will learn after reading the article

  • Syntax
  • What does ls command mean
  • Usage of ls command 
  • Parameters can be used with ls command?
  • ls command examples in Unix and linux
  • ls command parameters summary.

ls command : syntax

Unix/Linux are case sensitive operation systems. ls command always used and worked only in lowercase letters.

Syntax: ls paramaters

ls Command : ls command is used listing of files and directories .

What does ls Command mean?

ls means: Listing

ls command, It is used in listing of files and directories in unix and linux systems. 

you can use the command standalone also with other parameters like -l, lrt, a which will give your desires output.  

What is usage of ls command and what parameters can be used with it?

ls command used to list the file and directories, Also you can get the details of these files and directories using different parameters

You can use different parameters with ls command.

Examples:

  • ls -l : Long listing
  • ls -a : Hidden files
  • ls -lrt : sort and long listing
  • ls -p : directory 
  • ls -lh : human readable format 

ls command examples in unix and linux systems

ls command Example 1:

How to list all files and directories from your current directory?

Command: ls

You need to run the ls without parameters, It will display all the files and directories of your current path.

Note: It will not display the other details.

ls command Example

If you see above output, you will see it displays all the files and directories.

  • Blue color shows: It is directory.
  • White color : These are files. 

ls command Example 2:

How to list all files and directories from your current directory with other details as well like size, date?

Command: ls -l

You need to run the ls with -l (lowercase l) parameters, It will display all the files and directories of your current path with size, date and permission details.

Output Explanation:

-rw-r–r– 1 91989 197609 234 Jul 13 22:00 log.txt

  • -rw-r–r– : Permissions
  • 91989: Ownership of file
  • 234 : size in bytes
  • Jul 13 : Date
  • 22:00 : Time
  • log.txt : File Name

ls command Example 3:

How to check the size of file in human readable format, which is kb, mb and other?

Command: ls -lh

If there are multiple files, and then the bytes can be confusing .You need to run the ls with -lh (lowercase l) parameters, It will display the size of files and directories in kb, mb and other details.

Note: There is another command, that is ls -si which divide the bytes by 1000 and in kbs, that will divide by 1024. 

ls command Example 4:

How to sort the files according to size?

Command: ls -lhS

here S in uppercase letter, It will sort all the files and size from decreasing to increasing.

Biggest file display first and smallest file last.

-rw-r–r– 1 91989 197609 234 Jul 13 22:00 log.txt
-rw-r–r– 1 91989 197609 89 Jul 13 21:42 log3.txt
-rw-r–r– 1 91989 197609 35 Jul 13 21:42 log2.txt
-rw-r–r– 1 91989 197609 27 Jul 13 21:41 log1.txt
drwxr-xr-x 1 91989 197609 0 Jul 25 15:09 example1/
drwxr-xr-x 1 91989 197609 0 Jul 25 15:09 thinkheights/
-rw-r–r– 1 91989 197609 0 Jul 13 21:41 Unix

ls command Example 5:

How to scale the size according to prefereable size format?

for kb format:

Command: ls -l –block-size=k 

For other format:

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

ls command example

It displayed size of files in kb format.

If ypu want to dipslay in others like megabyte, gigabyte.

Need to use M, G and others.

ls command Example 6:

How to display the hidden files?

In Unix and Linux systems. files tarting with . are hidden files.

Command: ls -a

-a parameter will display all the hidden files.

In the above example

.unix and .world are hidden files. It displayed only with -a parameter.

ls command Example 7:

How to display the file name and file size only in output?

Command: ls -s -h

ls command example

It display online file name and size, Not other details.

ls command Example 8:

How to list only directories?

If we want to list only directories, we will use -d parameter

Command: ls -d */

ls command example

In output, you will get only directories. No files, No hidden files

ls command Example 9:

How to get list without owner information?

If we want to list without owner permissions  we need to use g parameter alomg with l

Command: ls -lg

ls command example

In output, you will got all information, but it omits owner information. 

ls command Example 10:

How to get list without group information?

If we want to list without group information we need to use G (in Uppercase) parameter alomg with l

Command: ls -lG

ls command example

In output, you will got all information, but it omits group information. 

ls command Example 11:

How to print UID and GID?

Command: ls -ln

If we use n parameter alsong with l parameter. like -ln, It displays the UID and GID of owner and group owner.

ls command Example 12:

How to print the index number of files.

For getting inex numbers, you will need to use i parameter along with l that is li.

Command : ls -li 

ls command example

ls command Example 13:

How to add / to mark directories?

For marking directories by / you can us parameter p.

Command : ls -p 

ls command Example 14:

How to do sorting in reverse order?

You should simply use parameter -r for reverse order sorting.

Command : ls -r 

ls command Example 15:

How to list sub-directories recursively?

You should simply use parameter -R for get the details of directory recursively.

Command : ls -R 

ls command Example 16:

How can you sort the files according to file type extension?

You can sort the file types (according to extension) parameter -X along with l .

Command : ls -lX 

ls command example

ls command Example 17:

How can you sort the files according to modification time?

You can sort the files and directories (according to modification time) parameter -t along with l .

Command : ls -lt 

ls command example

ls command Example 18:

How to list you home directory?

You can list the files and directories  of your home directory using ~ parameter 

Command : ls ~

ls command Example 19:

How to list the files and directories of your parent directory?

You can list the files and directories  of your parent directory with below command 

Command : ls ../

two level up : ls ../../

Also, you can get the list of files and directories of specific path as well 

ls path

ls command Example 20:

How to get the version of ls command?

Command : ls –version

The above command display the ls version.

ls command parameters Summary

Command Use
ls list without parameters
ls -l long listing of files/directories
ls -lh listing human readable format
ls -si listing in power of 1000 not 1024
ls -lhs listing and sorting file according to size
 ls -l –block-size=M listig file in mb size of files
ls -a listing hidden files
ls -d */ listing only direcories
ls -lg listing without owner information
ls -lG listing without group information
ls -ln getting UID and GID
ls –color = never listing files and directories witout color
ls -li listing inode number
ls -p for marking directory
ls -r reverse order sorting
ls -R list sub directories recursively
ls -lX or ls -l –sort-extension sort by extension
ls -lt list by modification time
 ls ~ list the home directory
ls ../ list the parent directory
$ ls ../../ 2 level up listing
ls –version ls command version

Leave a Comment

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.