Download your favorite Linux distribution at LQ ISO.
Go Back > Forums > Linux Forums > Linux - General
User Name
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.


  Search this Thread
Old 01-28-2006, 11:46 AM   #1
Senior Member
Registered: Dec 2004
Location: Lee, NH
Distribution: OpenSUSE, CentOS, RHEL
Posts: 1,794

Rep: Reputation: 46
Lightbulb Getting a DOS-style sorted directory listing

A question I've seen asked from time to time is "how do I get a DOS-style directory listing?" - that is, with the directories listed first, followed by the filenames in alphabetical order.

I am waiting for some downloads to finish so I figured I'd work out a solution and post it here.

It's actually easy: using GNU sort (from coreutils)

First you need to run ls then pipe it through sort, like so:

 ls -l | sort -k 1.1,1.2 -k 8.1 -f | less
What happens here is this:

`ls -l` provides the "long" directory listing

`sort` is the utility which will sort the lines output by ls

`-k 1.1,1.2` sorts by the first field, first and second column. Since directory files have 'd' as the first character for this field, directories will be listed first

`-k 8.1` applies a second sort by the eighth field (filename), starting from the first column

`-f` tells sort to ignore case, just as the DOS `dir` command does.

Now, by default, `dir` is aliased to `ls -l` in many (all?) distributions. If you wish to change this edit /etc/bash.bashrc and search for "alias dir". Once you find it then change 'ls -l' to the command specified above. Now you have a DOS-style directory sort which lists directories first, followed by a case-insensitive alphabetical listing of files.

In a similar fashion, you can get a DOS-style directory listings where the directories are grouped but sorted by date/time, and files are grouped but sorted by date-time (You can extend it further by also adding the filename sort back, for two files with the same timestamp).

To sort by date/time you just modify the above command slightly:

ls -l | sort -k 1.1,1.2 -k 6.1 -k 7.1 -f | less
And to add an additional sort by filename (after sorting FIRST by directory/file byte, THEN by date, THEN by time) run the following command:

ls -l | sort -k 1.1,1.2 -k 6.1 -k 7.1 -k 8.1 -f | less

Last edited by KimVette; 01-28-2006 at 12:06 PM.
Old 01-29-2006, 09:42 AM   #2
LQ Guru
Registered: May 2005
Location: Atlanta Georgia USA
Distribution: Redhat (RHEL), CentOS, Fedora, CoreOS, Debian, FreeBSD, HP-UX, Solaris, SCO
Posts: 7,831
Blog Entries: 15

Rep: Reputation: 1668Reputation: 1668Reputation: 1668Reputation: 1668Reputation: 1668Reputation: 1668Reputation: 1668Reputation: 1668Reputation: 1668Reputation: 1668Reputation: 1668
ls -R was too easy?
Old 01-29-2006, 11:22 AM   #3
Senior Member
Registered: Dec 2004
Location: Lee, NH
Distribution: OpenSUSE, CentOS, RHEL
Posts: 1,794

Original Poster
Rep: Reputation: 46
You did not even read the command (or even the post in general), did you?

This is not a recursive directory listing.

Last edited by KimVette; 01-29-2006 at 11:31 AM.


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Apache Directory Listing Of NFS Mount, cannot view directory list via apache luqmana Linux - Networking 2 12-19-2005 06:03 AM
directory size listing with ls mikemrh9 Linux - General 2 01-05-2005 12:04 PM
listing files in a directory Linh Programming 7 05-17-2004 04:35 AM
Directory Listing mattbeekler Linux - Software 8 07-22-2003 02:27 PM
Listing a web directory Radicalm16 Linux - Software 3 06-14-2003 11:05 AM > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 12:42 PM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration