Linux tree command

Discussion in 'Getting Started' started by Govinda Sakhare, Mar 14, 2014.

  1. Govinda Sakhare

    Govinda Sakhare New Member

    Likes Received:
    Trophy Points:
    i wanted tree view of folders so i used below listed command .Please someone explain me this command
    ls -R $var | grep ":$" | sed -e 's/:$//' -e 's/[^-][^\/]*\//|--/g' -e 's/^/ /' -e 's/-/|/'

    Attached Files:

    • Q3.txt
      File size:
      507 bytes
  2. MikeyD

    MikeyD Active Member

    Likes Received:
    Trophy Points:
    Explaining this command means explaining regular expressions, something that people have literally written whole books about.

    In a nutshell $var is a variable I'd assume is the root directory you're looking to get a tree mapping of. ls -R is listing every directory tree recursively, it then grabs all the lines that end in : aka all directory lines

    The sed commands edit the streams to produce the file-tree look of the result. It replaces the first expression with the second, so:
    sed 's/this/that/' will replace all occurences of 'this' with 'that'.

    It is MUCH more complex than this. The details of sed could fill another book, volume two of the regular expressions anthology.

    You can ultimately get all the same info just using ls -R $var | grep ":$", all the other commands just make it look prettier.

    If you want to learn more about sed and regular expressions there is a lot of good info online. Here is a good tutorial:

Share This Page