DaemonForums  

Go Back   DaemonForums > Miscellaneous > General software and network

General software and network General OS-independent software and network questions, X11, MTA, routing, etc.

Reply
 
Thread Tools Display Modes
  #1   (View Single Post)  
Old 11th April 2009
gosha gosha is offline
Spam Deminer
 
Join Date: Jun 2008
Location: China
Posts: 256
Thanked 0 Times in 0 Posts
Default ls sorting of numbered files

I don't really know where I should post this, but I remember Windows has the same behaviour, so I'm here.
Say I have some files named: 1 2 10 11 20
This is the order you expect, right?
Code:
$ ls
1  10 11 2  20
I have solved this in the past and present putting a zero in front of units, but I'm sure there's a better way to sort the files in the proper way. Is there?
Reply With Quote
  #2   (View Single Post)  
Old 11th April 2009
vermaden's Avatar
vermaden vermaden is offline
Administrator
 
Join Date: Apr 2008
Location: pl_PL.lodz
Posts: 1,052
Thanked 118 Times in 93 Posts
Default

maybe try that:
Code:
% ls | sort -n | tr '\n' ' '
__________________
religions, worst damnation of mankind
"If 386BSD had been available when I started on Linux, Linux would probably never had happened." Linus Torvalds

Linux is not UNIX! Face it! It is not an insult. It is fact: GNU is a recursive acronym for “GNU's Not UNIX”.
vermaden's: links resources deviantart spreadbsd
Reply With Quote
  #3   (View Single Post)  
Old 11th April 2009
gosha gosha is offline
Spam Deminer
 
Join Date: Jun 2008
Location: China
Posts: 256
Thanked 0 Times in 0 Posts
Default

thanks a lot
But why does ls behave like this?
Reply With Quote
  #4   (View Single Post)  
Old 11th April 2009
ephemera's Avatar
ephemera ephemera is offline
Knuth's homeboy
 
Join Date: Apr 2008
Posts: 537
Thanked 49 Times in 43 Posts
Default

The file names in 'ls' output are sorted in lexicographic order (dictionary order).
'2' is less than '10' numerically but in lex. order 2 comes after 10.

Last edited by ephemera; 11th April 2009 at 12:15 PM.
Reply With Quote
  #5   (View Single Post)  
Old 11th April 2009
gosha gosha is offline
Spam Deminer
 
Join Date: Jun 2008
Location: China
Posts: 256
Thanked 0 Times in 0 Posts
Default

but then, what about this:
Code:
$ ls | sort -n | tr '\n' ' ' 
a-1 a-10 a-2 a-20 $
Reply With Quote
  #6   (View Single Post)  
Old 11th April 2009
vermaden's Avatar
vermaden vermaden is offline
Administrator
 
Join Date: Apr 2008
Location: pl_PL.lodz
Posts: 1,052
Thanked 118 Times in 93 Posts
Default

If you want that kind of sorting, then number your files properly:
Code:
% :> asd_01
% :> asd_02
% :> asd_10
% :> asd_11
% :> asd_20
% ls -1 asd_*
asd_01
asd_02
asd_10
asd_11
asd_20
% ls asd_*
asd_01  asd_02  asd_10  asd_11  asd_20
__________________
religions, worst damnation of mankind
"If 386BSD had been available when I started on Linux, Linux would probably never had happened." Linus Torvalds

Linux is not UNIX! Face it! It is not an insult. It is fact: GNU is a recursive acronym for “GNU's Not UNIX”.
vermaden's: links resources deviantart spreadbsd
Reply With Quote
  #7   (View Single Post)  
Old 11th April 2009
gosha gosha is offline
Spam Deminer
 
Join Date: Jun 2008
Location: China
Posts: 256
Thanked 0 Times in 0 Posts
Default

this is what I actually do. I was only curious to know if there was a simple way around it if you get names with bad names without renaming them.
Thanks a lot.
Reply With Quote
Reply

Thread Tools
Display Modes

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

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Cleaning Portsnap files in /var/db/portsnap/files bram85 FreeBSD Ports and Packages 2 5th October 2009 09:54 AM
sorting special characters gosha Programming 15 9th April 2009 02:29 AM
finding files by uid carpman FreeBSD General 3 5th February 2009 07:51 PM
Encrypting Files JMJ_coder General software and network 22 25th October 2008 07:49 AM
Sorting Packages JMJ_coder NetBSD Package System (pkgsrc) 3 20th May 2008 01:08 AM


All times are GMT. The time now is 08:45 AM.


Powered by vBulletin® Version 3.8.4
Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.
Content copyright © 2007-2010, the authors
Daemon image copyright ©1988, Marshall Kirk McKusick