From unknown Sat Jun 21 10:44:20 2025 X-Loop: help-debbugs@gnu.org Subject: bug#26741: Numeric sort in ls Resent-From: Tony Malykh Original-Sender: "Debbugs-submit" Resent-CC: bug-coreutils@gnu.org Resent-Date: Tue, 02 May 2017 00:59:02 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: report 26741 X-GNU-PR-Package: coreutils X-GNU-PR-Keywords: To: 26741@debbugs.gnu.org X-Debbugs-Original-To: bug-coreutils@gnu.org Received: via spool by submit@debbugs.gnu.org id=B.14936867142525 (code B ref -1); Tue, 02 May 2017 00:59:02 +0000 Received: (at submit) by debbugs.gnu.org; 2 May 2017 00:58:34 +0000 Received: from localhost ([127.0.0.1]:50102 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1d5M9B-0000ef-TG for submit@debbugs.gnu.org; Mon, 01 May 2017 20:58:34 -0400 Received: from eggs.gnu.org ([208.118.235.92]:51278) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1d5M1a-0000Sk-Hr for submit@debbugs.gnu.org; Mon, 01 May 2017 20:50:42 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1d5M1U-0005ap-Fk for submit@debbugs.gnu.org; Mon, 01 May 2017 20:50:37 -0400 X-Spam-Checker-Version: SpamAssassin 3.3.2 (2011-06-06) on eggs.gnu.org X-Spam-Level: X-Spam-Status: No, score=0.8 required=5.0 tests=BAYES_50,FREEMAIL_FROM, T_DKIM_INVALID autolearn=disabled version=3.3.2 Received: from lists.gnu.org ([2001:4830:134:3::11]:43490) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1d5M1U-0005aX-C0 for submit@debbugs.gnu.org; Mon, 01 May 2017 20:50:36 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:42244) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1d5M1T-0002Tq-7u for bug-coreutils@gnu.org; Mon, 01 May 2017 20:50:36 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1d5M1S-0005YQ-6V for bug-coreutils@gnu.org; Mon, 01 May 2017 20:50:35 -0400 Received: from mail-wm0-x22f.google.com ([2a00:1450:400c:c09::22f]:38140) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1d5M1R-0005Xq-VI for bug-coreutils@gnu.org; Mon, 01 May 2017 20:50:34 -0400 Received: by mail-wm0-x22f.google.com with SMTP id r190so4008369wme.1 for ; Mon, 01 May 2017 17:50:33 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=mime-version:from:date:message-id:subject:to; bh=mCWsfAcZ6EvahzUlSvsAa0TG6cQYfxLQK91PNaAcleY=; b=qu9xKP5x2ueg4XCHFtp+5wnrJJmNbOXaXqBwKq4A4Xgx5OyvCGUTAJbi7sslvDRhoA oQnRM+RjiVrAtCgQ9+QkPVWcDEB4sH1hWoU+rt20Qj2n2mwoSqh00kMXpMmobvIDl6Dx 2siNoGvl9trh647pf6ebhQnVXiOykre4DFyRopCVBcfz4+uhw07oNWKhpc6RRdnv3Xh8 9akajyXgFe+cLGwnICYftclGtvO95bSjHVTtBgwK56Vowr/FJVzpoboTYnwC/eBqttzg Z5JlfKpv1PQ0nNZDgh7/BG9oibRCDGbQdejSP+oxaZQIWw/CFztwshQuyd0rfJJ56EDp mxNA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:from:date:message-id:subject:to; bh=mCWsfAcZ6EvahzUlSvsAa0TG6cQYfxLQK91PNaAcleY=; b=SRZQcpTtw/AzLUfosgtSGADR+3Cmdq9TayM49D+D9mO7Huvq+OK4u8ZcvYsxiCDejo g5nKG4p916iDi8BYGPX2Gq+94e8F4J0rZ5xgHQvS2ZdMlllK29mTrGtkytJ07h3uISul OV+oC4HNSPMaPXXMOjXwVzJDQ1KAiJtslx6fXe8V9ECCH2YdbyLD6qH6Ae9/4YctCvPu YzF1aUvA7HJadZDXNXY6Pjw0ckrFCuXrNnl1LhCF7AqQLjl4dmDdsJ6Wi+k7ysSemJBI Rld652FGYPsiDrFbf2+SENe+ll44THQpmhCLauUSgIQifgz6qm8Gt/8Ft3+7kx8AWrFq Qd4g== X-Gm-Message-State: AN3rC/7nTNvFhf+HzwvTiApm6PPZ2+J9QDseIJakCKo9dM9+/7jMZqWf pJ1dai3hKJJ+d2B1XjXajeMcbTtjfg== X-Received: by 10.80.144.206 with SMTP id d14mr22153561eda.9.1493686232332; Mon, 01 May 2017 17:50:32 -0700 (PDT) MIME-Version: 1.0 Received: by 10.80.131.103 with HTTP; Mon, 1 May 2017 17:50:31 -0700 (PDT) From: Tony Malykh Date: Mon, 1 May 2017 17:50:31 -0700 Message-ID: Content-Type: text/plain; charset=UTF-8 X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.6.x X-Received-From: 2001:4830:134:3::11 X-Spam-Score: -4.0 (----) X-Mailman-Approved-At: Mon, 01 May 2017 20:58:32 -0400 X-BeenThere: debbugs-submit@debbugs.gnu.org X-Mailman-Version: 2.1.18 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: debbugs-submit-bounces@debbugs.gnu.org Sender: "Debbugs-submit" X-Spam-Score: -4.0 (----) Hi all, I am wondering if there is a way to do a numeric sort in ls? I am aware of version sort option -v and blow I explain the problem with it. What I'd like to achieve: Suppose I have files with numbers in their names.: p1.py p2.py p11.py It appears that version sort (-v) option achieves exactly that. However, the problem with it is that it seems to be incompatible with extension sort (-X) option. This limitation seems artificial to me: it is fairly easy to sort files by the extension, and then sort them numerically within every extension group. If these files are sorted lexicographically, p2.py would come on the last position. I would like p2.py to appear in between p1.py and p11.py. Maybe I don't understand something about the version sort algorithm; maybe it does something more than numeric sort. But in this case, is it possible to add a new flag for numeric sort, that is one that would treat digits as numbers and would not conflict with the extension sort -X ? Here is a concrete example. Here are some files $ ls -1 1.py 11.py 2.pl 2.py Desired output: $ ls -1 -- 2.pl 1.py 2.py 11.py # Note that 2.pl goes to the top because of its extension. # Note also that the *.py files are sorted numerically and not lexicographically. Attempt #1: $ ls -1Xv 1.py 2.pl 2.py 11.py # Note that the files are sorted numerically, but the extension sort (-X) option seem to have been ignored, since the 2.pl file is among the *.py files. Attempt #2: $ ls -1vX 2.pl 1.py 11.py 2.py # Now the extension sort is working, but the files are sorted lexicographically and not numerically. Here is my version: $ ls --version ls (GNU coreutils) 8.26 Packaged by Cygwin (8.26-2) Any suggestions will be appreciated! Thanks Tony From debbugs-submit-bounces@debbugs.gnu.org Mon May 01 21:27:53 2017 Received: (at control) by debbugs.gnu.org; 2 May 2017 01:27:53 +0000 Received: from localhost ([127.0.0.1]:50117 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1d5MbV-0001QG-Oz for submit@debbugs.gnu.org; Mon, 01 May 2017 21:27:53 -0400 Received: from mx1.redhat.com ([209.132.183.28]:37756) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1d5MbL-0001Pk-E4; Mon, 01 May 2017 21:27:40 -0400 Received: from smtp.corp.redhat.com (int-mx02.intmail.prod.int.phx2.redhat.com [10.5.11.12]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 11206A6443; Tue, 2 May 2017 01:27:33 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mx1.redhat.com 11206A6443 Authentication-Results: ext-mx03.extmail.prod.ext.phx2.redhat.com; dmarc=none (p=none dis=none) header.from=redhat.com Authentication-Results: ext-mx03.extmail.prod.ext.phx2.redhat.com; spf=pass smtp.mailfrom=eblake@redhat.com DKIM-Filter: OpenDKIM Filter v2.11.0 mx1.redhat.com 11206A6443 Received: from [10.10.121.55] (ovpn-121-55.rdu2.redhat.com [10.10.121.55]) by smtp.corp.redhat.com (Postfix) with ESMTP id 57C4077DD8; Tue, 2 May 2017 01:27:32 +0000 (UTC) Subject: Re: bug#26741: Numeric sort in ls To: Tony Malykh , 26741-done@debbugs.gnu.org References: From: Eric Blake Openpgp: url=http://people.redhat.com/eblake/eblake.gpg Organization: Red Hat, Inc. Message-ID: Date: Mon, 1 May 2017 20:27:31 -0500 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:52.0) Gecko/20100101 Thunderbird/52.0 MIME-Version: 1.0 In-Reply-To: Content-Type: multipart/signed; micalg=pgp-sha256; protocol="application/pgp-signature"; boundary="SqQemfNwVSaRJukGlA8Dm1uaqlqSSmbIj" X-Scanned-By: MIMEDefang 2.79 on 10.5.11.12 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.27]); Tue, 02 May 2017 01:27:33 +0000 (UTC) X-Spam-Score: -5.0 (-----) X-Debbugs-Envelope-To: control X-BeenThere: debbugs-submit@debbugs.gnu.org X-Mailman-Version: 2.1.18 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: debbugs-submit-bounces@debbugs.gnu.org Sender: "Debbugs-submit" X-Spam-Score: -5.0 (-----) This is an OpenPGP/MIME signed message (RFC 4880 and 3156) --SqQemfNwVSaRJukGlA8Dm1uaqlqSSmbIj Content-Type: multipart/mixed; boundary="ntegKxdemkwQuT7gT7Lj513IfP8LobWpE"; protected-headers="v1" From: Eric Blake To: Tony Malykh , 26741-done@debbugs.gnu.org Message-ID: Subject: Re: bug#26741: Numeric sort in ls References: In-Reply-To: --ntegKxdemkwQuT7gT7Lj513IfP8LobWpE Content-Type: text/plain; charset=utf-8 Content-Language: en-US Content-Transfer-Encoding: quoted-printable tag 26741 notabug thanks On 05/01/2017 07:50 PM, Tony Malykh wrote: > Hi all, >=20 > I am wondering if there is a way to do a numeric sort in ls? Thanks for the report; the quick answer is that sort can already do what you want, so post-process your ls output with sort. > Here is a concrete example. Thanks, that makes it much easier to reproduce what you want, and demonstrate along the way what is going on. >=20 > Here are some files > $ ls -1 > 1.py > 11.py > 2.pl > 2.py I'll compress that set of data to: $ printf %s\\n 1.py 11.py 2.pl 2.py >=20 > Desired output: > $ ls -1 -- > 2.pl > 1.py > 2.py > 11.py If you are okay with: ls -1 | sort then we are good to go. In fact, in that configuration, you can use plain 'ls' rather than 'ls -1' (since POSIX requires -1 to be the default behavior when stdout of ls is a pipeline). > # Note that 2.pl goes to the top because of its extension. > # Note also that the *.py files are sorted numerically and not > lexicographically. So that says that you want your primary sort key to be the extension field, sorted lexically; and your secondary sort field to be the rest of the name, sorted numerically. How about the following? $ printf %s\\n 1.py 11.py 2.pl 2.py | sort -t . -k2,2 -k1,1n 2.pl 1.py 2.py 11.py Again, with your directory layout, you could use: $ ls | sort -t . -k2,2 -k1,1n for the same results. Now, is that entirely robust? Not really - all it takes is one file with no extension, or one file with embedded '.' in the name beyond the extension, and you are no longer able to reliably specify which sort field is the extension. $ printf %s\\n 4.4.py 3 1.py 11.py 2.pl 2.py | sort -t . -k2,2 -k1,1n 3 4.4.py 2.pl 1.py 2.py 11.py But never fear - we can use the decorate-sort-undecorate pattern to temporarily swap things around: $ printf %s\\n 4.4.py 3 1.py 11.py 2.pl 2.py \ | sed 's/\(.*\)\.\([^.]*\)$/\2.\1/' \ | sort -t . -k1,1 -k2n \ | sed 's/\(^[^.]*\)\.\(.*\)/\2.\1/' 3 2.pl 1.py 2.py 4.4.py 11.py (although when you start getting that complex, awk, perl, or a dedicated C program start to sound more appealing). >=20 > Attempt #1: > $ ls -1Xv > 1.py > 2.pl > 2.py > 11.py > # Note that the files are sorted numerically, but the extension sort > (-X) option seem to have been ignored, since the 2.pl file is among > the *.py files. That's because 'ls' has exactly ONE level of sort. You cannot specify a primary and secondary key to ls, but rather the last sort type requested overrules all earlier requests. The only program with multiple levels of sort is, not surprisingly, 'sort'. 'ls -Xv' is identical to 'ls -v'. >=20 > Attempt #2: > $ ls -1vX > 2.pl > 1.py > 11.py > 2.py > # Now the extension sort is working, but the files are sorted > lexicographically and not numerically. >=20 'ls -vX' is identical to 'ls -X'. It appears that your request is to modify ls directly to subsume the ability that sort already has to list multiple sort keys, and break ties under one key by resorting to the next. However, the bar is VERY high to add any new features to ls, and unless you can point to existing practice of some other ls implementation that does the same, we are probably going to leave it at requiring you to post-process the data. Besides, you'd have to wait for a new version of ls to be build and land in your distro, while you already have post-processing tools at your disposal that are more portable. So, for now, I'm closing this as not a bug, although you should feel free to continue the conversation if you have more to add. > Here is my version: > $ ls --version > ls (GNU coreutils) 8.26 > Packaged by Cygwin (8.26-2) [hmm - a good reminder that my TODO list includes packaging an updated coreutils for cygwin...] --=20 Eric Blake, Principal Software Engineer Red Hat, Inc. +1-919-301-3266 Virtualization: qemu.org | libvirt.org --ntegKxdemkwQuT7gT7Lj513IfP8LobWpE-- --SqQemfNwVSaRJukGlA8Dm1uaqlqSSmbIj Content-Type: application/pgp-signature; name="signature.asc" Content-Description: OpenPGP digital signature Content-Disposition: attachment; filename="signature.asc" -----BEGIN PGP SIGNATURE----- Version: GnuPG v2 Comment: Public key at http://people.redhat.com/eblake/eblake.gpg Comment: Using GnuPG with Thunderbird - http://www.enigmail.net/ iQEcBAEBCAAGBQJZB+CDAAoJEKeha0olJ0Nqi5MH/1ygDvwleh8kxO83zuBf+TAs gvjuXF02nFWZAC3n24q+G94C0ubCB1ma50ACmWnn1N5hEYa+OLJzig4+6/B4O8f5 SzLZz7weaTOc5Bqfy6YzgmF8EBpAwljHsLyt6dhZqS7VIc2mibYJJqjEBtu6QnHi aXKn/S1TpkYZsNW9xFHwvf2sBAS5IgGKGOVIMyJJHPUVkfLZT2pQ4sb/YpyHTBok CFAFXZ2G35mYeYvQyHaQXkDDYg6Wn4rvDD7q+RFMQV6R0Mrd9Wz2uOr8Pp338xHs 35TKoR1UsyZskOVIVAwS9vYydJuXk3Nui65xCltkkq2PcO10JTbIubTs02+RBvs= =2LFJ -----END PGP SIGNATURE----- --SqQemfNwVSaRJukGlA8Dm1uaqlqSSmbIj-- From unknown Sat Jun 21 10:44:20 2025 MIME-Version: 1.0 X-Mailer: MIME-tools 5.505 (Entity 5.505) X-Loop: help-debbugs@gnu.org From: help-debbugs@gnu.org (GNU bug Tracking System) To: Tony Malykh Subject: bug#26741: closed (Re: bug#26741: Numeric sort in ls) Message-ID: References: X-Gnu-PR-Message: they-closed 26741 X-Gnu-PR-Package: coreutils X-Gnu-PR-Keywords: notabug Reply-To: 26741@debbugs.gnu.org Date: Tue, 02 May 2017 01:28:04 +0000 Content-Type: multipart/mixed; boundary="----------=_1493688484-5512-1" This is a multi-part message in MIME format... ------------=_1493688484-5512-1 Content-Disposition: inline Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Your bug report #26741: Numeric sort in ls which was filed against the coreutils package, has been closed. The explanation is attached below, along with your original report. If you require more details, please reply to 26741@debbugs.gnu.org. --=20 26741: http://debbugs.gnu.org/cgi/bugreport.cgi?bug=3D26741 GNU Bug Tracking System Contact help-debbugs@gnu.org with problems ------------=_1493688484-5512-1 Content-Type: message/rfc822 Content-Disposition: inline Content-Transfer-Encoding: 7bit Received: (at 26741-done) by debbugs.gnu.org; 2 May 2017 01:27:49 +0000 Received: from localhost ([127.0.0.1]:50115 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1d5MbN-0001Q3-OJ for submit@debbugs.gnu.org; Mon, 01 May 2017 21:27:49 -0400 Received: from mx1.redhat.com ([209.132.183.28]:37756) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1d5MbL-0001Pk-E4; Mon, 01 May 2017 21:27:40 -0400 Received: from smtp.corp.redhat.com (int-mx02.intmail.prod.int.phx2.redhat.com [10.5.11.12]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 11206A6443; Tue, 2 May 2017 01:27:33 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mx1.redhat.com 11206A6443 Authentication-Results: ext-mx03.extmail.prod.ext.phx2.redhat.com; dmarc=none (p=none dis=none) header.from=redhat.com Authentication-Results: ext-mx03.extmail.prod.ext.phx2.redhat.com; spf=pass smtp.mailfrom=eblake@redhat.com DKIM-Filter: OpenDKIM Filter v2.11.0 mx1.redhat.com 11206A6443 Received: from [10.10.121.55] (ovpn-121-55.rdu2.redhat.com [10.10.121.55]) by smtp.corp.redhat.com (Postfix) with ESMTP id 57C4077DD8; Tue, 2 May 2017 01:27:32 +0000 (UTC) Subject: Re: bug#26741: Numeric sort in ls To: Tony Malykh , 26741-done@debbugs.gnu.org References: From: Eric Blake Openpgp: url=http://people.redhat.com/eblake/eblake.gpg Organization: Red Hat, Inc. Message-ID: Date: Mon, 1 May 2017 20:27:31 -0500 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:52.0) Gecko/20100101 Thunderbird/52.0 MIME-Version: 1.0 In-Reply-To: Content-Type: multipart/signed; micalg=pgp-sha256; protocol="application/pgp-signature"; boundary="SqQemfNwVSaRJukGlA8Dm1uaqlqSSmbIj" X-Scanned-By: MIMEDefang 2.79 on 10.5.11.12 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.27]); Tue, 02 May 2017 01:27:33 +0000 (UTC) X-Spam-Score: -5.0 (-----) X-Debbugs-Envelope-To: 26741-done X-BeenThere: debbugs-submit@debbugs.gnu.org X-Mailman-Version: 2.1.18 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: debbugs-submit-bounces@debbugs.gnu.org Sender: "Debbugs-submit" X-Spam-Score: -0.0 (/) This is an OpenPGP/MIME signed message (RFC 4880 and 3156) --SqQemfNwVSaRJukGlA8Dm1uaqlqSSmbIj Content-Type: multipart/mixed; boundary="ntegKxdemkwQuT7gT7Lj513IfP8LobWpE"; protected-headers="v1" From: Eric Blake To: Tony Malykh , 26741-done@debbugs.gnu.org Message-ID: Subject: Re: bug#26741: Numeric sort in ls References: In-Reply-To: --ntegKxdemkwQuT7gT7Lj513IfP8LobWpE Content-Type: text/plain; charset=utf-8 Content-Language: en-US Content-Transfer-Encoding: quoted-printable tag 26741 notabug thanks On 05/01/2017 07:50 PM, Tony Malykh wrote: > Hi all, >=20 > I am wondering if there is a way to do a numeric sort in ls? Thanks for the report; the quick answer is that sort can already do what you want, so post-process your ls output with sort. > Here is a concrete example. Thanks, that makes it much easier to reproduce what you want, and demonstrate along the way what is going on. >=20 > Here are some files > $ ls -1 > 1.py > 11.py > 2.pl > 2.py I'll compress that set of data to: $ printf %s\\n 1.py 11.py 2.pl 2.py >=20 > Desired output: > $ ls -1 -- > 2.pl > 1.py > 2.py > 11.py If you are okay with: ls -1 | sort then we are good to go. In fact, in that configuration, you can use plain 'ls' rather than 'ls -1' (since POSIX requires -1 to be the default behavior when stdout of ls is a pipeline). > # Note that 2.pl goes to the top because of its extension. > # Note also that the *.py files are sorted numerically and not > lexicographically. So that says that you want your primary sort key to be the extension field, sorted lexically; and your secondary sort field to be the rest of the name, sorted numerically. How about the following? $ printf %s\\n 1.py 11.py 2.pl 2.py | sort -t . -k2,2 -k1,1n 2.pl 1.py 2.py 11.py Again, with your directory layout, you could use: $ ls | sort -t . -k2,2 -k1,1n for the same results. Now, is that entirely robust? Not really - all it takes is one file with no extension, or one file with embedded '.' in the name beyond the extension, and you are no longer able to reliably specify which sort field is the extension. $ printf %s\\n 4.4.py 3 1.py 11.py 2.pl 2.py | sort -t . -k2,2 -k1,1n 3 4.4.py 2.pl 1.py 2.py 11.py But never fear - we can use the decorate-sort-undecorate pattern to temporarily swap things around: $ printf %s\\n 4.4.py 3 1.py 11.py 2.pl 2.py \ | sed 's/\(.*\)\.\([^.]*\)$/\2.\1/' \ | sort -t . -k1,1 -k2n \ | sed 's/\(^[^.]*\)\.\(.*\)/\2.\1/' 3 2.pl 1.py 2.py 4.4.py 11.py (although when you start getting that complex, awk, perl, or a dedicated C program start to sound more appealing). >=20 > Attempt #1: > $ ls -1Xv > 1.py > 2.pl > 2.py > 11.py > # Note that the files are sorted numerically, but the extension sort > (-X) option seem to have been ignored, since the 2.pl file is among > the *.py files. That's because 'ls' has exactly ONE level of sort. You cannot specify a primary and secondary key to ls, but rather the last sort type requested overrules all earlier requests. The only program with multiple levels of sort is, not surprisingly, 'sort'. 'ls -Xv' is identical to 'ls -v'. >=20 > Attempt #2: > $ ls -1vX > 2.pl > 1.py > 11.py > 2.py > # Now the extension sort is working, but the files are sorted > lexicographically and not numerically. >=20 'ls -vX' is identical to 'ls -X'. It appears that your request is to modify ls directly to subsume the ability that sort already has to list multiple sort keys, and break ties under one key by resorting to the next. However, the bar is VERY high to add any new features to ls, and unless you can point to existing practice of some other ls implementation that does the same, we are probably going to leave it at requiring you to post-process the data. Besides, you'd have to wait for a new version of ls to be build and land in your distro, while you already have post-processing tools at your disposal that are more portable. So, for now, I'm closing this as not a bug, although you should feel free to continue the conversation if you have more to add. > Here is my version: > $ ls --version > ls (GNU coreutils) 8.26 > Packaged by Cygwin (8.26-2) [hmm - a good reminder that my TODO list includes packaging an updated coreutils for cygwin...] --=20 Eric Blake, Principal Software Engineer Red Hat, Inc. +1-919-301-3266 Virtualization: qemu.org | libvirt.org --ntegKxdemkwQuT7gT7Lj513IfP8LobWpE-- --SqQemfNwVSaRJukGlA8Dm1uaqlqSSmbIj Content-Type: application/pgp-signature; name="signature.asc" Content-Description: OpenPGP digital signature Content-Disposition: attachment; filename="signature.asc" -----BEGIN PGP SIGNATURE----- Version: GnuPG v2 Comment: Public key at http://people.redhat.com/eblake/eblake.gpg Comment: Using GnuPG with Thunderbird - http://www.enigmail.net/ iQEcBAEBCAAGBQJZB+CDAAoJEKeha0olJ0Nqi5MH/1ygDvwleh8kxO83zuBf+TAs gvjuXF02nFWZAC3n24q+G94C0ubCB1ma50ACmWnn1N5hEYa+OLJzig4+6/B4O8f5 SzLZz7weaTOc5Bqfy6YzgmF8EBpAwljHsLyt6dhZqS7VIc2mibYJJqjEBtu6QnHi aXKn/S1TpkYZsNW9xFHwvf2sBAS5IgGKGOVIMyJJHPUVkfLZT2pQ4sb/YpyHTBok CFAFXZ2G35mYeYvQyHaQXkDDYg6Wn4rvDD7q+RFMQV6R0Mrd9Wz2uOr8Pp338xHs 35TKoR1UsyZskOVIVAwS9vYydJuXk3Nui65xCltkkq2PcO10JTbIubTs02+RBvs= =2LFJ -----END PGP SIGNATURE----- --SqQemfNwVSaRJukGlA8Dm1uaqlqSSmbIj-- ------------=_1493688484-5512-1 Content-Type: message/rfc822 Content-Disposition: inline Content-Transfer-Encoding: 7bit Received: (at submit) by debbugs.gnu.org; 2 May 2017 00:58:34 +0000 Received: from localhost ([127.0.0.1]:50102 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1d5M9B-0000ef-TG for submit@debbugs.gnu.org; Mon, 01 May 2017 20:58:34 -0400 Received: from eggs.gnu.org ([208.118.235.92]:51278) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1d5M1a-0000Sk-Hr for submit@debbugs.gnu.org; Mon, 01 May 2017 20:50:42 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1d5M1U-0005ap-Fk for submit@debbugs.gnu.org; Mon, 01 May 2017 20:50:37 -0400 X-Spam-Checker-Version: SpamAssassin 3.3.2 (2011-06-06) on eggs.gnu.org X-Spam-Level: X-Spam-Status: No, score=0.8 required=5.0 tests=BAYES_50,FREEMAIL_FROM, T_DKIM_INVALID autolearn=disabled version=3.3.2 Received: from lists.gnu.org ([2001:4830:134:3::11]:43490) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1d5M1U-0005aX-C0 for submit@debbugs.gnu.org; Mon, 01 May 2017 20:50:36 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:42244) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1d5M1T-0002Tq-7u for bug-coreutils@gnu.org; Mon, 01 May 2017 20:50:36 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1d5M1S-0005YQ-6V for bug-coreutils@gnu.org; Mon, 01 May 2017 20:50:35 -0400 Received: from mail-wm0-x22f.google.com ([2a00:1450:400c:c09::22f]:38140) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1d5M1R-0005Xq-VI for bug-coreutils@gnu.org; Mon, 01 May 2017 20:50:34 -0400 Received: by mail-wm0-x22f.google.com with SMTP id r190so4008369wme.1 for ; Mon, 01 May 2017 17:50:33 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=mime-version:from:date:message-id:subject:to; bh=mCWsfAcZ6EvahzUlSvsAa0TG6cQYfxLQK91PNaAcleY=; b=qu9xKP5x2ueg4XCHFtp+5wnrJJmNbOXaXqBwKq4A4Xgx5OyvCGUTAJbi7sslvDRhoA oQnRM+RjiVrAtCgQ9+QkPVWcDEB4sH1hWoU+rt20Qj2n2mwoSqh00kMXpMmobvIDl6Dx 2siNoGvl9trh647pf6ebhQnVXiOykre4DFyRopCVBcfz4+uhw07oNWKhpc6RRdnv3Xh8 9akajyXgFe+cLGwnICYftclGtvO95bSjHVTtBgwK56Vowr/FJVzpoboTYnwC/eBqttzg Z5JlfKpv1PQ0nNZDgh7/BG9oibRCDGbQdejSP+oxaZQIWw/CFztwshQuyd0rfJJ56EDp mxNA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:from:date:message-id:subject:to; bh=mCWsfAcZ6EvahzUlSvsAa0TG6cQYfxLQK91PNaAcleY=; b=SRZQcpTtw/AzLUfosgtSGADR+3Cmdq9TayM49D+D9mO7Huvq+OK4u8ZcvYsxiCDejo g5nKG4p916iDi8BYGPX2Gq+94e8F4J0rZ5xgHQvS2ZdMlllK29mTrGtkytJ07h3uISul OV+oC4HNSPMaPXXMOjXwVzJDQ1KAiJtslx6fXe8V9ECCH2YdbyLD6qH6Ae9/4YctCvPu YzF1aUvA7HJadZDXNXY6Pjw0ckrFCuXrNnl1LhCF7AqQLjl4dmDdsJ6Wi+k7ysSemJBI Rld652FGYPsiDrFbf2+SENe+ll44THQpmhCLauUSgIQifgz6qm8Gt/8Ft3+7kx8AWrFq Qd4g== X-Gm-Message-State: AN3rC/7nTNvFhf+HzwvTiApm6PPZ2+J9QDseIJakCKo9dM9+/7jMZqWf pJ1dai3hKJJ+d2B1XjXajeMcbTtjfg== X-Received: by 10.80.144.206 with SMTP id d14mr22153561eda.9.1493686232332; Mon, 01 May 2017 17:50:32 -0700 (PDT) MIME-Version: 1.0 Received: by 10.80.131.103 with HTTP; Mon, 1 May 2017 17:50:31 -0700 (PDT) From: Tony Malykh Date: Mon, 1 May 2017 17:50:31 -0700 Message-ID: Subject: Numeric sort in ls To: bug-coreutils@gnu.org Content-Type: text/plain; charset=UTF-8 X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.6.x X-Received-From: 2001:4830:134:3::11 X-Spam-Score: -4.0 (----) X-Debbugs-Envelope-To: submit X-Mailman-Approved-At: Mon, 01 May 2017 20:58:32 -0400 X-BeenThere: debbugs-submit@debbugs.gnu.org X-Mailman-Version: 2.1.18 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: debbugs-submit-bounces@debbugs.gnu.org Sender: "Debbugs-submit" X-Spam-Score: -4.0 (----) Hi all, I am wondering if there is a way to do a numeric sort in ls? I am aware of version sort option -v and blow I explain the problem with it. What I'd like to achieve: Suppose I have files with numbers in their names.: p1.py p2.py p11.py It appears that version sort (-v) option achieves exactly that. However, the problem with it is that it seems to be incompatible with extension sort (-X) option. This limitation seems artificial to me: it is fairly easy to sort files by the extension, and then sort them numerically within every extension group. If these files are sorted lexicographically, p2.py would come on the last position. I would like p2.py to appear in between p1.py and p11.py. Maybe I don't understand something about the version sort algorithm; maybe it does something more than numeric sort. But in this case, is it possible to add a new flag for numeric sort, that is one that would treat digits as numbers and would not conflict with the extension sort -X ? Here is a concrete example. Here are some files $ ls -1 1.py 11.py 2.pl 2.py Desired output: $ ls -1 -- 2.pl 1.py 2.py 11.py # Note that 2.pl goes to the top because of its extension. # Note also that the *.py files are sorted numerically and not lexicographically. Attempt #1: $ ls -1Xv 1.py 2.pl 2.py 11.py # Note that the files are sorted numerically, but the extension sort (-X) option seem to have been ignored, since the 2.pl file is among the *.py files. Attempt #2: $ ls -1vX 2.pl 1.py 11.py 2.py # Now the extension sort is working, but the files are sorted lexicographically and not numerically. Here is my version: $ ls --version ls (GNU coreutils) 8.26 Packaged by Cygwin (8.26-2) Any suggestions will be appreciated! Thanks Tony ------------=_1493688484-5512-1-- From unknown Sat Jun 21 10:44:20 2025 X-Loop: help-debbugs@gnu.org Subject: bug#26741: Numeric sort in ls Resent-From: Andreas Schwab Original-Sender: "Debbugs-submit" Resent-CC: bug-coreutils@gnu.org Resent-Date: Tue, 02 May 2017 17:24:02 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: followup 26741 X-GNU-PR-Package: coreutils X-GNU-PR-Keywords: notabug To: Tony Malykh Cc: 26741@debbugs.gnu.org Received: via spool by 26741-submit@debbugs.gnu.org id=B26741.149374584023247 (code B ref 26741); Tue, 02 May 2017 17:24:02 +0000 Received: (at 26741) by debbugs.gnu.org; 2 May 2017 17:24:00 +0000 Received: from localhost ([127.0.0.1]:51403 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1d5bWq-00062t-6p for submit@debbugs.gnu.org; Tue, 02 May 2017 13:24:00 -0400 Received: from mail-out.m-online.net ([212.18.0.9]:39297) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1d5bWp-00062m-6t for 26741@debbugs.gnu.org; Tue, 02 May 2017 13:23:59 -0400 Received: from frontend01.mail.m-online.net (unknown [192.168.8.182]) by mail-out.m-online.net (Postfix) with ESMTP id 3wHSnV4qKRz1qrtQ; Tue, 2 May 2017 19:23:58 +0200 (CEST) Received: from localhost (dynscan01.mnet-online.de [192.168.6.70]) by mail.m-online.net (Postfix) with ESMTP id 3wHSnV348bz3jgYf; Tue, 2 May 2017 19:23:58 +0200 (CEST) X-Virus-Scanned: amavisd-new at mnet-online.de Received: from mail.mnet-online.de ([192.168.8.182]) by localhost (dynscan01.mail.m-online.net [192.168.6.70]) (amavisd-new, port 10024) with ESMTP id k8YOhyTB9Ilz; Tue, 2 May 2017 19:23:57 +0200 (CEST) X-Auth-Info: WxxSF0whDrntNi0X99STyITem5Pc12xqvQTY+5z/UkARxbfbbH5pVtmS8pfD9Nf5 Received: from igel.home (ppp-88-217-0-45.dynamic.mnet-online.de [88.217.0.45]) (using TLSv1 with cipher DHE-RSA-CAMELLIA256-SHA (256/256 bits)) (No client certificate requested) by mail.mnet-online.de (Postfix) with ESMTPSA; Tue, 2 May 2017 19:23:57 +0200 (CEST) Received: by igel.home (Postfix, from userid 1000) id 614192C0CAB; Tue, 2 May 2017 19:23:57 +0200 (CEST) From: Andreas Schwab References: X-Yow: Is this "BIKINI BEACH"? Date: Tue, 02 May 2017 19:23:57 +0200 In-Reply-To: (Tony Malykh's message of "Mon, 1 May 2017 17:50:31 -0700") Message-ID: <87o9vbp3yq.fsf@linux-m68k.org> User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/25.2 (gnu/linux) MIME-Version: 1.0 Content-Type: text/plain X-Spam-Score: -0.7 (/) X-BeenThere: debbugs-submit@debbugs.gnu.org X-Mailman-Version: 2.1.18 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: debbugs-submit-bounces@debbugs.gnu.org Sender: "Debbugs-submit" X-Spam-Score: -0.7 (/) On Mai 01 2017, Tony Malykh wrote: > It appears that version sort (-v) option achieves exactly that. > However, the problem with it is that it seems to be incompatible with > extension sort (-X) option. This isn't limited to these two option. Generally, ls only supports a single sort option, with later options overriding previous ones. Andreas. -- Andreas Schwab, schwab@linux-m68k.org GPG Key fingerprint = 58CA 54C7 6D53 942B 1756 01D3 44D5 214B 8276 4ED5 "And now for something completely different." From unknown Sat Jun 21 10:44:20 2025 X-Loop: help-debbugs@gnu.org Subject: bug#26741: Numeric sort in ls Resent-From: Tony Malykh Original-Sender: "Debbugs-submit" Resent-CC: bug-coreutils@gnu.org Resent-Date: Wed, 03 May 2017 00:34:02 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: followup 26741 X-GNU-PR-Package: coreutils X-GNU-PR-Keywords: notabug To: Andreas Schwab Cc: 26741@debbugs.gnu.org Received: via spool by 26741-submit@debbugs.gnu.org id=B26741.14937716269836 (code B ref 26741); Wed, 03 May 2017 00:34:02 +0000 Received: (at 26741) by debbugs.gnu.org; 3 May 2017 00:33:46 +0000 Received: from localhost ([127.0.0.1]:51704 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1d5iEk-0002YZ-72 for submit@debbugs.gnu.org; Tue, 02 May 2017 20:33:46 -0400 Received: from mail-wm0-f52.google.com ([74.125.82.52]:36462) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1d5iEi-0002YN-3R for 26741@debbugs.gnu.org; Tue, 02 May 2017 20:33:44 -0400 Received: by mail-wm0-f52.google.com with SMTP id u65so129783822wmu.1 for <26741@debbugs.gnu.org>; Tue, 02 May 2017 17:33:44 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :cc; bh=Te8qvOFZlFI0Xkq8N0jcihzOiypnqlyBN2mc8/fD6r8=; b=s/Na6FAlSaUN/b7MFe3IDggqZbO/H0+HKXd634fXq+CLEMy7EFLWK1hAR5bsTHzpwr mujOq4JZvspPJcehG3xterGNylnl12Nxomk+jv+/ekZOpb5QLRKE7R5wUjIG766VsgZx yUkx/D+u/qEuBf69Teou2luYtK4Ow/fJGosASUxEM6iQuij8WevancR6xHvdP/n/9fJC SP7Jbd2GQzzTJcyoDLyRiWw8Byr+/KmBTVkrb+9MDVbnuIrBpzlX0q2WUHzTvdDKNHOE xG9Wn/7lPU9eM2cHhJ5xfjK9+mdku+XIAk61C2q8AZIGa2qDZ1967P3BD5DPaKsnb8rN WjaA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:in-reply-to:references:from:date :message-id:subject:to:cc; bh=Te8qvOFZlFI0Xkq8N0jcihzOiypnqlyBN2mc8/fD6r8=; b=RhKVmHZu9G6PsVLyE9cd6uD+qDFj8xPnEwqTAmx4hVc4Nu50r8vbcl1Ct0zytuEPhK 6UpGLTqW8jySd8rBopj8ZdOfGpteoKiTKukGRlwPkUJPtbzh+2Bd6ts3bVa5oa5gyrD5 8Dxe0MKGMAkJ2g9wpNjZhdgWIpIMG9RdzuDoaU6lm+XBozDoSJrip4uBbT1K3ohfmdIl mN6Xu3UzTg37YJ6uOBbHXwW3Se8boJiX2jJAu/GGlAOPV1ya2piggAke2aFL2n2xii+e xH8j9hDUVbst4+o+R7xiV3HahS2XRw35u052r33P5T4aL2jStW+yoeeOLOTIdHZ62vKI WNWA== X-Gm-Message-State: AN3rC/7fPlBCf1HT6s3z9qEcx3H33kt/JKNg6nsrjuRTR7OZIAIvwrp/ MqMsmCC4p7kqg+8TRyXGK3xe6Tpg6A== X-Received: by 10.80.149.203 with SMTP id x11mr24483624eda.181.1493771618329; Tue, 02 May 2017 17:33:38 -0700 (PDT) MIME-Version: 1.0 Received: by 10.80.131.103 with HTTP; Tue, 2 May 2017 17:33:37 -0700 (PDT) In-Reply-To: <87o9vbp3yq.fsf@linux-m68k.org> References: <87o9vbp3yq.fsf@linux-m68k.org> From: Tony Malykh Date: Tue, 2 May 2017 17:33:37 -0700 Message-ID: Content-Type: text/plain; charset=UTF-8 X-Spam-Score: 0.5 (/) X-BeenThere: debbugs-submit@debbugs.gnu.org X-Mailman-Version: 2.1.18 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: debbugs-submit-bounces@debbugs.gnu.org Sender: "Debbugs-submit" X-Spam-Score: 0.5 (/) It should be possible from technical point of view to add a new option --numeric-sort or --natural-sort that wouldn't conflict with -X or any other sort options of ls. I am a C++ developer myself, so I can implement this new options myself. Would the community of coreutils developers be interested in accepting a patch like this? I've looked at the source code, and it seems that the function that does lexicographic sorting is strcoll. The implementation of this function is quite complicated, and it is implemented in glibc. What I would propose is to write a new function strcoll_natural in ls code, that would identify which characters are digits in the input strings. Then it would pass non-digit chunks to strcoll to preserve the handling of locales, and it would do the smart comparison of digits as numbers. As a separate note, what I dislike about ls -v option is that it handles capital letters in a different way than without the -v option: $ ls -1 a B c D $ ls -1v B D a c I have no particular preference among these two, but I'd rather see them consistent. It seems like this is a known "caveat" of -v option, since it ignores the locale. Tony On 5/2/17, Andreas Schwab wrote: > On Mai 01 2017, Tony Malykh wrote: > >> It appears that version sort (-v) option achieves exactly that. >> However, the problem with it is that it seems to be incompatible with >> extension sort (-X) option. > > This isn't limited to these two option. Generally, ls only supports a > single sort option, with later options overriding previous ones. > > Andreas. > > -- > Andreas Schwab, schwab@linux-m68k.org > GPG Key fingerprint = 58CA 54C7 6D53 942B 1756 01D3 44D5 214B 8276 4ED5 > "And now for something completely different." >