LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 08-17-2018, 02:07 AM   #1
ddenial
Member
 
Registered: Dec 2016
Distribution: CentOS, Fedora, Ubuntu
Posts: 359

Rep: Reputation: 56
How to set natural sort in Linux by default?


Hello

How to set Linux so that the files are displayed in natural sort order?

By default file are listed in this way:
blah_10.txt
blah_11.txt
blah_1.txt
blah_20.txt
blah_2.txt

But I want to sort them by naturally, like
blah_1.txt
blah_2.txt
blah_10.txt
blah_11.txt
blah_20.txt

By using 'ls -lv', I can sort files in a natural way. But, it is not applied to others like loops and find command.

Is there a way to set natural sort as default in Linux?

Thanks
 
Old 08-17-2018, 04:08 AM   #2
lougavulin
Member
 
Registered: Jul 2018
Distribution: Slackware,x86_64,current
Posts: 279

Rep: Reputation: 100Reputation: 100
I don't know anyway to do this as default for all commands...

It will be by command :
* sort with -V
* ls with - v as you saw
* loop for with do like that : for F in blah_{1..20}.txt; do ...
* find, you have to use sort command
 
1 members found this post helpful.
Old 08-17-2018, 04:16 AM   #3
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,842

Rep: Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308
you can influence sorting by LC_ (locale) variables. But actually what you have got is the natural sorting, just you don't like it.
If you wish "better" result you need to rename files like:
blah_01.txt
blah_02.txt
blah_10.txt
blah_11.txt
blah_20.txt
ls itself cannot handle that non-natural partially alphabetic and partially numeric sorting (yes, -v will try to simulate something you expected).
And also if you wish you can implement your own sorting algorithm (or script) which will work as you wish.

Last edited by pan64; 08-17-2018 at 04:18 AM.
 
1 members found this post helpful.
Old 08-17-2018, 04:26 AM   #4
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 7,308
Blog Entries: 3

Rep: Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721
Quote:
Originally Posted by pan64 View Post
If you wish "better" result you need to rename files like:
blah_01.txt
blah_02.txt
blah_10.txt
blah_11.txt
blah_20.txt
It's rather easy to rename them using the sprintf() function, even if there are many:

Code:
rename -n 's/_([0-9]+)\./sprintf("_%02d.",$1)/e' blah*.txt
The %02d adds a leading zero when the number is a single digit. You can add any number of leading zeros %03d, %05d, etc. The s/// is a normal regex substitution, but adding the e modifier makes the substituted component accept perl expressions, such as a simple sprintf()
 
1 members found this post helpful.
Old 08-17-2018, 04:40 AM   #5
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 21,126

Rep: Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120
With our standard caveat - ...
IF you have the perl version of rename.

As to "version" sorting, I have found it really useful, but there have been corner cases (which I can't remember specifically) where it didn't work as expected. So buyer beware.
 
1 members found this post helpful.
Old 08-17-2018, 05:53 AM   #6
ddenial
Member
 
Registered: Dec 2016
Distribution: CentOS, Fedora, Ubuntu
Posts: 359

Original Poster
Rep: Reputation: 56
I get this sorting problem when recursing files in a directory using 'for' loop.

I solved this by using ls command in for loop.

Code:
$ shopt -s globstar
$ for i in "$(ls -v **/*.{txt,rtf})" ; do echo "$i" ; done
blah_1.txt
blah_2.txt
blah_10.txt
blah_11.txt
blah_20.txt
subdir/aaa/file01.rtf
subdir/aaa/file03.rtf
subdir/aaa/file020.rtf
subdir/file1.txt
subdir/file02.txt
subdir/file3.txt
subdir/file004.txt
subdir/file5.txt
Hope it always works flawlessly.

Thank you all.
 
Old 08-17-2018, 05:55 AM   #7
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,842

Rep: Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308
Quote:
Originally Posted by ddenial View Post
Hope it always works flawlessly.
No, unfortunately it doesn't. Especially when filename(s) contain space(s) or other "strange" characters.
 
1 members found this post helpful.
Old 08-17-2018, 06:16 AM   #8
ddenial
Member
 
Registered: Dec 2016
Distribution: CentOS, Fedora, Ubuntu
Posts: 359

Original Poster
Rep: Reputation: 56
Quote:
Originally Posted by pan64 View Post
No, unfortunately it doesn't. Especially when filename(s) contain space(s) or other "strange" characters.
Oh!

I guess I've to forget about natural sort and let Linux do its way. That will be safe.
 
Old 08-17-2018, 06:31 AM   #9
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 21,126

Rep: Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120
Do some testing - the quotes should (might ?) protect the special characters.
 
1 members found this post helpful.
Old 08-17-2018, 06:35 AM   #10
lougavulin
Member
 
Registered: Jul 2018
Distribution: Slackware,x86_64,current
Posts: 279

Rep: Reputation: 100Reputation: 100
One way to manage strange characters and space is to replace loop for with while read
Code:
while read File; do echo "$File"; done < <(ls -1v **/*.{txt,rtf})
 
1 members found this post helpful.
Old 08-17-2018, 06:51 AM   #11
ddenial
Member
 
Registered: Dec 2016
Distribution: CentOS, Fedora, Ubuntu
Posts: 359

Original Poster
Rep: Reputation: 56
Quote:
Originally Posted by lougavulin View Post
One way to manage strange characters and space is to replace loop for with while read
Code:
while read File; do echo "$File"; done < <(ls -1v **/*.{txt,rtf})
This works perfectly well. Thanks
 
  


Reply



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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
LXer: Natural Sort Order utility, in C LXer Syndicated Linux News 0 03-24-2013 04:31 AM
[SOLVED] How to set my default browser in Linux? ashok.g Linux - Software 3 12-18-2009 03:36 AM
Natural sort with just Bash or core Linux commands? ahz Programming 15 12-06-2006 09:38 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 03:10 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration