Popular White Paper On This Topic

You are asking for a numeric sort of alphanumeric data. Try "--version-sort" or sort -VNote that the "vee" in " -V " is capitalized. I think that the "--version-sort" feature of is relatively new, ISTR it was introduced with sort version 7, but I might be mistaken.
Good luck,
Dan Martin

Suggest you describe what your immediate requirement is, what you are trying to do about it, some sample data (directory listing, file content, etc), and then provide a manually arranged sample of what you are expecting.

A plain sort works all the way up to file9, but file10 is probably going to sort after file1 but before file2.

It would help if you had the flexibility to name files like file0001 etc when creating them.

This bash shell function should sort stdin according to the first integer in each line.
It does this using DSU (decorate-sort-undecorate). That is, it puts a temporary prefix on each line to make it sort OK in a plain sort, and strips it again after.

Seeing as how this is Unix/Linux, etc... with a decent, self-respecting file system, and _even_ if it's linked to a multi-mounted file-system that has to work (or not) on windows, here's a low-cost trick you should think about.
If you can't control the names of the files, e.g. you already have file1, ... file10, file11, ... file20, ...
you can create an alternate namespace that will conform to your name requirements.
steps.
1 create an alternate directory on the closest part of the tree you have write permission for, hopefully on the same file system
2 devise a re-name filter to LINK (i.e. the "ln" command) the "actual" or already existing file to a name that matches your requirements
3 through a script, link the existing name to the new one, considering your link may have to be a symbolic link.
in your case, if you have fewer than, say 10,000 files, consider these functions.

"commonFileStem" is that part of your filename which doesn't have a number, so the first set preserves the name and the numeric suffix is now "$2", so the next set further splits the pieces, then the ech returns the "alternate name" e.g. commonFileStem10001.

"foreach" is a for loop which excutes the "myln" function with the successive arguments, in this case the file names..

I used this trick in my FinSvc company, where any number of people on the support desk wanted to look at log files with date-stamp names with their Windows editor, like Notepad++. Recall that ~doze chokes over the messy ":" character. I created an immediate parallel directory to the .../log with a name like "wlg", where the names were identical except for the colons were removed. The beauty, of course, is that the "ln" command guarantees the data is identical at no cost in disc space other than the other i-node. Also, realize it's a Windows editor doing the looking it's either a feature or a benefit that a log isn't updated in the Windows version of the link.

I never had to write the "sweeper" to relink log files which were being updated "real time".

As your filename is not purely numeric, so sort -n shall not work correctly. Try this command combination in which you will not see the common "file" in the names but the numeric values in names are sorted. Later you can join the "file" string to this sorted list.

ls | tr "file" " " | tr -s " " | sort -n

Please see that spaces in the first tr command is 4 i.e equal to the " file". Exactly type this and see the o/p.

Copyright 1998-2015 Ziff Davis, LLC (Toolbox.com). All rights reserved. All product names are trademarks of their respective companies. Toolbox.com is not
affiliated with or endorsed by any company listed at this site.