iT邦幫忙

0

35 ls Command Examples in Linux (The Complete Guide)

  • 分享至 

  • xImage
  •  

Do you know how to list contents (files/directories) in Linux?

It’s straightforward.

In this article, you will learn what the ls command and its use is.

Promise me, you will read this article entirely, and I promise you that the concept of your ls command will be cleared till the end of this article.

In my opinion, if someone starts learning Linux, the first command he should learn is ls command.

What is the use of ls command?

In simple language, ls command helps to list the contents of a directory in Linux with useful information.

ls command provides the following information’s:

Permission details

  • Content owner and group information
  • Modified Date and Time
  • Size (Normally ls lists sizes in Bytes, but we can also check the size in Human Readable format)
  • Inode information and so on

Note: What we call a folder in Microsoft Windows is called a directory in Linux.

We usually use Microsoft Windows, and we are experts in it, that’s why I compare my Linux articles with Windows similar features so that your concept becomes clear.

What will you do if you want to list the contents of any directory in Microsoft Windows?

Just double click on the folder whose contents you want to list, and its contents will be listed in front of you because Windows is a GUI (Graphical User Interface) based operating system.

Similarly, since Linux provides us with a Command Line Interface (CLI), we have to use the ls command to see what is inside a directory.

ls was initially introduced in the Unix operating system, and as Linux is made up of the Unix OS, we are using this command in Linux.

How to remember Linux commands?
Often people ask how to remember Linux commands easily. I have an excellent trick to remember these commands.

Here it is:

Most of the commands of Linux are derived from ordinary words. Hence this command can be easily remembered. Here some examples:

ls command came from the Word “List.“
mkdir command came from the phrase “Make Directory.“
rmdir command came from the phrase “Remove Directory.“
du command came from the phrase “Disk Usage.“
pwd command came from the phrase “Print Working Directory” and so on…

So, let’s try to understand ls commands and all its options with example.

Every command of Linux comes with many options, and by using these options, we can use the complete features of that command.

Here ls is the core command, and -l is the option.

Note: You can also address the option as a flag.

Syntax:


You have to follow the below syntax to use ls command.

ls [OPTION]... [FILE]...

1. Simple listing of Files & Directories

Run the following command to list the files and directories of the current directory in a simple format.

You can check your current directory using the pwd command.

By default, it lists the contents in alphabetical order.

Output:

~$ ls
Desktop  Documents  Downloads  examples.desktop  Music  Pictures  Public  Templates  Videos

What you see in blue is the directory, and What you see in white is the file.

Almost all Linux distributions have this similar color pattern.

2. Long listing of Files and Directories

You can use the -l (It’s lowercase L) option to list contents in a long listing format.

Output:

~$ ls -l
total 44
drwxr-xr-x 2 ubuntu ubuntu 4096 Apr 28 05:33 Desktop
drwxr-xr-x 2 ubuntu ubuntu 4096 Apr 28 05:25 Documents
drwxr-xr-x 2 ubuntu ubuntu 4096 Apr 28 05:25 Downloads
-rw-r--r-- 1 ubuntu ubuntu 8980 Apr 28 05:18 examples.desktop
drwxr-xr-x 2 ubuntu ubuntu 4096 Apr 28 05:25 Music
drwxr-xr-x 2 ubuntu ubuntu 4096 Apr 28 05:25 Pictures
drwxr-xr-x 2 ubuntu ubuntu 4096 Apr 28 05:25 Public
drwxr-xr-x 2 ubuntu ubuntu 4096 Apr 28 05:25 Templates
drwxr-xr-x 2 ubuntu ubuntu 4096 Apr 28 05:25 Videos

As you can see, the output is divided into Seven columns with a lot of useful information related to files and directories.

So let’s try to understand one by one.

Column #1: First, we will understand the permission section :

The first character indicates the file type. Linux has many file types. Refer to the list below:
Edit
File Type Explanation
- Regular File
d Directory
n Network File
l Symbolic Link
c Character Special File
s Socket
b Block Special File
p Pipe (FIFO)Showing 1 to 8 of 8 entries

In Linux, permission has been divided into nine characters, which are distributed in User, Group, and others.

The first three characters belong to the Owner (---), Next three characters belong to Group (---), and Finally, the last three characters belong to Others (---). Refer to the snapshot above.

Let me tell you a little bit about how to recognize permissions. Like any other operating system, we have three types of permission in Linux :

r – Read
w – Write
x – Execute

Now that we have understood the file types and permissions, let’s move toward other information that we got in a long listing of contents and try to understand them.

Column #2: Hard links belong to the file.
Column #3: Owner of the file/directory
Column #4: Group to which the file/directory belongs
Column #5: Size (By Default prints in Bytes but we can display them in a human readable format)
Column #6: Last Modified Month, Date & Time
Column #7: Name of Files & Directories
You can also use the following command to long listing of contents.

~$ ll

This is an alias of the ls -l command, and by default, you will find this in every Linux distribution.

Read more at Edumotivation


圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言