Get directory tree linux

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.

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.

Читайте также:  Linux команда посмотреть все диски

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 directory tree using Linux

A directory tree on a Linux system is a way to see all of the directory and sub directories in a provided filesystem path. In this tutorial you will learn how to print directory tree in Linux terminal and GUI.

This type of overview can be difficult to achieve in GUI file browsers or by simply changing directories on the command line. But there are a few tools in Linux that give us a birds eye view of how our directories and their contents are structured.

Читайте также:  1с сервер linux postgresql установка

In this tutorial, you will see various ways to print a directory tree using command line or GUI on a Linux system.

In this tutorial you will learn:

  • How to use tree command and its options
  • How to use ls , du , and find commands to print directory tree
  • How to install tree and baobab
  • How to use Disk Usage Analyzer GUI utility

How to print directory tree using Linux

Software Requirements and Linux Command Line Conventions
Category Requirements, Conventions or Software Version Used
System Any Linux distro
Software tree, ls, du, find, Disk Usage Analyzer
Other Privileged access to your Linux system as root or via the sudo command.
Conventions # – requires given linux commands to be executed with root privileges either directly as a root user or by use of sudo command
$ – requires given linux commands to be executed as a regular non-privileged user

Let’s just dive right into the best tool for the job. The tree command is not usually included by default on Linux distros, but it is easily installable, and is perfect for lising the directory tree of any path.

In case you do not already have access to the command, you can use the appropriate command below to install tree with your system’s package manager.

Now that you can use the tree command, see some of the examples below to learn how it works.

    The most simple way to print a directory tree is by using the tree command and the path you would like to print a directory tree for. If used without specifying a directory, it will print the structure for your present working directory. We recommend piping the output to less if your directory contains many files and subdirectories.

Using the tree command to list directory tree on Linux

If you want to list only directories, use the -d option.

Limiting our tree output to a certain number of subdirectories

If you want to include hidden files and directories in the tree output, append the -a option.

Although tree has to be the ideal command for listing directory trees, Linux comes with a few default commands that can also do the job, namely du , ls , and find . See some of the examples below to learn how to use these commands to list directory trees.

    The find command will list all files and directories in a given path. To search the present working directory, just use . .

Using find command to print directory tree on Linux

If you only want the find command to list directories and subdirectories, use the -type d option.

Listing directories recursively with the ls command

Listing directory tree and sizes with du command

See our other guide on List all directories and sort by size for more ways to use du to print directory trees.

Sometimes, it is easier to visualize a directory tree if we use a GUI utility. One such application is called Disk Usage Analyzer, but it may not be installed by default on your Linux distro. Use the appropriate command below to install it with your system’s package manager.

You can use the appropriate command below to install Disk Usage Analyzer with your system’s package manager.

Читайте также:  Hardware info for linux

To install Disk Usage Analyzer on Ubuntu, Debian, and Linux Mint:

To install Disk Usage Analyzer on Fedora, CentOS, AlmaLinux, and Red Hat:

To install Disk Usage Analyzer on Arch Linux and Manjaro:

After it is installed, search for and open the application.

Open Disk Usage Analyzer from the applications launcher

When the program opens, it will ask if you want it to scan the home directory or an entire disk. You can also click the options menu (three stacked lines) for the ability to scan a particular folder.

Choose to scan the home folder, whole disk, or select a particular directory

Make your selection and the utility will begin scanning for files. Once it finishes scanning for content, it will give you a full readout of how your hard disk space is being distributed to various directories on your system. There is also a graphical representation which you can move your mouse cursor over to get an even better idea. It lists directories by size, so you can quickly determine what’s chewing up the most disk space.

Disk Usage Analyzer shows how storage space is being used in different directories

Use the arrows next to each directory to expand a list of files and subdirectories, effectively viewing the directory tree of any path you want

Closing Thoughts

In this tutorial, we saw how to print a directory tree on Linux from command line and GUI. The tree command is our best recommendation, as it is meant especially for this purpose and comes loaded with a lot of options. But Linux also includes the default ls , find , and du commands, which can be equally as useful.

If you do not want to fiddle with the command line, then Disk Usage Analyzer works well at not only printing directory trees, but showing you how much space each directory is consuming.

Comments and Discussions

Источник

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,

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.

Источник

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