{"id":6176,"date":"2024-07-08T02:10:00","date_gmt":"2024-07-08T06:10:00","guid":{"rendered":"https:\/\/www.both.org\/?p=6176"},"modified":"2024-07-01T09:53:27","modified_gmt":"2024-07-01T13:53:27","slug":"tips-for-using-the-ls-command-in-linux","status":"publish","type":"post","link":"https:\/\/www.both.org\/?p=6176","title":{"rendered":"Tips for using the &#8216;ls&#8217; command in Linux"},"content":{"rendered":"<div class=\"pld-like-dislike-wrap pld-template-1\">\r\n    <div class=\"pld-like-wrap  pld-common-wrap\">\r\n    <a href=\"javascript:void(0)\" class=\"pld-like-trigger pld-like-dislike-trigger  \" title=\"\" data-post-id=\"6176\" data-trigger-type=\"like\" data-restriction=\"cookie\" data-already-liked=\"0\">\r\n                        <i class=\"fas fa-thumbs-up\"><\/i>\r\n                <\/a>\r\n    <span class=\"pld-like-count-wrap pld-count-wrap\">    <\/span>\r\n<\/div><\/div>\n<p>One of the first commands I learned in Linux was <code>ls<\/code>. Knowing what\u2019s in a directory where a file on your system resides is important. Being able to see and modify not just some but <em>all<\/em> of the files is also important.<\/p>\n\n\n\n<p>My first Linux cheat sheet was the One Page Linux Manual, released in 1999 and it became my go-to reference. I taped it over my desk and referred to it often as I explored Linux. Listing files with <code>ls -l<\/code> is introduced on the first page, at the bottom of the first column.<\/p>\n\n\n\n<p>Later, I would learn other iterations of this most basic command. Through the <code>ls<\/code> command, I began to learn about the complexity of the Linux file permissions and what was mine and what required root or sudo permission to change. I became very comfortable on the command line over time, and while I still use <code>ls -l<\/code> to find files in the directory, I frequently use <code>ls -al<\/code> so I can see hidden files that might need to be changed, like configuration files.<\/p>\n\n\n\n<p>According to an article by Eric Fischer about the <code>ls<\/code> command in the <a href=\"http:\/\/www.tldp.org\/LDP\/LG\/issue48\/fischer.html\">Linux Documentation Project<\/a>, the command&#8217;s roots go back to the <code>listf<\/code> command on MIT\u2019s Compatible Time Sharing System in 1961. When CTSS was replaced by <a href=\"https:\/\/en.wikipedia.org\/wiki\/Multics\">Multics<\/a>, the command became <code>list<\/code>, with switches like <code>list -all<\/code>. According to <a href=\"https:\/\/en.wikipedia.org\/wiki\/Ls\">Wikipedia<\/a>, <code>ls<\/code> appeared in the original version of AT&amp;T Unix. The <code>ls<\/code> command we use today on Linux systems comes from the <a href=\"http:\/\/www.gnu.org\/s\/coreutils\/\">GNU Core Utilities<\/a>.<\/p>\n\n\n\n<p>Most of the time, I use only a couple of iterations of the command. Looking inside a directory with <code>ls<\/code> or <code>ls -al<\/code> is how I generally use the command, but there are many other options that you should be familiar with.<\/p>\n\n\n\n<p><code>$ <strong>ls -l<\/strong><\/code> provides a simple list of the directory:<\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter size-full is-resized\"><img loading=\"lazy\" decoding=\"async\" width=\"665\" height=\"215\" src=\"https:\/\/www.both.org\/wp-content\/uploads\/2024\/06\/image-6.png\" alt=\"\" class=\"wp-image-6177\" style=\"width:501px;height:auto\"\/><\/figure>\n\n\n\n<p>Using the man pages of my Linux Mint system, I find that there are many other options to <code>ls<\/code>, all of which provide interesting and useful information about the Linux file system. By entering <code>man ls<\/code> at the command prompt, we can begin to explore some of the other options:<\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter size-full is-resized\"><a href=\"https:\/\/www.both.org\/wp-content\/uploads\/2024\/06\/image-7.png\" target=\"_blank\" rel=\"noopener\"><img loading=\"lazy\" decoding=\"async\" width=\"792\" height=\"373\" src=\"https:\/\/www.both.org\/wp-content\/uploads\/2024\/06\/image-7.png\" alt=\"\" class=\"wp-image-6178\" style=\"width:526px;height:auto\"\/><\/a><\/figure>\n\n\n\n<p>To sort the directory by file sizes, use <code>ls -lS<\/code>:<\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter size-full is-resized\"><a href=\"https:\/\/www.both.org\/wp-content\/uploads\/2024\/06\/image-9.png\" target=\"_blank\" rel=\"noopener\"><img loading=\"lazy\" decoding=\"async\" width=\"638\" height=\"223\" src=\"https:\/\/www.both.org\/wp-content\/uploads\/2024\/06\/image-9.png\" alt=\"\" class=\"wp-image-6180\" style=\"width:540px;height:auto\"\/><\/a><\/figure>\n\n\n\n<p>To list the contents in reverse order, use <code>ls -lr<\/code>:<\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter size-full is-resized\"><a href=\"https:\/\/www.both.org\/wp-content\/uploads\/2024\/06\/image-8.png\" target=\"_blank\" rel=\"noopener\"><img loading=\"lazy\" decoding=\"async\" width=\"685\" height=\"267\" src=\"https:\/\/www.both.org\/wp-content\/uploads\/2024\/06\/image-8.png\" alt=\"\" class=\"wp-image-6179\" style=\"width:501px;height:auto\"\/><\/a><\/figure>\n\n\n\n<p>List the contents in reverse order with $ ls -lr:<\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter size-full is-resized\"><a href=\"https:\/\/www.both.org\/wp-content\/uploads\/2024\/06\/image-10.png\" target=\"_blank\" rel=\"noopener\"><img loading=\"lazy\" decoding=\"async\" width=\"688\" height=\"261\" src=\"https:\/\/www.both.org\/wp-content\/uploads\/2024\/06\/image-10.png\" alt=\"\" class=\"wp-image-6181\" style=\"width:498px;height:auto\"\/><\/a><\/figure>\n\n\n\n<p>To list contents by columns, use <code>ls -c<\/code>:<\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter size-full is-resized\"><a href=\"https:\/\/www.both.org\/wp-content\/uploads\/2024\/06\/image-11.png\" target=\"_blank\" rel=\"noopener\"><img loading=\"lazy\" decoding=\"async\" width=\"686\" height=\"87\" src=\"https:\/\/www.both.org\/wp-content\/uploads\/2024\/06\/image-11.png\" alt=\"\" class=\"wp-image-6182\" style=\"width:512px;height:auto\"\/><\/a><\/figure>\n\n\n\n<p><code>ls -al<\/code> provides a list of all the files in the same directory:<\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter size-full is-resized\"><a href=\"https:\/\/www.both.org\/wp-content\/uploads\/2024\/06\/image-12.png\" target=\"_blank\" rel=\"noopener\"><img loading=\"lazy\" decoding=\"async\" width=\"644\" height=\"237\" src=\"https:\/\/www.both.org\/wp-content\/uploads\/2024\/06\/image-12.png\" alt=\"\" class=\"wp-image-6183\" style=\"width:512px;height:auto\"\/><\/a><\/figure>\n\n\n\n<p>Here are some additional options that I find useful and interesting:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>List only the .txt files in the directory: <code>ls *.txt<\/code><\/li>\n\n\n\n<li>List by file size: <code>ls -s<\/code><\/li>\n\n\n\n<li>Sort by time and date: <code>ls -d<\/code><\/li>\n\n\n\n<li>Sort by extension: <code>ls -X<\/code><\/li>\n\n\n\n<li>Sort by file size: <code>ls -S<\/code><\/li>\n\n\n\n<li>Long format with file size: <code>ls -ls<\/code><\/li>\n\n\n\n<li>List only the .txt files in a directory: <code>ls *.txt<\/code><\/li>\n<\/ul>\n\n\n\n<p>To generate a directory list in the specified format and send it to a file for later viewing, enter <code>ls -al &gt; mydirectorylist<\/code>. Finally, one of the more exotic commands I found is <code>ls -R<\/code>, which provides a recursive list of all the directories on your computer and their contents.<\/p>\n\n\n\n<p>For a complete list of the all the iterations of the <code>ls<\/code> command, refer to the <a href=\"https:\/\/www.gnu.org\/software\/coreutils\/manual\/html_node\/ls-invocation.html#ls-invocation\">GNU Core Utilities<\/a>.<\/p>\n\n\n\n<p class=\"has-small-font-size\"><a href=\"http:\/\/creativecommons.org\/licenses\/by-sa\/4.0\/\"><\/a>This work is licensed under a Creative Commons Attribution-Share Alike 4.0 International License.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>One of the first commands I learned in Linux was ls. Knowing what&rsquo;s in a directory where a<\/p>\n","protected":false},"author":32,"featured_media":4464,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_lmt_disableupdate":"","_lmt_disable":"","footnotes":""},"categories":[100,5],"tags":[472],"class_list":["post-6176","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-command-line","category-linux","tag-linux-commands"],"modified_by":"David Both","_links":{"self":[{"href":"https:\/\/www.both.org\/index.php?rest_route=\/wp\/v2\/posts\/6176","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.both.org\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.both.org\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.both.org\/index.php?rest_route=\/wp\/v2\/users\/32"}],"replies":[{"embeddable":true,"href":"https:\/\/www.both.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=6176"}],"version-history":[{"count":5,"href":"https:\/\/www.both.org\/index.php?rest_route=\/wp\/v2\/posts\/6176\/revisions"}],"predecessor-version":[{"id":6237,"href":"https:\/\/www.both.org\/index.php?rest_route=\/wp\/v2\/posts\/6176\/revisions\/6237"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.both.org\/index.php?rest_route=\/wp\/v2\/media\/4464"}],"wp:attachment":[{"href":"https:\/\/www.both.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=6176"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.both.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=6176"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.both.org\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=6176"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}