Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

Apparently you can rename file to ....

If I were insane, how would I rename file to .. or .? Is such a filename even allowed?

Backslash doesn't seem to disable dot's special meaning:

$ mv test \.
mv: `test' and `./test' are the same file
share|improve this question
9  
. and .. already exist. And dot does not have a special meaning. –  richard Aug 5 at 17:48
2  
I wonder if you could create files named . or .. from Windows onto external FAT or NTFS media, then try to mount that into a Unix-based system. Hm... –  IQAndreas Aug 5 at 23:30
2  
@IQAndreas nope, Windows uses . and .. about same way (superuser.com/a/571364/330318) as Unix does. (And heck lot of other special names: superuser.com/a/217526/330318) –  PTwr Aug 6 at 8:01
1  
@PTwr The windows story is trickier than that. Some special things happen at the file system level / NT API level, some in the Win32 subsystem and some and the shell level. For example the GUID magic from your second link is at the shell level, so you won't see them in a typical command line application. I'm not sure if . and .. actually exist in NTFS and the NT API or if they're only added by the Win32 API. I know that there are several discrepancies between the lower level NT API and Win32, for example Win32 hides several files from the drive root. –  CodesInChaos Aug 6 at 8:43
    
@CodesInChaos as far as I know dot/dotdot are virtual instances and are emulated in file system driver, and it was same in old FAT which borrowed (problematic) idea from Unix. As per second link, I admit to not reading it fully before posting. Something like this one would probably be better. (but again, this one lacks full ./.. info) –  PTwr Aug 6 at 8:59

3 Answers 3

up vote 44 down vote accepted

You can't rename a file to . or .. because all directories already contain entries for those two names. (Those entries point to directories, and you can't rename a file to a directory.)

mv detects the case where the destination is an existing directory, and interprets it as a request to move the file into that directory (using its current name).

Backslashes have nothing to do with this, because . is not a shell metacharacter. \. and . are the same to bash.

share|improve this answer

.. is not special, it is just that it already exists.

On Unix, Dos and MS-Windows every directory has a directory . it links back to itself, and a directory .. it links to its parent directory (or self if root directory).

If .. and . are special it is only because you can not remove them (actually you can, you just remove the directory that contains them).

Therefore you can not name (any other) file . or ...

However you can create files ..., \, , ..  (note there is a space after the .., but you can hardly see it here, or easily in you directory listing) or any other name you like; The only reserved character is / (Warning — advanced details: and null, null is a special character, not used for anything except to mark the end of things and sometimes as a separator). . has no special meaning: not to file names, kernel or to the shell, it does not need escaping. Actually if a file-name starts with a . then it is special, the file is normally hidden, but still it does not need escaping.

Aside

This hidden file behaviour came about in an early implementation of ls where the author wanted to hide . and .., so they wrote code to hide any files starting with a .. Other users noticed this bug/feature and started creating files starting with a . when they wanted the file to be hidden.

Explanation of Linked question

In the question you link to the questioner is trying to move the file to the parent directory .. but ends up renaming to ..., files starting with a dot are by default hidden, that is why they can not find it.

When using mv in the form mv a b

  • If you move to . it is effectively a no operation, but mv treats it as an error.
  • If you move to .. it will move the file to the parent directory.
share|improve this answer
    
What do you mean by If you move to . it is effectively a no operation? It is not a "no op" it is an error if you try to move a file from A to B when A and B are the same thing. More practical demonstration of this error is mv foo ../some_dir/foo or mv foo some_subdir/../foo. –  Brandin Aug 5 at 18:59
6  
For some definition of 'character', NUL (0x00) is also restricted from use in file and directory names. –  user117529 Aug 5 at 21:22
1  
The distinction between reserved character (i.e. / and NUL) and reserved name (i.e. . and ..) could be made clearer. Also it is worth pointing out that . at the start of the name has special meaning only at the application layer and not in the kernel or system call API. –  kasperd Aug 6 at 10:01
    
I think your space got lost. –  nyuszika7h Aug 6 at 14:50
    
@kaspend I added a bit about . at start, and that .. is not special, it just already exists, and you can not remove it. –  richard Aug 6 at 18:30

The problem is you're moving a file onto a directory. This is allowed to fail.

I'm going to tell you how it once was.

mkdir used to essentially read this (while I'm writing this in sh, it was really written in C and setuid-root).

mknod d $1
ln -d $1 $1/.
ln -d `dirname $1` $1/..

So as you can see there's not much special about . and .. except for the fact they are created for you by mkdir and already exist. There's code now that says you cannot remove them, but it wasn't always the case.

rmdir used to look like this:

rm -d $1/..
rm -d $1/.
rm -d $1
share|improve this answer
    
+1 for the history lesson, and adding something not properly covered. –  richard Aug 7 at 9:48

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.