I'm trying to tweak the output of the command 'ls -l' to show directories first without running two separate 'ls' commands. I currently have this command: `ls -l | sort -k1.1,1.1 -k9,9`. However, I want to swap the positions of the files and directories in the output. For example, I want to see all directories listed first in alphabetical order, followed by all files, also in alphabetical order. I've tried using `ls -l | sort -k1.1,1.1 -r -k9,9`, but while that gives me the correct layout, it reverses the order of the lists. Any suggestions?
3 Answers
It seems like you're aiming for a specific ordering. Could you clarify how exactly you want it sorted? It would help to have a clear idea of your end goal.
If you're looking for something simple, you can try reversing the output using the `tac` command, which flips the line order. However, I'm not sure how well that works in your scenario.
Unfortunately, I don't have the `tac` command available since I'm using macOS BSD.
You might want to try using `ls -l --group-directories-first`. It organizes directories before files by default, which sounds like what you're after!
I don't have that option available on my system and I'm not looking to download extra tools.

Sure! I want the output to first sort the lines by whether they're directories (denoted by 'd') or files (denoted by '-') and then list the directories by their names, followed by the files sorted by their names as well. Basically, directories first in order, then files in order.