Is there a nice way to sort directory contents (including hidden files) in the shell? Basically i'd like to be able to ls
directories just its done in my GUI file manager. In a typical directory, the output is as such:
.a_hidden_dir
.b_hidden_dir
.c_hidden_dir
a_dir
b_dir
c_dir
.a_hidden_file
.b_hidden_file
.c_hidden_file
a_file
b_file
c_file
Of course ls
has the --group-directories-first
option, but this only gets us part of the way there as sort
ignores the leading .
, it does not sort hidden files to the top.
I'd like to be able to sort output from ls
, find
, or other list of paths in such a way. Does anyone know a good way to do this - maybe a sort -k
KEYDEF?
Right now I'm doing something like this (it assumes directory names have a slash append to them):
pathsort(){
input=$(cat)
(
awk '/^\..+\/$/' <<<"$input" | sort
awk '/^[^.].+\/$/' <<<"$input" | sort
awk '/^\..+[^/]$/' <<<"$input" | sort
awk '/^[^.].+[^/]$/' <<<"$input" | sort
) | sed 's/\/$//'
}
\ls -Ap | pathsort
The above code gets the job done, but it is far from ideal. Please tell me there is a better way...