By default the ls command sorts just by filename, but I want directories to appear before other file types. I might even want files to be sorted by extension, like the way Windows explorer sorts by the type column. Is there a way to do something similar with ls?
|
feedback
|
migrated from stackoverflow.com Aug 7 '09 at 22:34
This question came from our site for professional and enthusiast programmers.
|
I think the complete answer is more of a combination of the above. -X (later --sort=extension) has been supported in Linux since at least FC3 and will sort based on extension. --group-directories-first was added more recently (maybe around FC8?). However, combining the two doesn't seem to work (at least on FC8). The primary issue seems to be with the use of singular primary sort keys. See http://www.mail-archive.com/bug-coreutils@gnu.org/msg12225.html for some discussion of it. | |||
|
feedback
|
|
On Linux,
( | |||
feedback
|
|
If you're running on Linux, GNU
| |||
|
feedback
|
|
If you're not on linux,
should sort directories first (let me know if I'm wrong). Doesn't sort by extension, though: you have to make the awk statement a lot busier if you want to do that ... To also make it work with names containing spaces, I would probably replace the awk with something like | |||||||||||||
feedback
|
|
On bash, this will sort of work: ls | rev | sort | rev From
So 1. ls gives its output, with any flags you want 2. each line is reversed 3. then they're sorted 4. and reversed again 5. like this:
Or, more to the point, as below. They're sorted by last character, then next-to-last, etc. All the .rtf files, for example, are listed together, after a .save file and another file with no extension whose name ends in 'e'. Then come .png files, and so on. This will also work with ls -l, because the extension is normally the last thing on the line (exceptions if you have lines like "tmp@ -> /home/jones/tmp", where links are followed by their targets). $ ls | rev|sort|rev cslu1 ls.mp2 ls.mp3 ls.mp4 trees_110214-15 PAT CSLU Proxy Form.doc finannbyid toannbyid 101209ssi.txt.save to_annotate_size Matas-time-by-week-integration2.rtf cyp3.rtf data-dir-scan.perl.doc.rtf whence-r21-numid.rtf platypus.rtf Screen shot 2011-01-21 at 2.17.50 PM.png emacs print help.png log new_month_log special Google-ngram-critique.html perl_path.html nl DWE_BEN_89808.2.ann foo d.o.foo 100811_from_iMac_Documents_in_dock.zip to-palikir.zip tmp file-cleanup bar data-scan-docs cmp-mg-ann-numids finished_numids to_annotate_numids manls.ps Mike_address_ticket cyp2.out cyp3.out locate-cyp.out manls.out DWE_BEN_89808.2.text tag2.txt l2.txt du-h-d3.txt finished_ann_numids_110407_1714.txt finished_all_numids_110407_1718.txt data-dir-scan.perl.doc.txt whence-r21-numid.txt finannid.txt toannid.txt b9-workspace-anndiff.txt tag.txt duh.txt d.o-mail.txt safextn.txt mg3longhdr.txt finished_numids.txt 41692-langnames.txt TimeAnnotationGuidelines.txt 41langs.txt thing4-homedir-links.txt bnlinks.txt grants.txt mata-file-reports.txt logx.txt logx b9-workspace-anndiff.txt~ bnlinks.txt~ | |||||
|
feedback
|