3

I've been trying to sort output with either tree or find, and end up getting about the same results each time of drilling down to deeper directories before files higher up in the tree. What I'm trying to do is store the directories of interest in a lua table with the directory as the key for sub tables of files.

I'm using tree -fi -noreport {Foo,Baz}

Foo
Foo/Foo.Build.cs
Foo/Private
Foo/Private/Foos
Foo/Private/Foos/Bars
Foo/Private/Foos/Bars/BaseBar.cpp
Foo/Private/Foo.cpp
Foo/Private/FOOPCH.h
Baz
Baz/Baz.Build.cs

I want the results to look like:

Foo
Foo/Foo.Build.cs
Foo/Private
Foo/Private/Foo.cpp
Foo/Private/FOOPCH.h
Foo/Private/Foos
Foo/Private/Foos/Bars
Foo/Private/Foos/Bars/BaseBar.cpp
Baz
Baz/Baz.Build.cs

tree fi -noreport {Foo,Baz} | sort -t '/' isn't working. I tried using the solution from this thread, which is tree | awk '{print gsub("/","/"), $0}' | sort -n | cut -d' ' -f2-. If I do that I lose the relationship between directories and files.

How can I sort so files at the same depth show up before directories?

  • have you run find with the -depth option? – Jeff Schaller Mar 2 '17 at 0:25
  • Yeah, I've done that it doesn't exactly give me what I'm looking for. – ZeroPhase Mar 2 '17 at 1:08
3

Display the directory names first, then jump in and display only filenames:

find . ! -name . -type d -print -exec sh -c 'find "$1" -maxdepth 1 ! -type d' {} {} \;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.