Skip to content

Option to group directories last #24

Open
@jzinn

Description

@jzinn

When finding paths with find and fd, I often pipe the results into a script I wrote called pathsort that groups files before directories.

Piping the output of pathsort into as-tree has no effect, as as-tree sorts its input.

It would be nice if as-tree had an option to group files before directories.

Gnu ls has the --group-directories-first option. tree has the --dirsfirst option. A long flag like those would be fine, but a one letter short flag would be ideal. Neither has an option to group files first.

For list output, I can see both groupings to be useful, but the existing tools only offer directories first.

For find and tree output, for some reason grouping files before directories makes more sense to me. I'm not sure why. Maybe it's because once you've seen the files in the directory, you can be done thinking about that directory for the remainder of the output.

This is my pathsort script:

#!/bin/sh
set -euo pipefail

# 1.  Replace the last `/` with `/ /`
# 2.  Sort
# 3.  Replace the `/ /` with `/`
pathsort()
{
        sed -E 's/(.*)\//\1\/ \//' | sort | sed -E 's/(.*)\/ \//\1\//'
}

case "${1-}" in
        -fd )
                # It would be nice if `fd` had an option to prepend `./` to all relative paths,
                # so that all paths have at least one `/` in them.
                sed 's/^/.\//' | pathsort
                ;;
        * )
                pathsort
                ;;
esac

Off topic

I have this in my shell's rc file:

fdtree() { fd . -H -E .git "$@" | as-tree ; }

I've been using fdtree more than tree lately because I can write fdtree -E aaa -E bbb to exclude using multiple patterns, while tree can only have one -I ignore pattern.

Thanks for as-tree. I love it!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions