Linux show tree directory

Linux command to print directory structure in the form of a tree

Just run find . Or find . -not -path ‘*/\.*’ to hide files and folders starting with . . If you want to have output with spaces, as in the question, use it with this «find prettifier» script: find . -not -path ‘*/\.*’ | python -c «import sys as s;s.a=[];[setattr(s,’a’,list(filter(lambda p: c.startswith(p+’/’),s.a)))or (s.stdout.write(‘ ‘*len(s.a)+c[len(s.a[-1])+1 if s.a else 0:])or True) and s.a.append(c[:-1]) for c in s.stdin]»

The policy of closing questions without migrating is harmful to both stackoverflow and human knowledge in general. In the last 3 days, every single questions I googled and came across was closed for similar reasoning, and no more activity was able to happen. This means no one can update it, no one can give a better answer, and it makes stackoverflow look shortsighted or elitist. Stackoverflow should consider requiring a migration when a topic is found to have these conditions.

I agree with @NickYeates I am here in late September of 2017 still finding answers to this same question. Think long term when we design these question and answer policies!

11 Answers 11

Is this what you’re looking for tree? It should be in most distributions (maybe as an optional install).

~> tree -d /proc/self/ /proc/self/ |-- attr |-- cwd -> /proc |-- fd | `-- 3 -> /proc/15589/fd |-- fdinfo |-- net | |-- dev_snmp6 | |-- netfilter | |-- rpc | | |-- auth.rpcsec.context | | |-- auth.rpcsec.init | | |-- auth.unix.gid | | |-- auth.unix.ip | | |-- nfs4.idtoname | | |-- nfs4.nametoid | | |-- nfsd.export | | `-- nfsd.fh | `-- stat |-- root -> / `-- task `-- 15589 |-- attr |-- cwd -> /proc |-- fd | `-- 3 -> /proc/15589/task/15589/fd |-- fdinfo `-- root -> / 27 directories 

sample taken from maintainer’s web page.

You can add the option -L # where # is replaced by a number, to specify the max recursion depth.

Remove -d to display also files.

ls -R | grep ":$" | sed -e 's/:$//' -e 's/[^-][^\/]*\//--/g' -e 's/^/ /' -e 's/-/|/' 

It will show a graphical representation of the current sub-directories without files in a few seconds, e.g. in /var/cache/ :

 . |-apache2 |---mod_cache_disk |-apparmor |-apt |---archives |-----partial |-apt-xapian-index |---index.1 |-dbconfig-common |---backups |-debconf 

If you want it with spaces, more like the OP requested, then this: ls -R | grep «:$» | sed -e ‘s/:$//’ -e ‘s/[^-][^\/]*\// /g’ -e ‘s/^/ /’

@Ben thanks for that, all other (non- tree ) answers produce a result that does not look perfectly correct to me. For example in this answer, there should be a line going down from apt then horizontally to archives , instead it comes down from . and goes to archives , only because of more indentation you can guess that it’s actually a subfolder of apt . So you could as well just leave the lines away, it’s at least not misleading then.

Читайте также:  Linux check file descriptors

This command works to display both folders and files.

. |-trace.pcap |-parent | |-chdir1 | | |-file1.txt | |-chdir2 | | |-file2.txt | | |-file3.sh |-tmp | |-json-c-0.11-4.el7_0.x86_64.rpm 

Source: Comment from @javasheriff here. Its submerged as a comment and posting it as answer helps users spot it easily.

for python3 I found find . |grep -vE ‘pyc|swp|__init’ | sed -e «s/[^-][^\/]*\// |/g» -e «s/|\([^ ]\)/|-\1/» working well

This was a great help as we didn’t have tree installed on a certain server. This will most likely work on any standard Linux system.

Since it was a successful comment, I am adding it as an answer:
To print the directory structure in the form of a tree,
WITH FILES

If you would like to sort your result list then combine the nice solution above with sort this way: find . | sort | sed -e «s/[^-][^\/]*\// |/g» -e «s/|\([^ ]\)/|-\1/»

To add Hassou’s solution to your .bashrc, try:

alias lst='ls -R | grep ":$" | sed -e '"'"'s/:$//'"'"' -e '"'"'s/[^-][^\/]*\//--/g'"'"' -e '"'"'s/^/ /'"'"' -e '"'"'s/-/|/'"'" 

Nice alias. But there is missing ‘ ‘ (2 single quote chars) at the end. It works even without it, but. if you want to add some more commands at the end you will see the literal is not complete. So it should go alias lst=’ls -R | grep «:$» | sed -e ‘»‘»‘s/:$//'»‘»‘ -e ‘»‘»‘s/[^-][^\/]*\//—/g'»‘»‘ -e ‘»‘»‘s/^/ /'»‘»‘ -e ‘»‘»‘s/-/|/'»‘»»

Since I was not too happy with the output of other (non- tree ) answers (see my comment at Hassou’s answer), I tried to mimic tree s output a bit more.

It’s similar to the answer of Robert but the horizontal lines do not all start at the beginning, but where there are supposed to start. Had to use perl though, but in my case, on the system where I don’t have tree , perl is available.

ls -aR | grep ":$" | perl -pe 's/:$//;s/[^-][^\/]*\// /g;s/^ (\S)/└── \1/;s/(^ | (?= ))/│ /g;s/ (\S)/└── \1/' 
. └── fd └── net │ └── dev_snmp6 │ └── nfsfs │ └── rpc │ │ └── auth.unix.ip │ └── stat │ └── vlan └── ns └── task │ └── 1310 │ │ └── net │ │ │ └── dev_snmp6 │ │ │ └── rpc │ │ │ │ └── auth.unix.gid │ │ │ │ └── auth.unix.ip │ │ │ └── stat │ │ │ └── vlan │ │ └── ns 

Suggestions to avoid the superfluous vertical lines are welcome 🙂

I still like Ben’s solution in the comment of Hassou’s answer very much, without the (not perfectly correct) lines it’s much cleaner. For my use case I additionally removed the global indentation and added the option to also ls hidden files, like so:

ls -aR | grep ":$" | sed -e 's/:$//' -e 's/[^-][^\/]*\// /g' 

Output (shortened even more):

. fd net dev_snmp6 nfsfs rpc auth.unix.ip stat vlan ns 

Источник

How to print the directory tree in terminal

enter image description here

What is the command that can be used to draw the directory tree inside the terminal emulator?

3 Answers 3

You can use tree to print the directory tree in terminal. Install tree from terminal,

Читайте также:  Командная оболочка пользователя linux

To see the directory tree, use

Or navigate to a directory and just use

It has some advanced options too. You can see owner’s username , groupname , date of last modification of a file/folder and so on using tree . It supports directory colors of ls so you can see colourized outputs.

You can do it easily with the following command:

find . -type d | sed -e "s/[^-][^\/]*\// |/g" -e "s/|\([^ ]\)/| - \1/" 

This command will search recursively for directories inside the parent directory, and then draw the tree of the directories found.

You may also try the following to include all of the files as well.

@AvinashRaj Buddy the overall of the command is clear if you wan to learn more about sed please refer to some tutorials or google it. It’s really hard to explain all sed here!

Maythux, you’re right that if you understand sed then your script is clear, but don’t be fooled into thinking sed/regexes are anything but a cryptic language you’ve learned over the years. It’s hardly intuitive — which I think is the spirit of Avinash Raj’s comment.

Источник

How to Master the Linux Tree Command

Linux Tree Command

As a Linux user, our first choice for directory listings is the good old ls command. The ls command, however, lacks some functions that the topic discussed here offers – the tree command. This command outputs the directories, subdirectories, and files as a tree. You can make the command even more helpful by giving it different options/flags to customize the list.

This article explains how to use the tree command with examples.

We have run the commands and procedures mentioned in this article on an Ubuntu 22.04 LTS system.

How to Install the Tree Command

Installing the tree command line utility is pretty simple through the apt-get command. Open your Ubuntu command line, the Terminal, either through the system Dash or the Ctrl+Alt+t shortcut.

Then enter the following command as sudo:

Please note that only an authorized user can add, remove and configure software on Ubuntu.

Install the tree command

After the tree command is installed, you can check the version number and also ensure that the installation was successful through the following command:

Check tree command version

We recommend running the following command before each install so that you can get the latest available version of software present in the online repositories:

How to Use Tree command

Here we will mention some examples of the tree command so that you can not only use it but also take a step forward in mastering it.

Basic Tree output

This is the most basic way of using the tree command:

Output of tree command

The output shows a tree structure of your current directory, displaying all the folders, sub-folders, and files.

Display contents of a specific directory

To list the files and subfolders of a specific directory rather than that of the current directory, you can specify the directory name or path through the following command syntax:

The following command will list all the files and sub-folders, if any, in the Pictures directory:

Tree output of specific directory

Display hidden files along with other files using Tree

The tree command does not display the listing of hidden files and folders in Ubuntu. You can, however, use the ‘a’ flag as follows in order to list them:

Читайте также:  What linux version is this

Display hidden files with tree

The files and folders in the tree starting from a ‘.’ are hidden. In the above output, I have highlighted one such entry to explain what it looks like.

Display only directory listing through Tree

If you want to view only the directory listing and not the underlying files, you can use the d flag with the tree command as follows:

Show directories only

Display full path prefix of files and folders using Tree

With the -f flag, you can customize the tree flag to display the complete path as a prefix for all the files and folders.

Display full path prefix of files

This is especially helpful when you want to know what exists where.

Display size of files and folders using Tree

With the -s flag, you can make the tree command print the bytes size of all the files and folders in your directory.

Display size of files and folders

This helps you determine which items are taking up a large amount of space on your system and get rid of the unnecessary ones.

Display read-write permissions of files and folders using Tree

Through the p flag in your tree command, you can view the read, write and delete permissions on the listed files and folders.

Display read-write permissions of files and folders

So before you want to operate on a file and folder, you can first know and maybe edit the permissions you have on a specific item.

List folder contents to a certain level/depth through Tree

Instead of listing all the contents of your directory, you can configure the tree command to display the tree to a certain level or depth. For example, level 1 in the tree command will only show the list of the given folder rather than any of its subfolders. Here is how to use the syntax:

The following command will display only the sub-directories (with the help of -d flag) of the current directory and not the further expanded tree.

List folder contents till a certain level

Make The Tree command print file listing containing a specific pattern

The tree command can only list the files containing a specific wild card pattern. Here is the syntax to specify the pattern:

In this example, I am using the tree command to list those files starting with the keyword “touch”:

Display files that match a certain name pattern

Make the Tree command avoid printing some selective file names

You can also use the tree command to list everything but the files containing a specific wild card pattern.

The following command will list all the files and folders except for the one containing the “snap” keyword.

avoid printing some selective file names

If you want to print the result of the tree command to a file, you can use the following syntax:

The following command will print the list of all files and folders of the Pictures folder to an HTML file named myfile.html

$ tree ./Pictures -o myfile.html

Tree Help

The tree command is much more helpful than the usage we have described. You can explore the command further by viewing the help of the tree command as follows:

Tree command help

By using the flags, we described and combinations of these flags, you can master the tree command even more!

About This Site

Vitux.com aims to become a Linux compendium with lots of unique and up to date tutorials.

Latest Tutorials

Источник

Оцените статью
Adblock
detector