15

Consider the following directory structure, printed to the screen using ls -l "$dir" ($dir is a bash variable standing for some folder on the same server, a few levels up), on a remote Ubuntu server (more precisely, Ubuntu 16.04.4 LTS (GNU/Linux 4.4.0-116-generic x86_64)):

-rw-r--r-- 1 user group     12 Apr  2 12:34 FILE1
-rwxr--r-- 1 user group    287 Apr 23  2017 File_
-rw-r--r-- 1 user group      0 Apr 21  2017 file_1
-rw-r--r-- 1 user group    272 Apr  3 22:16 myFile
-rw-r--r-- 1 user group  47633 Apr 11 14:01 myFile.txt
-rw-r--r-- 1 user group  37030 Apr 11 14:16 myfile1
-rw-r--r-- 1 user group      0 Apr 11 21:23 nothing_here
-rw-r--r-- 1 user group      2 Apr 22  2017 small_file
-rwxr-x--- 1 user group 262529 Apr  8 15:31 story

This is coming from a MacBook, and using the built-in terminal and the out-of-the-box installation of ssh (OS X Version 10.13.3, as the terminal tells me).

Consider, in comparison, the same server accessed from a Windows machine running PuTTy release 0.70:

-rwxr--r-- 1 user group    287 Apr 23  2017 File_
-rw-r--r-- 1 user group      0 Apr 21  2017 file_1
-rw-r--r-- 1 user group     12 Apr  2 12:34 FILE1
-rw-r--r-- 1 user group    272 Apr  3 22:16 myFile
-rw-r--r-- 1 user group  37030 Apr 11 14:16 myfile1
-rw-r--r-- 1 user group  47633 Apr 11 14:01 myFile.txt
-rw-r--r-- 1 user group      0 Apr 11 21:23 nothing_here
-rw-r--r-- 1 user group      2 Apr 22  2017 small_file
-rwxr-x--- 1 user group 262529 Apr  8 15:31 story

Notice the different sorting of the first 6 files in each code block.

Considering that the commands are running on the server, and not on the client computers, one would expect no difference between the two outputs. What's the reason they're not identical, then?

Edit 1: As mentioned in the comments, I've checked and the Mac's terminal is set to xterm-256color while the Windows one is set to xterm. A quick change of the Mac's setting to xterm had no effect.

Edit 2: As mentioned in the comments, I've tried running whereis ls, which gives the reasonable location you'd expect to find ls - i.e. bin/ls.

Edit 3: Got rid of the terminal images, traded them for properly-formatted code as per this meta post.

Edit 4: Added quotes around "$dir" to prevent problems with spaces.

2
  • Please remember to quote your variable, thus: ls -l "$dir". Without this, if the folder name were to contain a space, you'd get a nonsense result. Apr 24, 2018 at 6:00
  • @PaddyLandau you're right. Edited that.
    – Tom Granot
    Apr 24, 2018 at 15:01

2 Answers 2

30

Sorting order is defined by the locale variables LC_ALL, LC_COLLATE and LANG (in this order, the first being the strongest). These are also some of the few variables that typically can be set by the ssh client. Check and compare these variables. You might want to set them to a well-defined value on the server (e.g. in .bash_profile), or make sure that your ssh clients set the same consistent value.

0
4

What eventually worked was the "Stop Forwarding Locale From The Client" sub-section in this answer. Just comment out out the SendEnv LANG LC_* line under Host in the local /etc/ssh/ssh_config file. Some explanation about what it does can be found here.

1
  • Moved the verified answer to @egmont 's one since it actually explains the issue instead of providing a hack-y "solution".
    – Tom Granot
    Sep 6, 2020 at 15:33

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.