14

I downloaded two files, they were both saved to the same filename and then I transferred them with scp to another computer.

Why didn't they become one when the second was saved?

3
  • 2
    Can you show the results of ls -la please?
    – ryekayo
    Commented Aug 15, 2014 at 21:24
  • 2
    Better yet, ls -lb (or ls -lab, if the name begins with .). Commented Aug 15, 2014 at 21:45
  • 1
    Also, what, exactly, happened? You downloaded dir1/foo and dir2/foo on machine X, then scp'ed them to dir42 on machine Y, and they came out as separate files with the same name? Or you downloaded two files with the same name into the same directory (on machine X), and they came out as separate files with the same name? Commented Aug 15, 2014 at 21:48

1 Answer 1

41

Maybe they only look like they have the same name.

Try:

$ touch Stéphane Stéphane Stéphane\  St​éphane
$ ls -1
Stéphane
Stéphane
St​éphane
Stéphane 

They look pretty much the same.

$ ls -1b
Stéphane
Stéphane
St​éphane
Stéphane\ 

Slightly better. The space character is flagged as \ (though not all ls implementations do that).

$ LC_ALL=C ls -1b
Ste\314\201phane
St\303\251phane
St\303\251phane\ 
St\342\200\213\303\251phane

Now we're talking (all non-ASCII characters are rendered as the octal value of their byte constituents)

You could also do, and that works for any input:

$ ls | LC_ALL=C sed -n l
Ste\314\201phane$
St\303\251phane$
St\342\200\213\303\251phane$
St\303\251phane $

Here, the end of lines is marked with $ which makes it easier to spot the trailing space. However, that won't help spotting a file called Stéphane<newline>Stéphane

$ ls | perl -Mopen=locale -MUnicode::UCD=charinfo -lpe '
       s/[^\41-\177]/"<".charinfo(ord$&)->{name}.">"/ge'
Ste<COMBINING ACUTE ACCENT>phane
St<LATIN SMALL LETTER E WITH ACUTE>phane
St<ZERO WIDTH SPACE><LATIN SMALL LETTER E WITH ACUTE>phane
St<LATIN SMALL LETTER E WITH ACUTE>phane<SPACE>

Makes it clearer what happened.

See also this other answer for more on the subject.

6
  • 1
    If you don't mind me asking Stephane, what does the piped command LC_ALL=C sed -n 1 do?
    – ryekayo
    Commented Aug 15, 2014 at 21:59
  • 3
    sed's l command displays the input in visually unambiguous form. With LC_ALL=C, we make sure it uses only ASCII characters for that (though at least with GNU sed, it is not necessary). Commented Aug 15, 2014 at 22:07
  • 2
    @StéphaneChazelas You can also use: LC_ALL=C ls -b
    – vinc17
    Commented Aug 16, 2014 at 0:11
  • @vinc17, good point, that's also better than sed to spot files with newline characters, but not always to spot trailing space. I've added it to the answer. Commented Aug 16, 2014 at 7:11
  • Damn, TIL that you can have spaces at the end of a file name and it looks like there are two identically names files on the filesystem.! Great answer, ls | LC_ALL=C sed -n l really was a big time saver just now, thanks! Commented Mar 16, 2020 at 1:06

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .