How can I sort ‘ls -l’ output with directories first without running multiple commands?

0
6
Asked By CuriousCat42 On

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

Answered By CodeWizard99 On

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.

UserWithQuestions -

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.

Answered By ShellSavvy On

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.

MacUser123 -

Unfortunately, I don't have the `tac` command available since I'm using macOS BSD.

Answered By TechieTim10 On

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!

UserWhoCan -

I don't have that option available on my system and I'm not looking to download extra tools.

Related Questions

LEAVE A REPLY

Please enter your comment!
Please enter your name here

This site uses Akismet to reduce spam. Learn how your comment data is processed.