{"id":451,"date":"2020-04-22T16:05:17","date_gmt":"2020-04-22T21:05:17","guid":{"rendered":"https:\/\/cdrhdev.unl.edu\/log\/?p=451"},"modified":"2020-04-22T16:49:11","modified_gmt":"2020-04-22T21:49:11","slug":"plant-a-tree-in-the-command-line","status":"publish","type":"post","link":"https:\/\/cdrhdev.unl.edu\/log\/2020\/plant-a-tree-in-the-command-line\/","title":{"rendered":"Plant a Tree in the Command Line"},"content":{"rendered":"<p>Happy 50th Anniversary of Earth Day!<\/p>\n<p>Not all of us can plant a tree or do other restorative work for Earth Day, and in the face of large-scale problems to solve we don&#8217;t always feel like the small things we do to help (reducing the amount of water or electricity we use) matter. But the small things do help. For me, this recently was highlighted in another part of my life in the form of a handy little command line tool.<\/p>\n<p>I was feeling that having to enter <code>cd<\/code> (change directory) and <code>ls<\/code> (list directory contents) commands over and over to examine the contents of the file system on the command line was an inefficient way to get a good picture of the files and directories. This is more frequently an issue when I download software to install or am getting familiar with files for a project already under way. What kinds of files are there, and how are they organized?<\/p>\n<p>I performed some simple web searches and found <a href=\"http:\/\/mama.indstate.edu\/users\/ice\/tree\/\"><code>tree<\/code><\/a>. It outputs a recursive directory listing in an easy-to-read tree format. It also provides a variety of options to filter what will be displayed and whether extra information beyond the file name is shown.<\/p>\n<figure id=\"attachment_455\" aria-describedby=\"caption-attachment-455\" style=\"width: 561px\" class=\"wp-caption aligncenter\"><img loading=\"lazy\" decoding=\"async\" class=\"wp-image-455 size-full\" src=\"https:\/\/cdrhdev.unl.edu\/log\/wp-content\/uploads\/2020\/04\/tree-example-color.png\" alt=\"tree command listing all files and directories of a python project\" width=\"561\" height=\"539\" srcset=\"https:\/\/cdrhdev.unl.edu\/log\/wp-content\/uploads\/2020\/04\/tree-example-color.png 561w, https:\/\/cdrhdev.unl.edu\/log\/wp-content\/uploads\/2020\/04\/tree-example-color-300x288.png 300w\" sizes=\"auto, (max-width: 561px) 100vw, 561px\" \/><figcaption id=\"caption-attachment-455\" class=\"wp-caption-text\">Figure 1<\/figcaption><\/figure>\n<h2>Install <code>tree<\/code><\/h2>\n<p><code>tree<\/code> is available on a large variety of operating systems. Linux, Mac, and even Windows with cygwin. I&#8217;ll just share the simplest ones for quick reference.<\/p>\n<p><strong>Fedora \/ CentOS \/ Red Hat \/ Other YUM-based Systems<\/strong><\/p>\n<p><code>sudo yum install tree<\/code><\/p>\n<p><strong>Debian \/ Ubuntu \/ Other APT-based Systems<\/strong><\/p>\n<p><code>sudo apt-get install tree<\/code><\/p>\n<p><strong>Mac OS with Brew<\/strong><\/p>\n<p><code>brew install tree<\/code><\/p>\n<h2>Example Usage<\/h2>\n<p>Running <code>tree<\/code> with no arguments will recursively list every single file and directory from the current directory. This is usually far too much information to be useful for a quick glance to get the big picture of file system organization. So here are a couple options to help narrow output to something more useful:<\/p>\n<h3>Colorize Output<\/h3>\n<p><code>tree -C<\/code><\/p>\n<p>This was used in Figure 1. Without this option, <code>tree<\/code> will display all files and directories with the same color. This option may not be as helpful for individuals with color-blindness or limited sight, but customizing the <code>LS_COLORS<\/code> environment variable which controls the colorization may help there. I personally use Dark Solarized colors via <a href=\"https:\/\/github.com\/seebi\/dircolors-solarized\">dircolors-solarized<\/a>. David Newcomb has a blog post digging further into <a href=\"http:\/\/www.bigsoft.co.uk\/blog\/2008\/04\/11\/configuring-ls_colors\">configuring LS_COLORS<\/a>.<\/p>\n<p>To always get colorized output without needing to specify the <code>-C<\/code> option every time, create an alias to apply it for you with <code>alias 'tree'='tree -C'<\/code>. Now calling <code>tree<\/code> from the command line will include the colorization option automatically. Creating the alias this way is temporary, but you can make this persist across all future terminals by adding it to your <a href=\"https:\/\/bash.cyberciti.biz\/guide\/Startup_scripts\">Bash startup script(s)<\/a>.<\/p>\n<h3>Limit Recursion Depth<\/h3>\n<p><code>tree -L 3<\/code><\/p>\n<p>This limits the depth of directories <code>tree<\/code> will traverse for listing files and directories. This limits output to more of a surface-level picture of a directory&#8217;s contents.<\/p>\n<figure id=\"attachment_453\" aria-describedby=\"caption-attachment-453\" style=\"width: 560px\" class=\"wp-caption aligncenter\"><img loading=\"lazy\" decoding=\"async\" class=\"wp-image-453 size-full\" src=\"https:\/\/cdrhdev.unl.edu\/log\/wp-content\/uploads\/2020\/04\/tree-example-depth.png\" alt=\"tree command listing top level files and directories of a python project\" width=\"560\" height=\"224\" srcset=\"https:\/\/cdrhdev.unl.edu\/log\/wp-content\/uploads\/2020\/04\/tree-example-depth.png 560w, https:\/\/cdrhdev.unl.edu\/log\/wp-content\/uploads\/2020\/04\/tree-example-depth-300x120.png 300w\" sizes=\"auto, (max-width: 560px) 100vw, 560px\" \/><figcaption id=\"caption-attachment-453\" class=\"wp-caption-text\">Figure 2<\/figcaption><\/figure>\n<h3>Only Display Directories<\/h3>\n<p><code>tree -d<\/code><\/p>\n<p>This limits <code>tree<\/code> to only list directories, for a more structural examination of a directory&#8217;s contents.<\/p>\n<figure id=\"attachment_454\" aria-describedby=\"caption-attachment-454\" style=\"width: 560px\" class=\"wp-caption aligncenter\"><img loading=\"lazy\" decoding=\"async\" class=\"wp-image-454 size-full\" src=\"https:\/\/cdrhdev.unl.edu\/log\/wp-content\/uploads\/2020\/04\/tree-example-directories.png\" alt=\"tree command listing only directories of a python project\" width=\"560\" height=\"209\" srcset=\"https:\/\/cdrhdev.unl.edu\/log\/wp-content\/uploads\/2020\/04\/tree-example-directories.png 560w, https:\/\/cdrhdev.unl.edu\/log\/wp-content\/uploads\/2020\/04\/tree-example-directories-300x112.png 300w\" sizes=\"auto, (max-width: 560px) 100vw, 560px\" \/><figcaption id=\"caption-attachment-454\" class=\"wp-caption-text\">Figure 3<\/figcaption><\/figure>\n<h3>List Only or Ignore Files Matching a Pattern<\/h3>\n<p><code>tree -P *.png<\/code><\/p>\n<p>This option will search the files and only list files which match a pattern (which follows the Bash globbing pattern syntax) and the intermediary parent directories of those files.<\/p>\n<figure id=\"attachment_456\" aria-describedby=\"caption-attachment-456\" style=\"width: 564px\" class=\"wp-caption aligncenter\"><img loading=\"lazy\" decoding=\"async\" class=\"wp-image-456 size-full\" src=\"https:\/\/cdrhdev.unl.edu\/log\/wp-content\/uploads\/2020\/04\/tree-example-pattern-match.png\" alt=\"tree command listing files of a python project matching a pattern\" width=\"564\" height=\"269\" srcset=\"https:\/\/cdrhdev.unl.edu\/log\/wp-content\/uploads\/2020\/04\/tree-example-pattern-match.png 564w, https:\/\/cdrhdev.unl.edu\/log\/wp-content\/uploads\/2020\/04\/tree-example-pattern-match-300x143.png 300w\" sizes=\"auto, (max-width: 564px) 100vw, 564px\" \/><figcaption id=\"caption-attachment-456\" class=\"wp-caption-text\">Figure 4<\/figcaption><\/figure>\n<p><code>tree -I *.png<\/code><\/p>\n<p>This option will display the inverse by ignoring all files which match the pattern entered.<\/p>\n<figure id=\"attachment_458\" aria-describedby=\"caption-attachment-458\" style=\"width: 564px\" class=\"wp-caption aligncenter\"><img loading=\"lazy\" decoding=\"async\" class=\"wp-image-458 size-full\" src=\"https:\/\/cdrhdev.unl.edu\/log\/wp-content\/uploads\/2020\/04\/tree-example-pattern-ignore.png\" alt=\"tree command listing files of a python project not matching a pattern\" width=\"564\" height=\"479\" srcset=\"https:\/\/cdrhdev.unl.edu\/log\/wp-content\/uploads\/2020\/04\/tree-example-pattern-ignore.png 564w, https:\/\/cdrhdev.unl.edu\/log\/wp-content\/uploads\/2020\/04\/tree-example-pattern-ignore-300x255.png 300w\" sizes=\"auto, (max-width: 564px) 100vw, 564px\" \/><figcaption id=\"caption-attachment-458\" class=\"wp-caption-text\">Figure 5<\/figcaption><\/figure>\n<h3>Ignore Directories with Lots of Files<\/h3>\n<p><code>tree --filelimit 4<\/code><\/p>\n<p>This will stop <code>tree<\/code> from recursing down into directories which contain more than the specified total number of files and directories at the next level down. This is another way to limit things to a more surface-level view and shorten the length of output.<\/p>\n<figure id=\"attachment_459\" aria-describedby=\"caption-attachment-459\" style=\"width: 563px\" class=\"wp-caption aligncenter\"><img loading=\"lazy\" decoding=\"async\" class=\"wp-image-459 size-full\" src=\"https:\/\/cdrhdev.unl.edu\/log\/wp-content\/uploads\/2020\/04\/tree-example-filelimit.png\" alt=\"tree command listing files and directories which don't contain too many files of a python project\" width=\"563\" height=\"284\" srcset=\"https:\/\/cdrhdev.unl.edu\/log\/wp-content\/uploads\/2020\/04\/tree-example-filelimit.png 563w, https:\/\/cdrhdev.unl.edu\/log\/wp-content\/uploads\/2020\/04\/tree-example-filelimit-300x151.png 300w\" sizes=\"auto, (max-width: 563px) 100vw, 563px\" \/><figcaption id=\"caption-attachment-459\" class=\"wp-caption-text\">Figure 6<\/figcaption><\/figure>\n<h3>Show Permissions<\/h3>\n<p><code>tree -p<\/code><\/p>\n<p>This shows permissions alongside the files and directories.<\/p>\n<figure id=\"attachment_464\" aria-describedby=\"caption-attachment-464\" style=\"width: 561px\" class=\"wp-caption aligncenter\"><img loading=\"lazy\" decoding=\"async\" class=\"wp-image-464 size-full\" src=\"https:\/\/cdrhdev.unl.edu\/log\/wp-content\/uploads\/2020\/04\/tree-example-permissions.png\" alt=\"tree command listing all files and directories of a python project with permissions info\" width=\"561\" height=\"539\" srcset=\"https:\/\/cdrhdev.unl.edu\/log\/wp-content\/uploads\/2020\/04\/tree-example-permissions.png 561w, https:\/\/cdrhdev.unl.edu\/log\/wp-content\/uploads\/2020\/04\/tree-example-permissions-300x288.png 300w\" sizes=\"auto, (max-width: 561px) 100vw, 561px\" \/><figcaption id=\"caption-attachment-464\" class=\"wp-caption-text\">Figure 7<\/figcaption><\/figure>\n<h3>Display Another Directory<\/h3>\n<p><code>tree -CI *.log -L 2 \/var\/local\/www\/data<\/code><\/p>\n<p>This command is combining a few of our examples, but the new part is just specifying a different directory at the end. This can be used to help minimize the repeated <code>cd<\/code> command usage I mentioned earlier.<\/p>\n<figure id=\"attachment_460\" aria-describedby=\"caption-attachment-460\" style=\"width: 560px\" class=\"wp-caption aligncenter\"><img loading=\"lazy\" decoding=\"async\" class=\"wp-image-460 size-full\" src=\"https:\/\/cdrhdev.unl.edu\/log\/wp-content\/uploads\/2020\/04\/tree-example-another-dir.png\" alt=\"tree command listing files and directories of another directory\" width=\"560\" height=\"976\" srcset=\"https:\/\/cdrhdev.unl.edu\/log\/wp-content\/uploads\/2020\/04\/tree-example-another-dir.png 560w, https:\/\/cdrhdev.unl.edu\/log\/wp-content\/uploads\/2020\/04\/tree-example-another-dir-172x300.png 172w\" sizes=\"auto, (max-width: 560px) 100vw, 560px\" \/><figcaption id=\"caption-attachment-460\" class=\"wp-caption-text\">Figure 8<\/figcaption><\/figure>\n<h2>Reduce Usage<\/h2>\n<p>Much like <code>tree<\/code> has helped me reduce my usage of the <code>cd<\/code> and <code>ls<\/code> commands, I hope for Earth Day you will take some time to reflect on your own habits and reduce usage of resources, whether it be disposable products as opposed to reusable ones, containers which can&#8217;t be recycled, water in the shower, gasoline for driving (take it easy on the accelerator), gas for heating, or electricity for lighting and appliances.<\/p>\n<p>I hope you found this helpful and have a healthy day!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Happy 50th Anniversary of Earth Day! Not all of us can plant a tree or do other restorative work for Earth Day, and in the face of large-scale problems to solve we don&#8217;t always feel like the small things we do to help (reducing the amount of water or electricity we use) matter. But the&hellip;<\/p>\n <a href=\"https:\/\/cdrhdev.unl.edu\/log\/2020\/plant-a-tree-in-the-command-line\/\" title=\"Plant a Tree in the Command Line\" class=\"entry-more-link\"><span>Read More<\/span> <span class=\"screen-reader-text\">Plant a Tree in the Command Line<\/span><\/a>","protected":false},"author":3,"featured_media":455,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"Layout":"","footnotes":""},"categories":[40],"tags":[64,63],"class_list":["entry","author-techgique","post-451","post","type-post","status-publish","format-standard","has-post-thumbnail","category-utilities","tag-bash","tag-command-line"],"_links":{"self":[{"href":"https:\/\/cdrhdev.unl.edu\/log\/wp-json\/wp\/v2\/posts\/451","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/cdrhdev.unl.edu\/log\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/cdrhdev.unl.edu\/log\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/cdrhdev.unl.edu\/log\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/cdrhdev.unl.edu\/log\/wp-json\/wp\/v2\/comments?post=451"}],"version-history":[{"count":11,"href":"https:\/\/cdrhdev.unl.edu\/log\/wp-json\/wp\/v2\/posts\/451\/revisions"}],"predecessor-version":[{"id":472,"href":"https:\/\/cdrhdev.unl.edu\/log\/wp-json\/wp\/v2\/posts\/451\/revisions\/472"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/cdrhdev.unl.edu\/log\/wp-json\/wp\/v2\/media\/455"}],"wp:attachment":[{"href":"https:\/\/cdrhdev.unl.edu\/log\/wp-json\/wp\/v2\/media?parent=451"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/cdrhdev.unl.edu\/log\/wp-json\/wp\/v2\/categories?post=451"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/cdrhdev.unl.edu\/log\/wp-json\/wp\/v2\/tags?post=451"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}