I was in my /etc
directory and typed the command
me@mylaptop:/etc$ sudo mv MyFile.conf /openvpn
I thought that would move the file into the directory openvpn
which is a subdirectory of the /etc
directory. However, that doesn't seem to be what happened. All that I can confirm is that the command got executed.
Provided a directory with this name doesn’t exist, you renamed the file to
openvpn
located in your root directory/
. To revert this, run:If the directory did exist you moved the file inside it, then the reversion is:
To move it to an existing subdirectory of the current directory you can run any one of these – those use relative paths:
.
is a link to the current directory, so./
at the beginning of a path means “in this directory”. The slash at the end means thatopenvpn
is a directory and you want to move the file inside it. If you don’t provide it, the file will be moved into it if a directory with this name exists, else the file will be renamed toopenvpn
. With a/
at the end,mv
will warn you if the directory is missing and not rename, so that’s the secure way if you don’t want to rename.If your path begins with
/
on the other hand it’s an absolute path regardless of the current working directory, it’s always relative to the root directory/
. Using absolute paths for both file and destination your command would look like this:Further reading
You need to use
to move the
MyFile.conf
file into theopenvpn
sub-directory of/etc/
.On the other hand
would move the file to the
/
directory asopenvpn
(i.e. rename asopenvpn
) instead (assuming/openvpn
directory doesn't exist).When you move a file, you tell the OS the directory into which you want to put it, or the new path you want to give it.
You told the OS "/openvpn". So it's going to do exactly that - check if /openvpn exists (meaning an object called "openvpn" located in the root directory) and is a directory, and if so move your file into it, or else if /openvpn is a file or doesn't exist, rename and move your file so it's now accessible as /openvpn.
What you wanted it to do was move it to within a directory "openvpn", which is to be found in your current directory, not one that's in the file system root directory (which is what the leading "/" signifies). So you had to point to that dir in the move command, not to absolute path "/openvpn". Any of these will work:
mv MyFile.conf openvpn
- look in my current dir for this "openvpn"mv MyFile.conf /etc/openvpn
- look for absolute path /etc/openvpnmv MyFile.conf openvpn/
- look in my current dir, for a dir called openvpnmv MyFile.conf ./openvpn
- makes the "current dir" even more obviousShort version - you probably used "/" to mean "my current directory", therefore "/openvpn" to mean "openvpn which is in my current directory". But a leading "/" only ever means "the root directory", so "/openvpn" meant "openvpn in the root directory". What you needed was simply to use "openvpn" or "openvpn/", meaning "openvpn in my current directory".
Any path that starts with
/
is an absolute path, not relative.If all paths were always relative to the current directory, how would you
cd /etc
in the first place? You'd have tocd ../../../../../etc
and hope that was enough levels of..
, or just keep doingcd ..
until you got to the root directory.Or you'd need some other syntax to express absolute paths. But Unix decided on
/
meaning absolute, anything else being relative to the process's current working directory. Somv MyFile.txt openvpn
would work.And no, it wouldn't work well to infer absolute vs. relative from files existing or not. We wouldn't want
mkdir
system calls to treat paths differently fromchdir
orrename
system calls, and making themv
program do it just leaves room for inconsistency betweenmv
and some other program that takes an output filename.mv
is already special because when therename()
destination is a directory, it appends the source filename to that destination directory and tries again. But notice that one simple implementation strategy relies of the firstrename()
system call failing withEEXIST
orEISDIR
. So we have to know whether a path is relative or absolute before checking the filesystem.(Early Unix ran on slow computers, where extra checks if a directory existed could mean extra I/O if it wasn't cached, or more pressure on directory caching. But I think sanity / correctness arguments are sufficient to explain why your first guess wasn't a plausible way for the system to work, without resorting to historical efficiency arguments.)
To complete the other answers :
is the same as, for Windows
The filesystem in linux is thought in accordance to the filesystem hierarchy standard, or FHS for short. it helps you knowing where should a file be placed depending on its nature and content.
The first folder (equivalent to
c:
in Windows) is/
.