Gentoo Forums
Gentoo Forums
Gentoo Forums
Quick Search: in
ls and sorting -- SOLVED
View unanswered posts
View posts from last 24 hours

 
Reply to topic    Gentoo Forums Forum Index Other Things Gentoo
View previous topic :: View next topic  
Author Message
Fred Krogh
Veteran
Veteran


Joined: 07 Feb 2005
Posts: 1036
Location: Tujunga, CA

PostPosted: Fri Mar 05, 2010 2:42 pm    Post subject: ls and sorting -- SOLVED Reply with quote

ls no longer sorts names the way it used to, and it has fouled up some scripts of mine. The first sign of problems for me was caused by sorting dprpl.f after dprpl1.f. But after seeing the sort order divadb.f,
diva.f, divag.f, I'm at a loss to know what to expect from ls. Does anyone know how this works or of an alternative to ls to get a sorted list of names into a file. Thanks,
Fred


Last edited by Fred Krogh on Fri Mar 05, 2010 3:17 pm; edited 1 time in total
Back to top
View user's profile Send private message
Frustie
Tux's lil' helper
Tux's lil' helper


Joined: 31 Aug 2007
Posts: 102
Location: My own little planet.

PostPosted: Fri Mar 05, 2010 3:14 pm    Post subject: Reply with quote

there are a couple of different ways of sorting with ls, see man ls :

Code:
      --sort=WORD
              sort by WORD instead of name: none -U, extension -X, size -S, time -t, version -v


but you could also pipe the output of ls through sort

# ls | sort

see man sort for more options.
Back to top
View user's profile Send private message
Fred Krogh
Veteran
Veteran


Joined: 07 Feb 2005
Posts: 1036
Location: Tujunga, CA

PostPosted: Fri Mar 05, 2010 3:17 pm    Post subject: Reply with quote

Thanks, passing the output through sort does the job.
LC_ALL=C ls ...
also works. Thanks.
Back to top
View user's profile Send private message
i92guboj
Bodhisattva
Bodhisattva


Joined: 30 Nov 2004
Posts: 10315
Location: Córdoba (Spain)

PostPosted: Fri Mar 05, 2010 5:19 pm    Post subject: Re: ls and sorting -- SOLVED Reply with quote

Fred Krogh wrote:
ls no longer sorts names the way it used to, and it has fouled up some scripts of mine. The first sign of problems for me was caused by sorting dprpl.f after dprpl1.f. But after seeing the sort order divadb.f,
diva.f, divag.f, I'm at a loss to know what to expect from ls. Does anyone know how this works or of an alternative to ls to get a sorted list of names into a file. Thanks,
Fred


First, if you want your script to be reliable, you shouldn't be using ls at all for that purpose. I'll spare you the explanation and post a link instead, in case you are interested.

http://mywiki.wooledge.org/ParsingLs

Besides that, you are also assuming that the ls output will come given into a very specific format, which is even insaner being ls the top-one aliased and modded command line tool. You shouldn't be expecting ls to act at all in a given way. It will vary from system to system. I can't be too specific without knowing anything about your script though.
Back to top
View user's profile Send private message
Fred Krogh
Veteran
Veteran


Joined: 07 Feb 2005
Posts: 1036
Location: Tujunga, CA

PostPosted: Fri Mar 05, 2010 5:26 pm    Post subject: Reply with quote

Thanks for this. In my case, there is no chance that names will contain anything other than letters numbers and a single '.'. It is also very doubtful that the script will be run on any other machine, but of course the future can hold surprises.
Back to top
View user's profile Send private message
Display posts from previous:   
Reply to topic    Gentoo Forums Forum Index Other Things Gentoo All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum