Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Option to group directories last #24

Open
jzinn opened this issue Nov 19, 2021 · 3 comments
Open

Option to group directories last #24

jzinn opened this issue Nov 19, 2021 · 3 comments

Comments

@jzinn
Copy link

jzinn commented Nov 19, 2021

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!

@jzinn
Copy link
Author

jzinn commented Nov 19, 2021

I just noticed there's a request for directories first: #9

@jzinn jzinn changed the title Option to group files before directories Option to group directories last Nov 19, 2021
@jez
Copy link
Owner

jez commented Nov 19, 2021

Thanks. I mostly don't work on this project anymore, because tree has a --fromfile option built in that behaves just as well or better than as-tree. You can use tree --dirsfirst --fromfile instead.

@jzinn
Copy link
Author

jzinn commented Nov 19, 2021

Woah! I did not know about tree --fromfile. Thanks for that!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants