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.

Rick
  • 205

1 Answers1

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
    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 '23 at 03:41