35 ls Command Examples in Linux (The Complete Guide)
Published: 2023-03-17 12:57:22

<!-- wp:paragraph -->
<p>Do you know how to list contents (files/directories) in Linux?</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>It's straightforward.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>In this article, you will learn what the <code>ls</code> command and its use is.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>Promise me, you will read this article entirely, and I promise you that the concept of your <code>ls</code> command will be cleared till the end of this article.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>In my opinion, if someone starts learning Linux, the first command he should learn is&nbsp;<code>ls</code> command.</p>
<!-- /wp:paragraph -->

<!-- wp:heading {"level":3} -->
<h3>What is the use of ls command?</h3>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>In simple language, <code>ls</code> command helps to list the contents of a directory in Linux with useful information.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p><strong><em>ls command</em>&nbsp;provides the following information's:</strong></p>
<!-- /wp:paragraph -->

<!-- wp:list -->
<ul><!-- wp:list-item -->
<li>Permission details</li>
<!-- /wp:list-item -->

<!-- wp:list-item -->
<li>Content owner and group information</li>
<!-- /wp:list-item -->

<!-- wp:list-item -->
<li>Modified Date and Time</li>
<!-- /wp:list-item -->

<!-- wp:list-item -->
<li>Size (Normally <code>ls</code> lists sizes in Bytes, but we can also check the size in Human Readable format)</li>
<!-- /wp:list-item -->

<!-- wp:list-item -->
<li>Inode information and so on</li>
<!-- /wp:list-item --></ul>
<!-- /wp:list -->

<!-- wp:paragraph -->
<p><strong>Note:</strong><em> What we call a folder in Microsoft Windows is called a directory in Linux.</em></p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>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.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>What will you do if you want to list the contents of any directory in Microsoft Windows?</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>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.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>Similarly, since Linux provides us with a Command Line Interface (CLI), we have to use the&nbsp;<code>ls</code> command&nbsp;to see what is inside a directory.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p><code>ls</code> 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.</p>
<!-- /wp:paragraph -->

<!-- wp:heading {"level":3} -->
<h3>How to remember Linux commands?</h3>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>Often people ask how to remember Linux commands easily. I have an excellent trick to remember these commands.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>Here it is:</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>Most of the commands of Linux are derived from ordinary words. Hence this command can be easily remembered. Here some examples:</p>
<!-- /wp:paragraph -->

<!-- wp:list -->
<ul><!-- wp:list-item -->
<li><code>ls</code> command came from the Word "<strong>List.</strong>"</li>
<!-- /wp:list-item -->

<!-- wp:list-item -->
<li><code>mkdir</code> command came from the phrase "<strong>Make Directory.</strong>"</li>
<!-- /wp:list-item -->

<!-- wp:list-item -->
<li><code>rmdir</code> command came from the phrase "<strong>Remove Directory.</strong>"</li>
<!-- /wp:list-item -->

<!-- wp:list-item -->
<li><code>du</code> command came from the phrase "<strong>Disk Usage.</strong>"</li>
<!-- /wp:list-item -->

<!-- wp:list-item -->
<li><code>pwd</code> command came from the phrase "<strong>Print Working Directory</strong>" and so on...</li>
<!-- /wp:list-item --></ul>
<!-- /wp:list -->

<!-- wp:paragraph -->
<p><strong>Suggested Read:</strong></p>
<!-- /wp:paragraph -->

<!-- wp:list -->
<ul><!-- wp:list-item -->
<li><span style="text-decoration: underline;"><a href="https://edumotivation.com/mkdir-command/" target="_blank" rel="noreferrer noopener">How to create a Directory in Linux</a></span></li>
<!-- /wp:list-item -->

<!-- wp:list-item -->
<li><span style="text-decoration: underline;"><a href="https://edumotivation.com/how-to-delete-a-directory-in-linux/" target="_blank" rel="noreferrer noopener">How to delete a directory in Linux</a></span></li>
<!-- /wp:list-item --></ul>
<!-- /wp:list -->

<!-- wp:paragraph -->
<p>So, let's try to understand&nbsp;<code>ls</code> commands&nbsp;and all its options with example.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>Every command of Linux comes with many options, and by using these options, we can use the complete features of that command.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>In the table below, you will find all the options related to this command.</p>
<!-- /wp:paragraph -->

<!-- wp:shortcode -->
[table id=6 /]
<!-- /wp:shortcode -->

<!-- wp:paragraph -->
<p>You can use these options with its core command with the help of hyphen (<code>-</code>).</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p><strong>For example:</strong></p>
<!-- /wp:paragraph -->

<!-- wp:html -->
<pre><code>~$ ls -l</code></pre>
<!-- /wp:html -->

<!-- wp:paragraph -->
<p>Here&nbsp;<code>ls</code>&nbsp;is the core command, and&nbsp;<code>-l</code>&nbsp;is the option.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p><strong>Note:</strong>&nbsp;<em>You can also address the option as a flag</em>.</p>
<!-- /wp:paragraph -->

<!-- wp:heading {"level":3} -->
<h3>Syntax:</h3>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>You have to follow the below syntax to use <code>ls</code> command.</p>
<!-- /wp:paragraph -->

<!-- wp:html -->
<pre><code>ls [OPTION]... [FILE]...</code></pre>
<!-- /wp:html -->

<!-- wp:heading -->
<h2>1. Simple listing of Files &amp; Directories</h2>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>Run the following command to list the files and directories of the current directory in a simple format.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>You can check your current directory using the&nbsp;<code>pwd</code>&nbsp;command.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>By default, it lists the contents in alphabetical order.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p><strong>Output:</strong></p>
<!-- /wp:paragraph -->

<!-- wp:html -->
<pre><code>~$ ls
Desktop  Documents  Downloads  examples.desktop  Music  Pictures  Public  Templates  Videos</code></pre>
<!-- /wp:html -->

<!-- wp:image {"id":354,"sizeSlug":"large"} -->
<figure class="wp-block-image size-large"><img src="https://edumotivation.com/wp-content/uploads/2020/06/simple-listing-of-the-contents.png" alt="Simple listing of files &amp; directories" class="wp-image-354"/></figure>
<!-- /wp:image -->

<!-- wp:paragraph -->
<p>What you see in blue is the&nbsp;<strong>directory</strong>, and What you see in white is the&nbsp;<strong>file</strong>.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>Almost all Linux distributions have this similar color pattern.</p>
<!-- /wp:paragraph -->

<!-- wp:heading -->
<h2>2. Long listing of Files and Directories</h2>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>You can use the <code>-l</code> (<strong>It's lowercase L</strong>) option to list contents in a long listing format.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p><strong>Output:</strong></p>
<!-- /wp:paragraph -->

<!-- wp:html -->
<pre><code>~$ 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</code></pre>
<!-- /wp:html -->

<!-- wp:image {"id":391,"sizeSlug":"large"} -->
<figure class="wp-block-image size-large"><img src="https://edumotivation.com/wp-content/uploads/2020/06/long-listing-of-contents_1-1024x567.png" alt="Long listing of Contents" class="wp-image-391"/></figure>
<!-- /wp:image -->

<!-- wp:paragraph -->
<p>As you can see, the output is divided into Seven columns with a lot of useful information related to files and directories.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>So let's try to understand one by one.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p><strong>Column #1</strong>: First, we will understand the permission section : </p>
<!-- /wp:paragraph -->

<!-- wp:image {"id":344,"sizeSlug":"large"} -->
<figure class="wp-block-image size-large"><img src="https://edumotivation.com/wp-content/uploads/2020/06/permissions.png" alt="" class="wp-image-344"/></figure>
<!-- /wp:image -->

<!-- wp:list -->
<ul><!-- wp:list-item -->
<li>The first character indicates the file type. Linux has many file types. Refer to the list below:</li>
<!-- /wp:list-item --></ul>
<!-- /wp:list -->

<!-- wp:shortcode -->
[table id=7 /]
<!-- /wp:shortcode -->

<!-- wp:list -->
<ul><!-- wp:list-item -->
<li>In Linux, permission has been divided into nine characters, which are distributed in User, Group, and others.</li>
<!-- /wp:list-item --></ul>
<!-- /wp:list -->

<!-- wp:paragraph -->
<p>The first three characters belong to the <strong>Owner</strong> <code>(---)</code>, Next three characters belong to <strong>Group</strong> <code>(---)</code>, and Finally, the last three characters belong to <strong>Others</strong> <code>(---)</code>. Refer to the snapshot above.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>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 :</p>
<!-- /wp:paragraph -->

<!-- wp:list -->
<ul><!-- wp:list-item -->
<li><code>r</code> - Read</li>
<!-- /wp:list-item -->

<!-- wp:list-item -->
<li><code>w</code> - Write</li>
<!-- /wp:list-item -->

<!-- wp:list-item -->
<li><code>x</code> - Execute</li>
<!-- /wp:list-item --></ul>
<!-- /wp:list -->

<!-- wp:paragraph -->
<p><strong>Read Also:</strong></p>
<!-- /wp:paragraph -->

<!-- wp:list -->
<ul><!-- wp:list-item -->
<li><span style="text-decoration: underline;"><a href="https://edumotivation.com/understanding-linux-file-permissions/" target="_blank" rel="noreferrer noopener">chmod command: Understanding Linux File Permissions</a></span></li>
<!-- /wp:list-item -->

<!-- wp:list-item -->
<li><span style="text-decoration: underline;"><a href="https://edumotivation.com/special-file-permissions-in-linux/" target="_blank" rel="noreferrer noopener">Special File Permissions in Linux (SUID, SGID and Sticky Bit)</a></span></li>
<!-- /wp:list-item --></ul>
<!-- /wp:list -->

<!-- wp:paragraph -->
<p>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.</p>
<!-- /wp:paragraph -->

<!-- wp:list -->
<ul><!-- wp:list-item -->
<li><strong>Column #2</strong>: Hard links belong to the file.</li>
<!-- /wp:list-item -->

<!-- wp:list-item -->
<li><strong>Column #3</strong>: Owner of the file/directory</li>
<!-- /wp:list-item -->

<!-- wp:list-item -->
<li><strong>Column #4</strong>: Group to which the file/directory belongs</li>
<!-- /wp:list-item -->

<!-- wp:list-item -->
<li><strong>Column #5</strong>: Size (By Default prints in Bytes but we can display them in a human readable format)</li>
<!-- /wp:list-item -->

<!-- wp:list-item -->
<li><strong>Column #6</strong>: Last Modified Month, Date &amp; Time</li>
<!-- /wp:list-item -->

<!-- wp:list-item -->
<li><strong>Column #7</strong>: Name of Files &amp; Directories</li>
<!-- /wp:list-item --></ul>
<!-- /wp:list -->

<!-- wp:paragraph -->
<p>You can also use the following command to long listing of contents.</p>
<!-- /wp:paragraph -->

<!-- wp:html -->
<pre><code>~$ ll</code></pre>
<!-- /wp:html -->

<!-- wp:paragraph -->
<p>This is an alias of the <code>ls -l</code> command, and by default, you will find this in every Linux distribution.</p>
<!-- /wp:paragraph -->

<!-- wp:image {"id":405,"sizeSlug":"large"} -->
<figure class="wp-block-image size-large"><img src="https://edumotivation.com/wp-content/uploads/2020/06/alias-of-ls-command.png" alt="Alias of ls Command" class="wp-image-405"/></figure>
<!-- /wp:image -->

<!-- wp:heading -->
<h2>3. List authors of the contents</h2>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>Now, as you know that using <code>-l</code> option we can list owner and group details. </p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>There are many chances that the owner and the author can be the same.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>I thought it necessary to include it in this guide because it is a feature of <code>ls</code>.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>So to list the authors of the content you can use <code>--author</code> option. You have to use this option with the combination <code>-l</code>.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p><strong>Output:</strong></p>
<!-- /wp:paragraph -->

<!-- wp:html -->
<pre><code>~$ ls -l --author
total 52
drwxr-xr-x 4 ubuntu ubuntu ubuntu 4096 Jun  4 23:46 data
drwxr-xr-x 2 ubuntu ubuntu ubuntu 4096 Jun  4 05:39 data1
drwxr-xr-x 2 ubuntu ubuntu ubuntu 4096 Apr 28 05:33 Desktop
drwxr-xr-x 2 ubuntu ubuntu ubuntu 4096 Apr 28 05:25 Documents
drwxr-xr-x 2 ubuntu ubuntu ubuntu 4096 Apr 28 05:25 Downloads
-rw-r--r-- 1 ubuntu ubuntu ubuntu 8980 Apr 28 05:18 examples.desktop
-rw-r--r-- 1 ubuntu ubuntu ubuntu    0 Jun  4 22:19 file.txt
drwxr-xr-x 2 ubuntu ubuntu ubuntu 4096 Apr 28 05:25 Music
drwxr-xr-x 2 ubuntu ubuntu ubuntu 4096 Apr 28 05:25 Pictures
drwxr-xr-x 2 ubuntu ubuntu ubuntu 4096 Apr 28 05:25 Public
drwxr-xr-x 2 ubuntu ubuntu ubuntu 4096 Apr 28 05:25 Templates
drwxr-xr-x 2 ubuntu ubuntu ubuntu 4096 Apr 28 05:25 Videos</code></pre>
<!-- /wp:html -->

<!-- wp:image {"id":403,"sizeSlug":"large"} -->
<figure class="wp-block-image size-large"><img src="https://edumotivation.com/wp-content/uploads/2020/06/list-authors.png" alt="List Authors" class="wp-image-403"/></figure>
<!-- /wp:image -->

<!-- wp:heading -->
<h2>4. List contents of a specific Directory</h2>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>You can list the contents of a specific directory using <code>ls</code> command.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>To do so, pass the path of the directory to the <code>ls</code>.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>Here is an example:</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>I want to list the contents of <strong>Documents/data/</strong> directory.</p>
<!-- /wp:paragraph -->

<!-- wp:html -->
<pre><code>~$ ls -l Documents/data/
total 0
-rw-r--r-- 1 ubuntu ubuntu 0 Apr 28 06:44 test1.txt
-rw-r--r-- 1 ubuntu ubuntu 0 Apr 28 06:44 test2.txt
-rw-r--r-- 1 ubuntu ubuntu 0 Apr 28 06:44 test3.txt
-rw-r--r-- 1 ubuntu ubuntu 0 Apr 28 06:44 test4.txt
-rw-r--r-- 1 ubuntu ubuntu 0 Apr 28 06:44 test5.txt</code></pre>
<!-- /wp:html -->

<!-- wp:heading -->
<h2>5. List contents of multiple Directories</h2>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>This command is the same as the above one. </p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>The only difference is we have to mention the path of multiple directories instead of one.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>Let me show you an example.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>Here I want to list the contents of the following directories :</p>
<!-- /wp:paragraph -->

<!-- wp:list {"ordered":true} -->
<ol><!-- wp:list-item -->
<li>data1</li>
<!-- /wp:list-item -->

<!-- wp:list-item -->
<li>data2  </li>
<!-- /wp:list-item -->

<!-- wp:list-item -->
<li>data3</li>
<!-- /wp:list-item --></ol>
<!-- /wp:list -->

<!-- wp:paragraph -->
<p>So the command would be:</p>
<!-- /wp:paragraph -->

<!-- wp:html -->
<pre><code>~$ ls -l data1 data2 data3
data1:
total 0
-rw-r--r-- 1 ubuntu ubuntu 0 May  2 06:40 file1.txt
-rw-r--r-- 1 ubuntu ubuntu 0 May  2 06:40 file2.txt
-rw-r--r-- 1 ubuntu ubuntu 0 May  2 06:40 file3.txt
-rw-r--r-- 1 ubuntu ubuntu 0 May  2 06:40 file4.txt
-rw-r--r-- 1 ubuntu ubuntu 0 May  2 06:40 file5.txt

data2:
total 0
-rw-r--r-- 1 ubuntu ubuntu 0 May  2 06:40 test1.txt
-rw-r--r-- 1 ubuntu ubuntu 0 May  2 06:40 test2.txt
-rw-r--r-- 1 ubuntu ubuntu 0 May  2 06:40 test3.txt
-rw-r--r-- 1 ubuntu ubuntu 0 May  2 06:40 test4.txt
-rw-r--r-- 1 ubuntu ubuntu 0 May  2 06:40 test5.txt

data3:
total 0
-rw-r--r-- 1 ubuntu ubuntu 0 May  2 06:41 data1.txt
-rw-r--r-- 1 ubuntu ubuntu 0 May  2 06:41 data2.txt
-rw-r--r-- 1 ubuntu ubuntu 0 May  2 06:41 data3.txt
-rw-r--r-- 1 ubuntu ubuntu 0 May  2 06:41 data4.txt
-rw-r--r-- 1 ubuntu ubuntu 0 May  2 06:41 data5.txt</code></pre>
<!-- /wp:html -->

<!-- wp:heading -->
<h2>6. List contents of the Home Directory</h2>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>You don't have to mention the entire path of the home directory to list the contents.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>All you have to do is pass the Tilde (<code>~</code>) Special character to&nbsp;<code>ls</code> because Tilde (<code>~</code>) Indicates Home Directory in Linux.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p><strong>Example:</strong></p>
<!-- /wp:paragraph -->

<!-- wp:html -->
<pre><code>~$ ls -l ~
total 44
drwxr-xr-x 2 ubuntu ubuntu 4096 Apr 28 05:33 Desktop
drwxr-xr-x 3 ubuntu ubuntu 4096 Apr 28 06:44 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</code></pre>
<!-- /wp:html -->

<!-- wp:heading -->
<h2>7. List contents of Parent Directory</h2>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>Let me explain to you what is the parent and current directory.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>The directory within which the current directory exists is called the <strong>parent directory</strong>.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>The directory in which i am working now is called&nbsp;<strong>the current directory,</strong>&nbsp;also known as&nbsp;<strong>Working Directory</strong>.</p>
<!-- /wp:paragraph -->

<!-- wp:image {"id":350,"sizeSlug":"large"} -->
<figure class="wp-block-image size-large"><img src="https://edumotivation.com/wp-content/uploads/2020/06/Parent-Directory.png" alt="Parent Directory" class="wp-image-350"/></figure>
<!-- /wp:image -->

<!-- wp:paragraph -->
<p>In this example my current directory is <strong>/home/ubuntu/data</strong>.</p>
<!-- /wp:paragraph -->

<!-- wp:html -->
<pre><code>~/data$ pwd
/home/ubuntu/data
~/data$ ls
dir1  dir2  file1.txt  file2.txt  file3.txt  file4.txt  file5.txt</code></pre>
<!-- /wp:html -->

<!-- wp:paragraph -->
<p>But our purpose is to list the contents of the parent directory.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>In this case, its <strong>/home/ubuntu</strong>.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>To do so, type the following command.</p>
<!-- /wp:paragraph -->

<!-- wp:html -->
<pre><code>~/data$ ls ../
data   Desktop    Downloads         file1.txt  Music     Public     Videos
data1  Documents  examples.desktop  file.txt   Pictures  Templates</code></pre>
<!-- /wp:html -->

<!-- wp:paragraph -->
<p>You can also list the contents of <strong>2nd</strong> &amp; <strong>3rd</strong> level Parent Directories.</p>
<!-- /wp:paragraph -->

<!-- wp:html -->
<pre><code>ubuntu@ubuntu:~/data$ ls ../../
ubuntu
 
ubuntu@ubuntu:~/data$ ls ../../../
bin    dev   initrd.img      lib64       mnt   root  snap      sys  var
boot   etc   initrd.img.old  lost+found  opt   run   srv       tmp  vmlinuz
cdrom  home  lib             media       proc  sbin  swapfile  usr</code></pre>
<!-- /wp:html -->

<!-- wp:image {"id":353,"sizeSlug":"large"} -->
<figure class="wp-block-image size-large"><img src="https://edumotivation.com/wp-content/uploads/2020/06/Parent-Directory-Levels.png" alt="Parent Directory Levels" class="wp-image-353"/></figure>
<!-- /wp:image -->

<!-- wp:heading -->
<h2>8. List only files using ls command</h2>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>By default, <code>ls</code> lists all available contents of a requested directory, but you can list only files using the following command.</p>
<!-- /wp:paragraph -->

<!-- wp:html -->
<pre><code>~$ ls -l | egrep -v '^d'
total 80
-rw-r--r-- 1 ubuntu ubuntu  8980 Apr 28 05:18 examples.desktop
-rw-r--r-- 1 ubuntu ubuntu    23 May  1 07:18 file1
-rw-r--r-- 1 ubuntu ubuntu    21 May  1 07:01 file1.txtx
-rw-r--r-- 1 ubuntu ubuntu     0 May  1 06:56 file2</code></pre>
<!-- /wp:html -->

<!-- wp:heading -->
<h2>9. List only directories</h2>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>To list only directories instead of all contents, use the following commands.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p><strong>Method 1</strong>:</p>
<!-- /wp:paragraph -->

<!-- wp:html -->
<pre><code>ubuntu@ubuntu:~$ ls -l | grep "^d"
drwxr-xr-x 2 ubuntu ubuntu  4096 May  1 05:46 Desktop
drwxr-xr-x 2 ubuntu ubuntu  4096 Apr 29 21:19 docs
drwxr-xr-x 3 ubuntu ubuntu  4096 Apr 28 06:44 Documents
drwxr-xr-x 2 ubuntu ubuntu  4096 Apr 28 05:25 Downloads
drwxr-xr-x 2 ubuntu ubuntu  4096 Apr 28 05:25 Music
drwxr-xr-x 2 ubuntu ubuntu  4096 Apr 29 21:19 my
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 20480 May  1 07:13 test
drwxr-xr-x 2 ubuntu ubuntu  4096 Apr 28 05:25 Videos</code></pre>
<!-- /wp:html -->

<!-- wp:paragraph -->
<p><strong>Suggested Read:</strong> <span style="text-decoration: underline;"><a href="https://edumotivation.com/grep-command/" target="_blank" rel="noreferrer noopener">grep Command: Search for a pattern in a file</a></span></p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p><strong>Method 2</strong>:</p>
<!-- /wp:paragraph -->

<!-- wp:html -->
<pre><code>~$ ls -ld */</code></pre>
<!-- /wp:html -->

<!-- wp:heading -->
<h2>10. List hidden Files and Directories</h2>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>How to identify hidden files and directories in Linux?</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>It's Simple.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>In Linux, the file or directory that starts with a period (<code>.</code>) is hidden.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>So to list the Hidden contents use <code>-a</code> option with <code>ls</code>.</p>
<!-- /wp:paragraph -->

<!-- wp:html -->
<pre><code>~$ ls -a
.              .bashrc  docs              file1.txtx     .mozilla  .profile                   Templates
..             .cache   Documents         .gnupg         Music     Public                     Videos
.bash_history  .config  Downloads         .ICEauthority  my        .ssh
.bash_logout   Desktop  examples.desktop  .local         Pictures  .sudo_as_admin_successful</code></pre>
<!-- /wp:html -->

<!-- wp:paragraph -->
<p>As you can see, Implied is also listed in the above output.</p>
<!-- /wp:paragraph -->

<!-- wp:image {"id":358,"sizeSlug":"large"} -->
<figure class="wp-block-image size-large"><img src="https://edumotivation.com/wp-content/uploads/2020/06/Hidden-files-directories.png" alt="Hidden Files &amp; Directories with Implied" class="wp-image-358"/></figure>
<!-- /wp:image -->

<!-- wp:list -->
<ul><!-- wp:list-item -->
<li><code>.</code> - for Current Directory</li>
<!-- /wp:list-item -->

<!-- wp:list-item -->
<li><code>..</code>- for Parent Directory</li>
<!-- /wp:list-item --></ul>
<!-- /wp:list -->

<!-- wp:paragraph -->
<p>If you don't want to list Implied pass the <code>-A</code> option to <code>ls</code>.</p>
<!-- /wp:paragraph -->

<!-- wp:html -->
<pre><code>~$ ls -A
.bash_history  data       examples.desktop  .local    Public
.bash_logout   data1      file1.txt         .mozilla  .ssh
.bashrc        Desktop    file.txt          Music     .sudo_as_admin_successful
.cache         Documents  .gnupg            Pictures  Templates
.config        Downloads  .ICEauthority     .profile  Videos</code></pre>
<!-- /wp:html -->

<!-- wp:heading -->
<h2>11. Recursively list the contents of Directories and Sub-Directories</h2>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>What is Recursive?</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>Recursive means the executed command in Linux will work on a complete directory tree.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>As per the following scenario, if I run the <code>ls</code> command to list the contents of the directory <strong>dir1</strong> recursively, it will display the contents of all three directories (<strong>dir1</strong>, <strong>dir2</strong> &amp; <strong>dir3</strong>).</p>
<!-- /wp:paragraph -->

<!-- wp:image {"align":"center","id":365,"sizeSlug":"large"} -->
<figure class="wp-block-image aligncenter size-large"><img src="https://edumotivation.com/wp-content/uploads/2020/06/directory-tree.png" alt="Directory Tree" class="wp-image-365"/></figure>
<!-- /wp:image -->

<!-- wp:paragraph -->
<p>To list contents, recursively pass the <code>-R</code> option to <code>ls</code>. </p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p><strong>Example:</strong></p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>Here I am listing the contents of the directory <strong>data</strong>, which has two sub-directories i.e., <strong>dir1</strong> &amp; <strong>dir2</strong>.</p>
<!-- /wp:paragraph -->

<!-- wp:html -->
<pre><code>~$ ls -R data
data:
dir1  dir2  file1.txt  file2.txt  file3.txt  file4.txt  file5.txt

data/dir1:
f1.txt  f2.txt  f3.txt  f4.txt  f5.txt

data/dir2:
note1.txt  note2.txt  note3.txt  note4.txt  note5.txt</code></pre>
<!-- /wp:html -->

<!-- wp:image {"id":378,"sizeSlug":"large"} -->
<figure class="wp-block-image size-large"><img src="https://edumotivation.com/wp-content/uploads/2020/06/list-content-recursively_1.png" alt="List contents Recursively" class="wp-image-378"/></figure>
<!-- /wp:image -->

<!-- wp:heading -->
<h2>12. Display one File/Directory per line</h2>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>Use the following command to list one file/directory per line.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>Note: <code>ls -1</code>(<strong>It's Numeric One</strong>)</p>
<!-- /wp:paragraph -->

<!-- wp:html -->
<pre><code>~$ ls -1
Desktop
Documents
Downloads
examples.desktop
Music
Pictures
Public
Templates
Videos</code></pre>
<!-- /wp:html -->

<!-- wp:heading -->
<h2>13. List directory themselves and not their content</h2>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>To list the directories themselves instead of their content, pass the <code>-d</code> option to <code>ls</code>.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>Refer to the following command.</p>
<!-- /wp:paragraph -->

<!-- wp:html -->
<pre><code>~$ ls -ld data
drwxr-xr-x 4 ubuntu ubuntu 4096 Jun  4 05:25 data</code></pre>
<!-- /wp:html -->

<!-- wp:heading -->
<h2>14. Display size in Human Readable format</h2>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>Usually, <code>ls</code> command displays the size of the file or directory in Bytes, which is difficult to understand.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>But if it displays the size of the file in Human Readable Format (GB, MB, KB), then it will be easy to understand, which is possible with the help of ls command.</p>
<!-- /wp:paragraph -->

<!-- wp:list -->
<ul><!-- wp:list-item -->
<li>KB (K) - Kilo Bytes</li>
<!-- /wp:list-item -->

<!-- wp:list-item -->
<li>MB (M) - Mega Bytes</li>
<!-- /wp:list-item -->

<!-- wp:list-item -->
<li>GB (G) - Giga Bytes</li>
<!-- /wp:list-item --></ul>
<!-- /wp:list -->

<!-- wp:paragraph -->
<p>To print the size in Human readable format, use the <code>-h</code> option. You have to use this option with the combination <code>-l</code>.</p>
<!-- /wp:paragraph -->

<!-- wp:html -->
<pre><code>~$ ls -lh Documents/data/
total 40K
-rw-r--r-- 1 ubuntu ubuntu 1.4K Apr 28 06:51 rsyslog.conf
-rw-r--r-- 1 ubuntu ubuntu  19K Apr 28 06:51 services
-rw-r--r-- 1 ubuntu ubuntu 2.7K Apr 28 06:51 sysctl.conf
-rw-r--r-- 1 ubuntu ubuntu 1.3K Apr 28 06:51 ucf.conf
-rw-r--r-- 1 ubuntu ubuntu 4.9K Apr 28 06:51 wgetrc</code></pre>
<!-- /wp:html -->

<!-- wp:heading -->
<h2>15. Print the allocated size of each File &amp; Directory</h2>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>Usually, <code>ls</code> command prints the file size in a long listing format, but if you want to display the size in the simple listing of contents, use the <code>-s</code> option.</p>
<!-- /wp:paragraph -->

<!-- wp:html -->
<pre><code>~$ ls -hs
total 56K
4.0K Desktop  4.0K Documents   12K examples.desktop  4.0K Music  4.0K Pictures  4.0K Templates
4.0K docs     4.0K Downloads  4.0K file1.txtx        4.0K my     4.0K Public    4.0K Videos</code></pre>
<!-- /wp:html -->

<!-- wp:heading -->
<h2>16. List files &amp; directories with different indicators at the end</h2>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>What  is the use of Indicators?</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>Indicators are used to identify the file type. Here is the list of different types of indicators:</p>
<!-- /wp:paragraph -->

<!-- wp:list -->
<ul><!-- wp:list-item -->
<li><code>/</code>- Directory</li>
<!-- /wp:list-item -->

<!-- wp:list-item -->
<li><code>|</code>- Pipe</li>
<!-- /wp:list-item -->

<!-- wp:list-item -->
<li><code>@</code>- Symlink.</li>
<!-- /wp:list-item -->

<!-- wp:list-item -->
<li><code>*</code>- Executable files</li>
<!-- /wp:list-item -->

<!-- wp:list-item -->
<li><code>=</code>- socket.</li>
<!-- /wp:list-item --></ul>
<!-- /wp:list -->

<!-- wp:paragraph -->
<p>Use the <code>-F</code> option to append indicator to the entries.</p>
<!-- /wp:paragraph -->

<!-- wp:html -->
<pre><code>~$ ls -F
data/   Desktop/    Downloads/        file1.txt@  Pictures/  Templates/
data1/  Documents/  examples.desktop  Music/      Public/    Videos/</code></pre>
<!-- /wp:html -->

<!-- wp:heading -->
<h2>17. List Inode numbers using ls command</h2>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>What is the Inode number?</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>An Inode is a unique number in Linux that is assigned to files. </p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>Whenever you create a new file, it is assigned a file name and an Inode number.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>Inode number contains the following information’s:</p>
<!-- /wp:paragraph -->

<!-- wp:list {"type":"1"} -->
<ul type="1"><!-- wp:list-item -->
<li>UID (User ID)</li>
<!-- /wp:list-item -->

<!-- wp:list-item -->
<li>GID (Group ID)</li>
<!-- /wp:list-item -->

<!-- wp:list-item -->
<li>File Size</li>
<!-- /wp:list-item -->

<!-- wp:list-item -->
<li>Permissions</li>
<!-- /wp:list-item -->

<!-- wp:list-item -->
<li>Modified Month, Date and Time</li>
<!-- /wp:list-item -->

<!-- wp:list-item -->
<li>Device ID</li>
<!-- /wp:list-item -->

<!-- wp:list-item -->
<li>Hard links</li>
<!-- /wp:list-item -->

<!-- wp:list-item -->
<li>Other meta information’s related to that file and many more.</li>
<!-- /wp:list-item --></ul>
<!-- /wp:list -->

<!-- wp:paragraph -->
<p><strong>Read Also:</strong> <span style="text-decoration: underline;"><a href="https://edumotivation.com/explaining-soft-links-and-hard-links-in-linux-with-ln-command/" target="_blank" rel="noreferrer noopener">Explaining Soft Links And Hard Links in Linux</a></span></p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>When you access a file, you use the name of that file from outside, but in the back end, it remains mapped to an Inode number that processes all requested tasks.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>To print Inode numbers, you can use <code>-i</code> option. </p>
<!-- /wp:paragraph -->

<!-- wp:html -->
<pre><code>~$ ls -li
total 44
659121 drwxr-xr-x 2 ubuntu ubuntu 4096 Apr 28 05:33 Desktop
659125 drwxr-xr-x 3 ubuntu ubuntu 4096 Apr 28 06:44 Documents
659122 drwxr-xr-x 2 ubuntu ubuntu 4096 Apr 28 05:25 Downloads
659057 -rw-r--r-- 1 ubuntu ubuntu 8980 Apr 28 05:18 examples.desktop
659126 drwxr-xr-x 2 ubuntu ubuntu 4096 Apr 28 05:25 Music
659127 drwxr-xr-x 2 ubuntu ubuntu 4096 Apr 28 05:25 Pictures
659124 drwxr-xr-x 2 ubuntu ubuntu 4096 Apr 28 05:25 Public
659123 drwxr-xr-x 2 ubuntu ubuntu 4096 Apr 28 05:25 Templates
659128 drwxr-xr-x 2 ubuntu ubuntu 4096 Apr 28 05:25 Videos</code></pre>
<!-- /wp:html -->

<!-- wp:image {"id":359,"sizeSlug":"large"} -->
<figure class="wp-block-image size-large"><img src="https://edumotivation.com/wp-content/uploads/2020/06/Inode-Numbers.png" alt="Inode Numbers" class="wp-image-359"/></figure>
<!-- /wp:image -->

<!-- wp:heading -->
<h2>18. List contents by lines</h2>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>By default,  <code>ls</code> command lists the content by columns.</p>
<!-- /wp:paragraph -->

<!-- wp:image {"id":386,"sizeSlug":"large"} -->
<figure class="wp-block-image size-large"><img src="https://edumotivation.com/wp-content/uploads/2020/06/contents-listed-by-columns-1024x208.png" alt="Contents listed by columns" class="wp-image-386"/></figure>
<!-- /wp:image -->

<!-- wp:paragraph -->
<p>Pass the <code>-x</code> option to <code>ls</code> command to list entries by lines.</p>
<!-- /wp:paragraph -->

<!-- wp:html -->
<pre><code>~$ ls -x
Desktop    docs    Documents  Downloads  examples.desktop  file1.txtx  Music  my  Pictures  Public
Templates  Videos</code></pre>
<!-- /wp:html -->

<!-- wp:heading -->
<h2>19. List contents in a comma-separated format</h2>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>You can list the contents in a comma-separated format.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>To do so, Pass the <code>-m</code> option to <code>ls</code> command.</p>
<!-- /wp:paragraph -->

<!-- wp:html -->
<pre><code>~$ ls -m
Desktop, docs, Documents, Downloads, examples.desktop, file1.txtx, Music, my, Pictures, Public,
Templates, Videos</code></pre>
<!-- /wp:html -->

<!-- wp:heading -->
<h2>20. Print content names within Quotes</h2>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>Use the <code>-Q</code> option with <code>ls</code> to print the content names withing Quotes. </p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>Refer to the following sample output.</p>
<!-- /wp:paragraph -->

<!-- wp:html -->
<pre><code>~$ ls -lQ
total 44
drwxr-xr-x 2 ubuntu ubuntu 4096 Apr 28 05:33 "Desktop"
drwxr-xr-x 3 ubuntu ubuntu 4096 Apr 28 06:44 "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"</code></pre>
<!-- /wp:html -->

<!-- wp:heading -->
<h2>21. Display UID and GID of Files &amp; Directories</h2>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>What is UID &amp; GID?</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>By default, when we create a new user in Linux, it is assigned a UID and a GID.</p>
<!-- /wp:paragraph -->

<!-- wp:list -->
<ul><!-- wp:list-item -->
<li>UID stands for User Identifier</li>
<!-- /wp:list-item -->

<!-- wp:list-item -->
<li>GID stands for Group Identifier</li>
<!-- /wp:list-item --></ul>
<!-- /wp:list -->

<!-- wp:paragraph -->
<p>Let's check the ID information of a user named <strong>helpdesk</strong>:</p>
<!-- /wp:paragraph -->

<!-- wp:image {"id":361,"sizeSlug":"large"} -->
<figure class="wp-block-image size-large"><img src="https://edumotivation.com/wp-content/uploads/2020/06/uid-and-gid-information.png" alt="UID &amp; GID Information" class="wp-image-361"/></figure>
<!-- /wp:image -->

<!-- wp:paragraph -->
<p>Usually, <code>ls -l</code> command lists the name of Owner and Group, but we can use the <code>-n</code> option to list UID &amp; GID information.</p>
<!-- /wp:paragraph -->

<!-- wp:html -->
<pre><code>~$ ls -ln
total 44
drwxr-xr-x 2 1000 1000 4096 Apr 28 05:33 Desktop
drwxr-xr-x 3 1000 1000 4096 Apr 28 06:44 Documents
drwxr-xr-x 2 1000 1000 4096 Apr 28 05:25 Downloads
-rw-r--r-- 1 1000 1000 8980 Apr 28 05:18 examples.desktop
drwxr-xr-x 2 1000 1000 4096 Apr 28 05:25 Music
drwxr-xr-x 2 1000 1000 4096 Apr 28 05:25 Pictures
drwxr-xr-x 2 1000 1000 4096 Apr 28 05:25 Public
drwxr-xr-x 2 1000 1000 4096 Apr 28 05:25 Templates
drwxr-xr-x 2 1000 1000 4096 Apr 28 05:25 Videos</code></pre>
<!-- /wp:html -->

<!-- wp:paragraph -->
<p><strong>Suggested Read:</strong></p>
<!-- /wp:paragraph -->

<!-- wp:list -->
<ul><!-- wp:list-item -->
<li><span style="text-decoration: underline;"><a href="https://edumotivation.com/useradd-command/" target="_blank" rel="noreferrer noopener">How to create Users in Linux</a></span></li>
<!-- /wp:list-item -->

<!-- wp:list-item -->
<li><span style="text-decoration: underline;"><a href="https://edumotivation.com/groupadd-command/" target="_blank" rel="noreferrer noopener">How to create Groups in Linux</a></span></li>
<!-- /wp:list-item --></ul>
<!-- /wp:list -->

<!-- wp:heading -->
<h2>22. Only display Group names in a long listing format</h2>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>As we have seen, a long listing format lists both owner and group names.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>But you can use the <code>-g</code> option to list only Group name instead of both.</p>
<!-- /wp:paragraph -->

<li><span style="text-decoration: underline;"><a href="https://edumotivation.com/ls-command/" target="_blank" rel="noreferrer noopener">Read More</a></span></li>