4

By default, tree shows this:

$ tree
.
├── Package.resolved
├── Package.swift
├── Sources
│   └── SwiftClientSDK
│       ├── OpenAPI.yaml
│       ├── SwiftClientSDK.swift
│       └── openapi-generator-config.yaml
└── Tests
    └── SwiftClientSDKTests
        └── SwiftClientSDKTests.swift

I would like for it to show the contents of the cwd like this:

Package.resolved
Package.swift
Sources
└── SwiftClientSDK
    ├── OpenAPI.yaml
    ├── SwiftClientSDK.swift
    └── openapi-generator-config.yaml
Tests
└── SwiftClientSDKTests
    └── SwiftClientSDKTests.swift

Nothing I see in the man page suggests this is possible.

1
  • Does it have to be a single invocation of tree? I think you could invoke tree from within find, for example. Jun 13, 2023 at 21:23

1 Answer 1

11

It doesn't look like it

tree -- *

comes close, but it complains about those files that are not of type directory and follows symlinks to directories.

You could always post-process the output to remove that extra level of indentation:

tree -C | sed '1d;$!s/....//'

Here deleting the 1st line, and removing the first four characters (substituting the first four characters with nothing) on all but (!) the last ($) line. -C option to tree is to keep the colours now that its output doesn't go to a terminal.

You could create a function for it:

ltree() (
  set -o pipefail
  if [ -t 1 ]; then
    set -- -C "$@"
  fi
  tree "$@" | sed '1d;$!s/....//'
)

Note that it won't work properly if passed more than one directory (as in ltree dir1 dir2). In that case, you likely want the normal tree output.

1
  • 1
    Yeah, I did something like that with cut -c 5-, which worked, but truncated the first bits of the summary line as well. Then I downloaded the source and started adding support for this feature directly ;)
    – Rick
    Jun 14, 2023 at 3:41

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .