LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 11-15-2005, 01:05 PM   #1
gvaught
Member
 
Registered: Sep 2003
Location: MidWest
Distribution: Debian GNU/Linux 8 (jessie)
Posts: 199

Rep: Reputation: 30
'ls' in Linux vs. 'dir' in M$


Okay, this is not intended to start some big "Linux vs. Windows" debate. They both have their strengths and, without explicitly stating my opinion and preference, they both have appropriate functionality for their advocates. And I'm no n00b to either OS, although I have a LOT more to learn about Linux before it is my primary OS. And Linux will never (and I don't think it should) run Windows games like Windows. I do hope to find acceptable FPS as well as more of other games on Linux in the future. But I digress.

My concern and question is very specific:
- How do I get ls to list directories before other file types, as dir does in Windows?

That is all - I want to be able to sort the results list so that directories are at the top OR bottom of the list, rather than intermixed with the other file-types.
 
Old 11-15-2005, 01:16 PM   #2
bosewicht
Senior Member
 
Registered: Aug 2003
Location: Houston, TX
Distribution: Arch
Posts: 1,381

Rep: Reputation: 47
try
ls -F | grep /
 
Old 11-15-2005, 01:39 PM   #3
gvaught
Member
 
Registered: Sep 2003
Location: MidWest
Distribution: Debian GNU/Linux 8 (jessie)
Posts: 199

Original Poster
Rep: Reputation: 30
That's good info (thanks, sincerely) but it does not accomplish what I'm trying to do - which is get my listing of files that are not directories listed with the directories, yet sorted apart from the directories.
 
Old 11-15-2005, 01:45 PM   #4
XavierP
Moderator
 
Registered: Nov 2002
Location: Kent, England
Distribution: Debian Testing
Posts: 19,192
Blog Entries: 4

Rep: Reputation: 475Reputation: 475Reputation: 475Reputation: 475Reputation: 475
Why not use the "dir" command?
 
Old 11-15-2005, 08:20 PM   #5
twsnnva
Member
 
Registered: Oct 2003
Location: Newport News, Va
Distribution: Debian
Posts: 246

Rep: Reputation: 30
ls -u
 
Old 11-15-2005, 09:07 PM   #6
PTrenholme
Senior Member
 
Registered: Dec 2004
Location: Olympia, WA, USA
Distribution: Fedora, (K)Ubuntu
Posts: 4,187

Rep: Reputation: 354Reputation: 354Reputation: 354Reputation: 354
Or ls -l | sort. That even puts the total at the bottom. (Assuming that "d" sorts before "-" in your selected character scheme.)
 
Old 11-16-2005, 01:52 AM   #7
Dark_Helmet
Senior Member
 
Registered: Jan 2003
Posts: 2,786

Rep: Reputation: 374Reputation: 374Reputation: 374Reputation: 374
gvaught:
You're not the first person to ask about this. As far as I know, ls has no option to list directories first. and files afterward. You'll need to combine ls with other utilities or go a completely different route.

I think the closest so far has been PTrenholme's suggestion. I didn't have any luck getting dir or ls -u to do what you need on my system. But using sort gives some mixed results sometimes. The owner, group, filesize, and other fields will influence the sort and might put the listing out of alphabetical order. If what I assume is right, you want directories first (in alphabetical order) followed by files (in alphabetical order).

I can't think of any "clean" way to do it off-hand. However, here's a nasty way (which you could shove into an alias):
Code:
( ls -lQ | grep    "^d" | awk -F '"' '{ print "\""$2"\"" }' | sort;    \
  ls -lQ | grep -v "^d" | awk -F '"' '{ print "\""$2"\"" }' | sort ) | \
  xargs ls -ldU
You can copy it directly to a command line and try it if you like. Be careful, because this behemoth of a command will break if you have any filenames that contain double quotes as a part of the filename. There's probably a way to work around that, but this works reasonably well.

Anyway, there may be a cleaner way to do it, but it'll likely require a shell/perl/python script. Once it's put in an alias, it won't matter how long it is

Last edited by Dark_Helmet; 11-16-2005 at 01:54 AM.
 
Old 11-16-2005, 02:48 PM   #8
PTrenholme
Senior Member
 
Registered: Dec 2004
Location: Olympia, WA, USA
Distribution: Fedora, (K)Ubuntu
Posts: 4,187

Rep: Reputation: 354Reputation: 354Reputation: 354Reputation: 354
Picky, picky, picky

Well, sort has several options. See info coreutils sort for details.

Try this code to produce a directory listing with the files sorted by name:
Code:
 ls -l | sort -k 1.1,1.1 -fk 9
The -k 1.1,1.1 tells sort to sort first by the first field, using only the first character.
the -fk 9 tells it to sort duplicates by the ninth field, ignoring case.

O.K.?

Oh, here's another example, which produces a somewhat M$ type listing:
Code:
$ ls -l | sort -k 1.1,1.1r -fk 9 | gawk '/^t/{};/^-/{print $9};/^d/{print "[" $9 "]"}'
And here's the output from my ~/ directory.
Code:
[Applications]
[backup]
[bin]
[Books]
[C]
[cc]
[cmdcons]
[Desktop]
[Downloads]
[dump]
[etc]
[gcd]
[GNUstep]
[Links]
[News]
[Papers]
[Pictures]
[Projects]
[Samsung]
[Shell]
[Templates]
[Text]
[tmp]
[winetools]
40600016.AVI
Address
anaconda-ks.cfg
autoexec.bat
bashprompt-install.log-6044
bookmarks.html
cookies.txt
floppy.img
history.scilab
Home
House
kdelibs-3.4.0-0.fc3.0.i386.rpm
KPlayer
ks.cfg
Last3.awk
RealPlayerFavorites
temp2
test.py
test.sh
wine-save.tar.gz
$
Note that the AWK code just prints field 9, so file names containg blanks will be truncated. (Hey, it's quick-and-dirty proof-of-concept code, not intended for production. )

Last edited by PTrenholme; 11-16-2005 at 03:12 PM.
 
Old 11-16-2005, 07:32 PM   #9
gvaught
Member
 
Registered: Sep 2003
Location: MidWest
Distribution: Debian GNU/Linux 8 (jessie)
Posts: 199

Original Poster
Rep: Reputation: 30
WOW.

I did not, and never will, intend to start an argument. And I hope that this is not the case. I hope the exchange is all about educating users (like me) and not a bruised ego.

Don't get me wrong - I love an animated debate. As long as it's just that - debate. An exchange of facts, or opinions supported by facts. But there are enough angry people starting fires all over the web to have LQ get any smokier on my account.

In any event, this is obviously something that CAN be done - just not as simply and easily as dir. I thank everyone for their input and hope to hear from you in the future.
 
Old 11-16-2005, 07:48 PM   #10
Dark_Helmet
Senior Member
 
Registered: Jan 2003
Posts: 2,786

Rep: Reputation: 374Reputation: 374Reputation: 374Reputation: 374
An argument? Nah, I don't think so. At least, I didn't take it as such. And I wasn't "picking a fight" either. From my perspective, I was just saying a plain sort is a good start. Things get developed by a process of refinement. That's how everything works.

Actually, I never even thought to try multiple -k options to sort. I tried to do "-k 1,9" and that got me nowhere. So then I went on to develop the other method.

There are virtually an infinite number of ways to do anything. And there's usually at least one that's "better." In my case, there's usually 100 better ways.
 
Old 11-17-2005, 01:42 AM   #11
Emmanuel_uk
Senior Member
 
Registered: Nov 2004
Distribution: Mandriva mostly, vector 5.1, tried many.Suse gone from HD because bad Novell/Zinblows agreement
Posts: 1,606

Rep: Reputation: 53
Quote:
In any event, this is obviously something that CAN be done - just not as simply and easily as dir
I think you need to see it from the flexibility view point.
ls is not dir,
old habits dies hard. I programmed an alias called dirr that always tell me the HD space left after a ls operation
ls is more flexible
ls can be made to behave like dir, this a programation / seting issue

But once it is implemented it is as simple as typing dir or dirr or whatever.
It is as easy as dir if implemented.

I think linux score very high on this

If you want a clone of dir behaviour, this is another story. You can create a patch,
a utility, whatever, and it may get into some distro, and it will be there for all.

I agree with what you may be saying: a utility cloning dir maybe a nice thing,
and may help to win over more people to linux. At the same time if you know
about dir then you know of CLI, then you know better than just clicking,
so maybe a clone is not that important. If it is important to you, well create
a sourceforge project. I will give you my "patch" for the HD space

I cannot remember the link but there is ftp repository in the uk with
all sort of short scripts to erase, move, shred

gvaught, did you notice how by default in linux the rename function does not work the same
AFAIK you cannot do mv file??name.jpg file??name.jpeg (have no pc here to test it)
(or one of this command with * and ? that can be done with dos is very different within linux)

Also there has been tutorial around about the advanced ls usage because the gnu ls
has not well known flags etc to do with date format (<> dates whether file is > or < 6 month old)
 
Old 11-17-2005, 02:51 AM   #12
enemorales
Member
 
Registered: Jul 2004
Location: Santiago, Chile
Distribution: Ubuntu
Posts: 410

Rep: Reputation: 31
Quote:
Originally posted by gvaught
In any event, this is obviously something that CAN be done - just not as simply and easily as dir. I thank everyone for their input and hope to hear from you in the future. [/B]
Well, ease is a relative thing. Would you say that implementing "ls" in Windows will be easier than "dir" in Linux, for instance?
 
Old 11-18-2005, 06:44 AM   #13
gvaught
Member
 
Registered: Sep 2003
Location: MidWest
Distribution: Debian GNU/Linux 8 (jessie)
Posts: 199

Original Poster
Rep: Reputation: 30
Okay, okay. ls is *not* a dir. And Linux is not simply a Windows replacement.

Yes, I do understand that ls is more flexible and therefore, in my book, more powerful. I've read a lot of good ideology, which I happen to agree with, over the last few days. I have an entirely new perspective on what it is I could and should be doing as a Linux user. Not the least of which is build my own tools.

I appreciate all of the feedback and help - both programmatically and philosophically. As with most things in this world, just about the time I started feeling like I was beyond beginner or n00b, I find out that there are not enough hours in the day or years in my life to get beyond padwan.

At this point, I have to thank George Lucas for adding that word to our vocabulary. Whether you liked the movies or not, the concept is valid and it is expressed with two simple, little syllables.
 
Old 11-19-2005, 04:21 AM   #14
linmix
Senior Member
 
Registered: Jun 2004
Location: Spain
Distribution: FC5
Posts: 1,993
Blog Entries: 1

Rep: Reputation: 46
What about 'mdir' (from the mtools package'). I know it's meant to be used to access ms-dos floppies, but maybe it can be tweaked to do the same for any linux directory.
 
Old 11-19-2005, 09:59 AM   #15
Emmanuel_uk
Senior Member
 
Registered: Nov 2004
Distribution: Mandriva mostly, vector 5.1, tried many.Suse gone from HD because bad Novell/Zinblows agreement
Posts: 1,606

Rep: Reputation: 53
Heard about mdir, not tried.

I mentioned that there were lots of utilities out there for file handling, dir, shreding
and what not. I remembered the address, here goes:
ftp://sunsite.unc.edu/pub/Linux/utils/file/
 
  


Reply



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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
How to paginate in the 'LS' command? jhecht Linux - Software 10 04-13-2010 06:15 AM
alias cs='cd $1; ls' milanche Linux - Newbie 11 09-13-2005 05:57 PM
howto make a dir shared that is not in my home dir Schmurff Linux - Newbie 2 06-19-2004 07:54 PM
'ls' command... sramelyk Slackware 7 10-01-2003 12:45 PM
general 'ls' question purpleburple Linux - General 8 04-17-2003 12:21 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 10:04 PM.

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