{"id":8090,"date":"2021-03-09T16:35:49","date_gmt":"2021-03-09T13:35:49","guid":{"rendered":"https:\/\/kifarunix.com\/?p=8090"},"modified":"2024-03-19T19:22:22","modified_gmt":"2024-03-19T16:22:22","slug":"example-usage-of-ls-command-in-linux","status":"publish","type":"post","link":"https:\/\/kifarunix.com\/example-usage-of-ls-command-in-linux\/","title":{"rendered":"Example Usage of ls Command in Linux"},"content":{"rendered":"\n<p>In this tutorial you will learn how to use <code>ls<\/code> command in Linux\/Unix Operating Systems.<code> ls<\/code><strong> <\/strong>command is used to list files or directories contents from the command line (CLI).<\/p>\n\n\n\n<p>Through<strong> <\/strong><code>ls<\/code> command, we obtain information about the files in the current or in any other directory.<\/p>\n\n\n\n<p>Command line syntax of<code> ls<\/code> command.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>ls [OPTIONS] ...[FILES] \u2026<\/code><\/pre>\n\n\n\n<p>Let\u2019s dig in.<\/p>\n\n\n\n<p><strong>List Current Directory Contents<\/strong><\/p>\n\n\n\n<p>Open your terminal and type <code>ls <\/code>command.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>ls<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>Data  file.txt  inputs.txt  Music  THM  user.txt<\/code><\/pre>\n\n\n\n<p><strong>List Another Directory Contents<\/strong><\/p>\n\n\n\n<p>Type <code>ls [directory name]<\/code><strong> <\/strong>i.e. <code>ls \/boot<\/code><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>ls \/boot<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>config-5.4.0-21-generic  grub                         initrd.img-5.4.0-56-generic  memtest86+_multiboot.bin     System.map-5.4.0-56-generic  vmlinuz-5.4.0-56-generic\nconfig-5.4.0-47-generic  initrd.img                   initrd.img.old               System.map-5.4.0-21-generic  vmlinuz                      vmlinuz.old\n<\/code><\/pre>\n\n\n\n<p><strong>List Contents Including Hidden Contents<\/strong><\/p>\n\n\n\n<p>Type <code>ls -a<\/code> command to list all contents including hidden directories and files. Anything that begins with a period <code>(.)<\/code> is a hidden file in Linux\/Unix.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>ls -a<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>.  ..  .bash_history  .bash_logout  .bashrc  dejavu-fonts-ttf-2.37  Documents  Downloads  Music  Pictures  .profile  Videos<\/code><\/pre>\n\n\n\n<p><strong>List Contents in Long Format<\/strong><\/p>\n\n\n\n<p>Type<code> ls -l<\/code> command to list files in an organized manner in form of columns.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>ls -l<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>total 24\ndrwxrwxr-x 2 pilot pilot 4096 Mar  4 11:52 Data\n-rw-rw-r-- 1 pilot pilot   45 Mar  3 22:34 file.txt\n-rw-rw-r-- 1 pilot pilot   22 Mar  3 22:36 inputs.txt\ndrwxrwxr-x 4 pilot pilot 4096 Mar  4 11:51 Music\ndrwxrwxr-x 2 pilot pilot 4096 Mar  4 11:53 THM\n-rw-rw-r-- 1 pilot pilot   54 Mar  3 22:35 user.txt<\/code><\/pre>\n\n\n\n<p>The columns include:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><strong><code>-permission<\/code><\/strong> of the files<\/li><li><code><strong>-links<\/strong><\/code> to the files<\/li><li><strong><code>-owner<\/code><\/strong> of the files<\/li><li><strong><code>-group<\/code><\/strong> owner of the files<\/li><li><strong><code>-size<\/code><\/strong> of the files<\/li><li><strong><code>-last date\/time<\/code><\/strong> files is modified<\/li><li><strong><code>-files\/directories<\/code><\/strong> names<\/li><\/ul>\n\n\n\n<p><strong>List Contents in Long Format Including Hidden Contents and in Human Readable Format<\/strong><\/p>\n\n\n\n<p>Type <code>ls -alh<\/code><strong> <\/strong>or<code> ls -a -l -h<\/code><strong> <\/strong>command to list all hidden files in an organized manner in form of columns and human readable format sizes <code>(e.g. 1K, 2M, 2G).<\/code><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>ls -alh<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>total 32K\ndrwxrwxr-x 5 pilot pilot 4.0K Mar  3 23:08 .\ndrwxr-xr-x 8 pilot pilot 4.0K Mar  4 11:36 ..\ndrwxrwxr-x 2 pilot pilot 4.0K Mar  4 11:52 Data\n-rw-rw-r-- 1 pilot pilot   45 Mar  3 22:34 file.txt\n-rw-rw-r-- 1 pilot pilot   22 Mar  3 22:36 inputs.txt\ndrwxrwxr-x 4 pilot pilot 4.0K Mar  4 11:51 Music\ndrwxrwxr-x 2 pilot pilot 4.0K Mar  4 11:53 THM<\/code><\/pre>\n\n\n\n<p><strong>List Contents and Sort by Time and Date<\/strong><\/p>\n\n\n\n<p>Type<code> ls -t<\/code> command to sort files and directories by modification time and date showing newest to oldest in that order.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code> ls -t<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>Videos  Pictures  Music  Documents  dejavu-fonts-ttf-2.37  Downloads<\/code><\/pre>\n\n\n\n<p><strong>List Contents and Sort by File Size<\/strong><\/p>\n\n\n\n<p>Type <code>ls -S<\/code><strong> <\/strong>(uppercase S) command to sort files by their sizes from biggest to smallest.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>ls -S<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>dejavu-fonts-ttf-2.37  Documents  Downloads  Music  Pictures  Videos<\/code><\/pre>\n\n\n\n<p><strong>List Contents With Their Sizes<\/strong><\/p>\n\n\n\n<p>Type <code>ls -s<\/code> (lowercase s) command to print the allocated sizes of each file, in blocks.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>ls -s<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>total 24\n4 dejavu-fonts-ttf-2.37  4 Documents  4 Downloads  4 Music  4 Pictures  4 Videos<\/code><\/pre>\n\n\n\n<p><strong>List Directories Only<\/strong><\/p>\n\n\n\n<p>Type <code>ls -d *\/<\/code><strong> <\/strong>command to list directories only without their contents.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>ls -d *\/<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>Data\/  Music\/  THM\/<\/code><\/pre>\n\n\n\n<p><strong>List Contents With Their Sub-directories<\/strong><\/p>\n\n\n\n<p>Type<strong> <\/strong><code>ls * <\/code>command to lists files of the directory together with their sub-directories.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>ls *<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>file.txt  inputs.txt  user.txt\n\nData:\noutputs.txt\n\nMusic:\ngospel  reggue  songs.txt\n\nTHM:\ntutorial.txt<\/code><\/pre>\n\n\n\n<p>And this is a summary of how to use<code> ls<\/code> command in Linux\/Unix OS.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Reference<\/h3>\n\n\n\n<p>You can learn more about<code> ls <\/code>command using their man and info pages:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>man ls<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>info ls<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Other Tutorials<\/h3>\n\n\n\n<p><a aria-label=\" (opens in a new tab)\" href=\"https:\/\/kifarunix.com\/example-usage-of-ps-command-in-linux\/\" target=\"_blank\" rel=\"noreferrer noopener\" class=\"rank-math-link\">Example Usage of ps Command in Linux<\/a><\/p>\n\n\n\n<p><a aria-label=\" (opens in a new tab)\" href=\"https:\/\/kifarunix.com\/run-only-specific-commands-with-sudo-in-linux\/\" target=\"_blank\" rel=\"noreferrer noopener\" class=\"rank-math-link\">Run only Specific Commands with sudo in Linux<\/a><\/p>\n\n\n\n<p><a aria-label=\" (opens in a new tab)\" href=\"https:\/\/kifarunix.com\/how-to-add-users-to-sudo-group-in-linux\/\" target=\"_blank\" rel=\"noreferrer noopener\" class=\"rank-math-link\">How to Add Users to sudo group in Linux<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this tutorial you will learn how to use ls command in Linux\/Unix Operating Systems. ls command is used to list files or directories contents<\/p>\n","protected":false},"author":6,"featured_media":8173,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"rank_math_lock_modified_date":false,"footnotes":""},"categories":[49,121],"tags":[3242,3243,3246,3245,3244,3241],"class_list":["post-8090","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-command-cheatsheets","category-howtos","tag-how-to-list-directory-contents-in-linux","tag-linux-view-directory-contents","tag-list-files-with-ls-command-linux","tag-ls-command","tag-ls-command-in-linux","tag-use-ls-command-in-linux","generate-columns","tablet-grid-50","mobile-grid-100","grid-parent","grid-50","resize-featured-image"],"_links":{"self":[{"href":"https:\/\/kifarunix.com\/wp-json\/wp\/v2\/posts\/8090"}],"collection":[{"href":"https:\/\/kifarunix.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/kifarunix.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/kifarunix.com\/wp-json\/wp\/v2\/users\/6"}],"replies":[{"embeddable":true,"href":"https:\/\/kifarunix.com\/wp-json\/wp\/v2\/comments?post=8090"}],"version-history":[{"count":15,"href":"https:\/\/kifarunix.com\/wp-json\/wp\/v2\/posts\/8090\/revisions"}],"predecessor-version":[{"id":8176,"href":"https:\/\/kifarunix.com\/wp-json\/wp\/v2\/posts\/8090\/revisions\/8176"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/kifarunix.com\/wp-json\/wp\/v2\/media\/8173"}],"wp:attachment":[{"href":"https:\/\/kifarunix.com\/wp-json\/wp\/v2\/media?parent=8090"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/kifarunix.com\/wp-json\/wp\/v2\/categories?post=8090"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/kifarunix.com\/wp-json\/wp\/v2\/tags?post=8090"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}