TL;DR
This forces the destination to be a directory that already exists or the command to fail without effect and is used as a fail-safe.
The meaning of .
Every directory in typical Unix/Unix-like filesystems include two special directories: one referencing the directory where it is: .
and one referencing the parent directory of where it is: ..
(allowing to move back in the hierarchy of directories). They are relative to where they are found. So the directory .
inside a directory named backups
references backups
. To tell it otherwise, when there's a directory named backups
, backups/.
is equivalent to backups
.
Different behaviors with the source or destination being a file or directory
If the directory exists, the two commands will have the same effect: when the target is a directory mv
will move source(s) to that directory. That's the expected outcome.
If the target backups
is a file instead of the directory:
- 1st case will fail with the error
Not a directory
- 2nd case will rename
Caiti.bak
and overwrite backups
If the target doesn't exist:
- 1st case will fail with
No such file or directory
- second command will rename the file
Caiti.bak
into the file backups
, leading to the previous case and possible loss of data the next time it happens.
That's good practice to append /.
to a directory target supposed to be existing and abort a script at an error from this.
All these cases would work the same when only appending /
if the source is a file rather than a directory.
If we can't assume this, this example where the source is a directory:
mv somedirectory targetnothere/
would not fail (and somedirectory will be renamed instead of being put into targetnothere or abort). The extra .
will make this case fail too:
$ mv somedirectory targetnothere/.
mv: cannot move 'somedirectory' to 'targetnothere/.': No such file or directory
backups
directory when it comes after that as inbackups/.
. It’s an actual directory in each directory, pointing to the directory containing it.mv
(and some similar commands) have an additional option likemv -t target file ...
. This has two benefits. (a) It explicitly checks target is a directory, avoiding the rename-file ambiguity. (b) It is suitable for use withxargs
because the file list comes last, not the target directory name..
is actually a directory entry on disk. The special handling of.
in POSIX compliant systems was inherent to the filesystem structure itself originally, not the VFS. However, many newer filesystems do not store physical.
(and..
) entries in the directory, instead generating them forreaddir()
calls as-needed, though that's still part of the FS itself and not the VFS layer in most cases (though the VFS is usually responsible for normalization of stuff like./././.
).backups
doesn't exist yet, but it won't do what you were expecting, and would be disastrous in the long run.