Basic Usage of ‘ls’ Command in Kali Linux With Examples

One of the most used Kali Linux Command ‘ls’ used by mostly every person in Kali Operating system. It is usually to view the content of current directory or targeted directory where you wanted to list the files in specific folder. This command not only work on Kali Linux it basically works on all Linux Distros and Mac OS X as well.

Basic Usage of 'ls' Command in Kali Linux With Examples

I Thought to made a tutorial of it,

1. ls Command – Basic Usage

The most basic use of any command is when it is used without any option or parameter. So, when ls command is executed with no option or parameter, then it will enlist files and sub-directories of current directory.

Syntax:

ls

Output:

Basic Usage of 'ls' Command in Kali Linux With Examples

 

2. A Long List

ls command when used with option -l (L in small caps), it will display the contents of the current directory in a long list format.

Syntax:

ls -l

Output:

Basic Usage of 'ls' Command in Kali Linux With Examples

In the output:

  • 1st column will give detailed information regarding file permission,
  • 2nd column will tell you about the number of links to the file,
  • 3rd and 4th columns are associated with owner and group of the file,
  • 5th column will be displaying the size of the file in bytes,
  • 6th column will display the recent time and date at which the file was modified,
  • and the last and 7th column is the actual file/directory name.

3. Displaying Size in Human Readable Format

Sometimes it is very confusing to read the size of the files in terms of bytes, especially when the file size is very large. So, instead of printing the file size in terms of bytes, we can display it in human readable form, like MB, GB, TB and so on. This can be achieved when ls command is executed with the option -h as follows:

Syntax:

ls -lh

Output:

4. Sort According to File Size You can sort the ls command output such that largest file (according to size) will be displayed first and smallest one at the last and this can be done with the use of option -S. Syntax:

 

4. Sort According to File Size

You can sort the ls command output such that largest file (according to size) will be displayed first and smallest one at the last and this can be done with the use of option -S.

Syntax:

ls -lhS

Output:

Basic Usage of 'ls' Command in Kali Linux With Examples

 

5. Determine the Block Size

If you wish to display the size of all the files in a specific human readable format, you can do it by using following syntax:

Syntax:

ls -l –block-size=[SIZE]

The [SIZE] in above syntax can be replaced by following letters in order to scale the size as per your requirement:
  • K = Kilobyte
  • M = Megabyte
  • G = Gigabyte
  • T = Terabyte
  • P = Petabyte
  • E = Exabyte
  • Z = Zettabyte
  • Y = Yottabyte

 

Output:

In the above example, I have scaled the file size to KB and to do so, I have replaced the [SIZE] thing in the syntax with letter 'K'.

In the above example, I have scaled the file size to KB and to do so, I have replaced the [SIZE] thing in the syntax with letter ‘K’.

6. List the Hidden Files too

In Linux, the files can be hidden by starting a file name with a “.” (dot) symbol. You can list these hidden files by the use of option -a with ls command.

Syntax:

ls -a

Output:

Basic Usage of 'ls' Command in Kali Linux With Examples

7. Show Me Only Sub-Directories

If you wish to exclude all the files and display only sub-directories, then you can use option -d as follows:

Syntax:

ls */

Output:

Basic Usage of 'ls' Command in Kali Linux With Examples

 

8. Don’t Display Owner and Group Info

You can exclude the columns providing Owner and Group information by using options -g and -G respectively.

Syntax:

ls -g

Output:

Basic Usage of 'ls' Command in Kali Linux With Examples

9. Display User ID and Group ID

When ls command is run with option -n, it will print UID and GID of the owner and group instead of their names.

Syntax:

ls -n

Output:

9. Display User ID and Group ID When ls command is run with option -n, it will print UID and GID of the owner and group instead of their names. Syntax:

10. Color Changes

This will simply colorize and de-colorize the list when the [VALUE] in the syntax given below is replaced by proper values. You can de-colorize it by replacing [VALUE] by never and colorize it by replacing it with auto.

Syntax:

ls –color=never

Output:

ls -color=[VALUE] ls command tutorial

11. Give Me INODE Numbers

When the ls command is complemented by option -i, it will print the Inode (Index) Numbers of the files in the first column.

Syntax:

ls -li

Output:

Basic Usage of 'ls' Command in Kali Linux With Examples

12. Marking the Directories with a Slash (/) Sign

This is to make identifications of the directories easier.

Syntax:

ls -p

Output:

12. Marking the Directories with a Slash (/) Sign This is to make identifications of the directories easier. Syntax:

13. Sort in Reverse Order

This is for sorting the results in reverse order.

Syntax:

ls -r

Output:

ls thehacktoday commands series

14. Show the Contents of Sub-Directories

ls command when used with option -R, it will display the contents of the sub-directories too.

Syntax:

ls -R

Output:

14. Show the Contents of Sub-Directories

15. Sort by Extensions

With the option -X, you can group the files with same extensions together.

Syntax:

ls -lX

Output:

15. Sort by Extensions

16. Sort by Modification Time

You can sort the output according to the time of modification. Recently modified file will be displayed first.

Syntax:
ls -lt

17. Listing the Contents of Home Directory

 

Syntax:

ls  ~

18. Listing the Contents of Parent Directory

Syntax:

ls ../

19. Check out the Version Details

 

Syntax:

ls –version
That’s all about ‘ls’ command in Kali Linux for now!

Back to top button
Close