Using Nushell I want ls to list directories before files. Additionally, I want symbolic links to be sorted correctly: If it is a link to a directory, it should be included in the list of directories. And if it is a link to a file, it should be in the list of files.
I developed the following script:
let is_dir = {|file|
$file.type == 'dir' or ($file.target | to text | str ends-with '/')
};
let dirs = ls --all --long
| filter $is_dir
| sort-by name --ignore-case;
let files = ls --all --long
| filter {|file| not (do $is_dir $file)}
| sort-by name --ignore-case;
$dirs ++ $files
| select name
But that seems to be too much code for such an easy request.
- Can the code be simplified somehow?
1.1 Is there a better way to find out whether the symlink points to a directory? Using$file.target | to text | str ends-with '/')was the only way I saw, but I'm not proud of that solution.
1.2 Also,filter {|file| not (do $is_dir $file)}is not the best code I ever wrote. How can I do that better? - Is there a better approach to achieve the same?
This question is about Nushell, not other shells. :-)