Linux – bash – directories ending in . and whitespace cannot be removed

bashfilenameslinuxmacosunix

On my external hard drive are two directories, Fun. and Complex_, where that _ is a whitespace. I cannot empty from my trash or remove them with rm -rf and I want to.
Neither show up in Finder on OSX, and I don't remember if they showed up in dolphin on linux. But they do show up in bash.

rm and mv returns No such file or directory. Containing folders can be moved and bring the special directories with them but they cannot be deleted.

Why can ls detect the files but rm and mv can't? How can I fix them? I am using \_on the complex file. (_ is whitespace).

Edit

The characters must not be the problem. I made "test." and "test " and I had no trouble removing them using quotes. But these dirs still cannot be removed. There must be something very low level wrong with the actual files themselves

sagan:Math ptwales$ ls
Complex 
sagan:Math ptwales$ ls -i
ls: Complex : No such file or directory
sagan:Math ptwales$ ls -idF *
ls: Complex : No such file or directory
sagan:Math ptwales$ find . -name * -print0
find: ./Complex : No such file or directory
sagan:Math ptwales$ 

Same results with Fun. I'm guessing that the parent dir's have links to the files and know their names but files just don't exist or are corrupted somehow.

The external drive is in exFAT format. Would that be relevant?

Best Answer

rm and mv can also detect the directories just fine, but it you type: rm file then the shell will ignore the whitespace.

There are a few things you can do to work around this. The easiest is to encapsulate the filename in quotes. Another solution is to escape the special char, e.g. with a backslash

Example:

touch "testfile "

host:/home/username/test>ls -al
total 12
drwx------   2 hennes  users  4096 Sep 26 02:44 .
drwxr-xr-x  34 hennes  users  8192 Sep 26 01:39 ..
-rw-------   1 hennes  users     0 Sep 26 02:44 testfile

Now to delete them, either:

rm "testfile ", or

rm testfile\ (please notice the whitespace after the backslash)


As for filenames ending in a dot. I can not reproduce the problem. Are you sure it ends in a dot and not in some other special char?

toad:/home/hennes/test>bash --version
GNU bash, version 4.1.10(1)-release (amd64-portbld-freebsd7.3)
Copyright (C) 2009 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later 

This is free software; you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.

toad:/home/hennes/test>touch Fun.

toad:/home/hennes/test>ls
Fun.

toad:/home/hennes/test>rm Fun.
toad:/home/hennes/test>ls
toad:/home/hennes/test>

If it is another char than a dot then read up on the Internal file separator in bash. You can set it to something else, or just use commands sunch as find with -print0. (Example find /path/to/search-name 'SomeFilePattern*' -print0 | some_command)

Related Question