best linux shell scripting course

Learn all about use of ls command | learn linux & shell scripting

Chapter: 3 Learn all about Linux ls command

The ls command is used to see the directories & files present in the current directory. We can also run the ls command on any path to check the content of any particular directory. Sharing the examples below:-

> ls  # display all files & folders in row manner 
folder1 folder2 file1 file2

To show the content of directory in a vertical list, use below command.

> ls -1 # display content in vertical list 
folder1
folder2
file1
file2

To check more details about all the content of a folder/directory. Use ls in long form. It will show file permissions, the owner of the file, the group to which it belongs, the date the file was created, and the file size:

> ls -l  # long form
drwx——+   4 mtroview  staff   128 Feb 03  2023  folder1
drwx——+   4 mtroview  staff   128 Feb 03  2023  folder2
rwx——–+   4 mtroview  staff   1828 Feb 03  2023  file1
rwx——-+   4 mtroview  staff   1732 Feb 03  2023  file2

To display the content in human-readable long form use the below command.
Note : human-readable means bytes will be rounded to kilobytes, gigabytes, etc.

> ls -hl  # long form, human readable

To display the folder in time-sorted format along with human-readable & long form. Use the below command:-

> ls -hlt # long form, human readable, sorted by time

To list all content including hidden/dotfiles in Linux with long-form, use the below command 

> ls -al  # list all, including dot- files and dirs

Regex can also be used while listing directory/files like below

>  ls *.txt   # it will show all files with .txt extension

In a shell script, ls can be useful in for-loops:

>  for i in $( ls /some/path/*.txt ); do echo $i; done

The above one will print the file name one by one which has a .txt extension

You can also list multiple different directories in a below way

> ls dir1  dir2  dir3

This will display the content from all mentioned directories at a time. This method is known as globbing.

Chapter: 4 Coming soon…. In next chapter will learn about cd command

1 thought on “Learn all about use of ls command | learn linux & shell scripting”

  1. Pingback: Best way to learn Linux & Shell scripting | Introduction CH-2 - MtroView

Leave a Comment

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