2

I get a strange result when sorting files in bash

ruediger@r30-pc-2:~/myPics$ ls 'Picture 001b.jpg' 'Picture 011.jpg' 'Picture 024.jpg' 'Picture 032.jpg' 'Picture 001c.jpg' 'Picture 013.jpg' 'Picture 027.jpg' 'Picture 033.jpg' 'Picture 001.jpg' 'Picture 014.jpg' 'Picture 028.jpg' 'Picture 034.jpg' 'Picture 003a.jpg' 'Picture 015.jpg' 'Picture 028r.jpg' 'Picture 038a.jpg' 'Picture 003b.jpg' 'Picture 016.jpg' 'Picture 028s.jpg' 'Picture 038.jpg' 'Picture 003.jpg' 'Picture 018.jpg' 'Picture 029a.jpg' 'Picture 039.jpg' 'Picture 009.jpg' 'Picture 019.jpg' 'Picture 029b.jpg' 'Picture 040.jpg' 'Picture 010.jpg' 

Normally index 001 should be before 001b and 001c (same for 003, 003a, 003b). I tried to pipe ls | sort with different options, but the result remains the same.

Interesting to see, that pictures with index 028, 028r, 028s were sorted correctly. I played around with the indices 001 and followings and found out that beginning with 001k it was sorted correctly.

I tried string comparison in bash:

#! /bin/bash if [[ "Picture 001" < "Picture 001b" ]] then echo "true" else echo "false" fi 

results in true. but putting the same suffix behind both strings (like .jpg or .txt) results in false.

I don't understand this sorting algorithm and to be honest, it seems to have a bug. Other programs sort this correctly.

3
  • show your locale output Commented Jun 18, 2022 at 10:24
  • Could you run the command ls | od -x and post result ? Commented Jun 18, 2022 at 12:04
  • The result is too long to post it here. Here is a part if it helps: 0000740 6369 7574 6572 3020 3033 6a2e 6770 500a 0000760 6369 7574 6572 3020 3133 6a2e 6770 500a 0001000 6369 7574 6572 3020 3233 6a2e 6770 500a 0001020 6369 7574 6572 3020 3333 6a2e 6770 500a 0001040 6369 7574 6572 3020 3433 6a2e 6770 500a 0001060 6369 7574 6572 3020 3833 2e61 706a 0a67 0001100 6950 7463 7275 2065 3330 2e38 706a 0a67 0001120 6950 7463 7275 2065 3330 2e39 706a 0a67 0001140 6950 7463 7275 2065 3430 2e30 706a 0a67 0001160 6950 7463 7275 2065 3430 2e33 706a 0a67 0001200 Commented Jul 6, 2023 at 20:49

2 Answers 2

3

Can you try this?

printf '%s\n' Picture*.jpg | LANG=C sort 
Sign up to request clarification or add additional context in comments.

3 Comments

Works fine. Thanks. I didn't know that it's possible to work with C in BASH. My Problem: I'm not very familiar with C. Is there any other solution with BASH-commands?
@R.30 You already got a pure POSIX shell solution here, LANG=C doesn't have anything to do with the C language, it sets locale the sort command (any sorting is sensitive to the locale). You can also use LANG=C ls, or export LANG=C; printf '%s\n' *
Works. I put LANG=C in the beginning of the shell script and got correct results. Thanks.
0

This solution works too, when the suffix of files can be ignored (what it does in my case):

ls | sort -t . -k 1,1

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.