Show directory command 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/^/ /’

Читайте также:  Команда очистки терминале linux команда

@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.

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.

Читайте также:  Запуск скрипта при выключении linux

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 can I see folders from terminal?

list directories themselves, not their contents. To explain this, consider what happens if we type ls */ . ls goes one layer down, into each subdirectory, and lists all the files in each of those sequentially

*/ is known as a «glob» in UNIX. (see Wikipedia for more details). But basically, it means «any file name ending in a forward slash.» In UNIX, directories are really just files, fundamentally. But they are specially named ending in a forward slash so the operating system knows they are directories (or folders, in everyday-person-speak). And the asterisk * is technically a wildcard standing for «any string of characters.»

What is a glob?

This paragraph will not pertain specifically to your question, but if you’ve never read about this, it’ll be good to see it. Globs are different from Regular Expressions, as (partially) explained in What is the difference between Regular Expressions and Globbing? There have been whole books written on regular expressions, but tl;dr there are a bunch of different ways to encode pattern-matching expressions.

Читайте также:  Узнать пароль от своего wifi linux

How to show hidden folders as well? ls -d .*/ shows only hidden folders. How to view BOTH hidden and non-hidden folders? I can only think of ls -d */ .*/ Anything better?

As I am a very inexperienced user I love this website. It tells you all you want to know about bash commands, in some cases it even gives you examples. Very useful.

  • ls to list the files
  • ls -a to include hidden files
  • ls -l for a long listing format
  • .

lists one directory per line.

If you want to be able to distinguish folders from files easily, use something like ls -alhF . I usually define l as an alias for that, ie. I put the line alias l=’ls -alhF’ in my .bashrc .

include hidden files ‘-a’ grep ^d get start with ‘d’ wich means directory when name starts with dot directory is hidden

to list recursively see this

find ./ -type d | less ./ . means starts find in current folder -type . indicates the type to be searched d . means directory | . redirects the command less . enables paging using the keyboard arrows and leave with q 
$ ls Desktop Downloads hadoop Pictures Templates Documents examples.desktop Music Public Videos 
$ ls ~ /usr /home/hadoop1: Desktop Downloads hadoop Pictures Templates Documents examples.desktop Music Public Videos /usr: bin games include lib local locale sbin share src 
$ ls -l total 48 drwxr-xr-x 2 hadoop1 hadoop1 4096 Jul 1 2017 Desktop drwxr-xr-x 2 hadoop1 hadoop1 4096 Jul 1 2017 Documents drwxr-xr-x 2 hadoop1 hadoop1 4096 Jul 1 2017 Downloads -rw-r--r-- 1 hadoop1 hadoop1 8980 Jul 1 2017 examples.desktop drwxr-xr-x 10 hadoop1 hadoop1 4096 Jul 1 2017 hadoop drwxr-xr-x 2 hadoop1 hadoop1 4096 Jul 1 2017 Music drwxr-xr-x 2 hadoop1 hadoop1 4096 Jul 1 2017 Pictures drwxr-xr-x 2 hadoop1 hadoop1 4096 Jul 1 2017 Public drwxr-xr-x 2 hadoop1 hadoop1 4096 Jul 1 2017 Templates drwxr-xr-x 2 hadoop1 hadoop1 4096 Jul 1 2017 Videos 

Источник

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