Bash – ‘ls’ showing two identical files in a directory

bashlsosx

So somehow the command ls seems to be showing me two identical files in a directory.

$ ls -Blah /System/Library/LaunchDaemons
total 32                                                              
drwxr-xr-x  266 root  wheel   8.8K Jun 18 10:41 .
drwxr-xr-x   79 root  wheel   2.6K Mar 31 12:28 ..
[redacted]
-rw-r--r--    1 root  wheel   715B Jun 18 10:36 tftp.plist
-rw-r--r--    1 root  wheel   715B Jun 18 10:35 tftp.plist

I can move, rename, edit etc. one of the files, but the other one does not even seem to be there. bash tab completion even shows identical files.

For example, entering the following and then hitting TAB

$ sudo mv /System/Library/LaunchDaemons/tftp
tftp.plist   tftp.plist

If I rename the file:

$ sudo mv /System/Library/LaunchDaemons/tftp.plist /System/Library/LaunchDaemons/tftp.plist.derp

The tab completion still shows the file:

$ ls -Blah /System/Library/LaunchDaemons/tf
tftp.plist       tftp.plist.derp 

But the original, unmodified file does not appear to 'ls'

$ ls -Blah /System/Library/LaunchDaemons/tftp.plist
ls: /System/Library/LaunchDaemons/tftp.plist: No such file or directory

However if I just list the files as in the first code snippet above, behold:

$ ls -Blah /System/Library/LaunchDaemons
total 32                                                              
drwxr-xr-x  266 root  wheel   8.8K Jun 18 10:41 .
drwxr-xr-x   79 root  wheel   2.6K Mar 31 12:28 ..
[redacted]
-rw-r--r--    1 root  wheel   715B Jun 18 10:35 tftp.plist
-rw-r--r--    1 root  wheel   715B Jun 18 10:36 tftp.plist.derp

Any idea what's going on here and how I can get rid of this ghost file?

This is a mac running OS X if that adds any info to the problem. I was using sed on this file just before the craziness began.

Edit

I have used both the blah and the Blah ls flags with no change in apparent output.

Edit 2

Additional info requested in comments:

$ echo tftp* | xxd
0000000: 7466 7470 2e70 6c69 7374 2020 7466 7470  tftp.plist  tftp
0000010: 2e70 6c69 7374 2e64 6572 700a            .plist.derp.

More:

$ printf '<%q>\n' tftp*
<tftp.plist\ >
<tftp.plist.derp>

Even more:

$ locale                                                                                                                      │-rw-r--r--    1 root  wheel   495B Sep  9  2014 org.net-snmp.snmpd.plist
LANG="en_US.UTF-8"                                                                                                            │-rw-r--r--    1 root  wheel   498B Jan 15 23:15 org.ntp.ntpd.plist
LC_COLLATE="en_US.UTF-8"                                                                                                      │-rw-r--r--    1 root  wheel   1.0K Nov 13  2014 org.openldap.slapd.plist
LC_CTYPE="en_US.UTF-8"                                                                                                        │-rw-r--r--    1 root  wheel   572B Sep  9  2014 org.postfix.master.plist
LC_MESSAGES="en_US.UTF-8"                                                                                                     │-rw-r--r--    1 root  wheel   238B Sep  9  2014 shell.plist
LC_MONETARY="en_US.UTF-8"                                                                                                     │-rw-r--r--    1 root  wheel   941B Sep  9  2014 ssh.plist
LC_NUMERIC="en_US.UTF-8"                                                                                                      │-rw-r--r--    1 root  wheel   260B Sep  9  2014 telnet.plist
LC_TIME="en_US.UTF-8"                                                                                                         │-rw-r--r--    1 root  wheel   715B Jun 18 10:36 tftp.plist
LC_ALL="en_US.UTF-8"

Note

The answer below helped me see that there was a trailing space in the name.

Best Answer

You either have trailing whitespace, or a corrupt filesystem.

Try

for i in tftp.plist*
do
    echo "'$i'"
done

That should output something like

'tftp.plist'
'tftp.plist '

note the quotes and the extra space. If it outputs the exact same thing twice, you likely have a corrupt filesystem.

Try

ls -i tftp.plist*

this will give you the inode numbers of the file. If they're the same, you have the same file twice in your directory. That would be Really Bad(tm), and you should run fsck asap. But I doubt that's the problem; it's more likely the whitespace thing.

Related Question