0

I want to list a bunch of files that are sorted by their names excluding the file extension. I have these files in a directory

$ls 
mobility_arXiv.tex
mobility_SR.bbl
paper-hj-mm.tex
paper-hj-mm-vp-rkp.tex
paper-hj.tex

I want the output to be

mobility_arXiv.tex
mobility_SR.bbl
paper-hj.tex
paper-hj-mm.tex
paper-hj-mm-vp-rkp.tex

Is there a way to achieve this.

  • 1
    In Linux it doesn't care if it's an extension or not - it's part of a filename. It just happens to be that Dash(-) has a 0x2D hex code and Dot has a 0x2E hex code, so the dash precedes in alphabetic order – Ashtray Apr 30 '14 at 12:41
  • I assume you meant your second snippet to exclude the '.EXT' parts..?? – jmetz Apr 30 '14 at 12:41
  • @Ashtray, Thanks for the explanation. But why is then paper-hj-mm.tex listed before paper-hj-mm-vp-rkp.tex – imsc Apr 30 '14 at 12:55
  • @imsc can't really say, but I think they have some sorting rule for each character encoding. Still, sort or ls utility doesn't give a damn about file extension – Ashtray May 2 '14 at 5:03
  • @Ashtray indeed LANG=C ls will sort irrespective of extension, and will thus be an easy solution for this question. See this answer: stackoverflow.com/a/18451819 – Patrick Mevzek Jan 29 '18 at 19:57
2

You can pipe the output of ls into cut, which cuts fields, specifying "." as the delimiter. The result can then be sorted by piping into sort.

ls | cut -f 1 -d '.' | sort

If you need to handle dots "." in the filename you can use

ls | rev | cut -d . -f 2- | rev | sort

This works by first reversing each listing returned by ls, then cutting away the first (originally last) field, then reversing again.

NOTE: Cannot be used with colour output

If you wanted to keep the extension anyway, and just sort by the first part of the filename you can use

ls | sort -k 1,1 -t . 

Here you're telling sort to use "." as the delimiter and sort according to key(field) 1.

  • What if I have more than a single dot in a filename? It might work in this specific case, but it's no universal at all – Ashtray Apr 30 '14 at 12:42
  • Sorry, my question was bit confusing. I want the sort order to be different. – imsc Apr 30 '14 at 12:45
  • @Ashtray: Updated with dot handling. – jmetz Apr 30 '14 at 13:04
  • 1
    @imsc: I updated to include sorting, as well as dot in filename handling. – jmetz Apr 30 '14 at 13:06
  • @imsc: I also added the solution if you wanted to keep the extension but sort just from the first part of the filename – jmetz Apr 30 '14 at 13:14
2

The ls command sorts files by filename alphabetically ascending by default. The extension is part of the filename so the best you can do, if you don't need the extension in the output is:

ls | cut -f 1 -d '.' | sort

UPDATE

A better solution:

ls | sort -k 1,1 -t .

Here sort is told to use just field number 1 and the dot as field separator. So it's ignoring the extension but not cutting it out.

  • Is there a way to get the extension back after sorting? – imsc Apr 30 '14 at 13:01
  • Yes. Just found a better way. Check my update. – drk.com.ar Apr 30 '14 at 13:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.