10

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.

0

5 Answers 5

51

Provided a directory with this name doesn’t exist, you renamed the file to openvpn located in your root directory /. To revert this, run:

sudo mv /openvpn /etc/MyFile.conf

If the directory did exist you moved the file inside it, then the reversion is:

sudo mv /openvpn/MyFile.conf /etc/

To move it to an existing subdirectory of the current directory you can run any one of these – those use relative paths:

mv MyFile.conf openvpn
mv MyFile.conf openvpn/
mv MyFile.conf ./openvpn
mv MyFile.conf ./openvpn/

. is a link to the current directory, so ./ at the beginning of a path means “in this directory”. The slash at the end means that openvpn 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 to openvpn. 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:

mv /etc/MyFile.conf /etc/openvpn
mv /etc/MyFile.conf /etc/openvpn/

Further reading

1
  • 2
    If you don't know whether or not the directory existed, try the sudo mv /openvpn/MyFile.conf /etc/ command first. In the likely scenario the directory didn't exist, it'll just give you an error (probably "Not a directory") and not touch the filesystem. Then, you can just do sudo mv /openvpn /etc/MyFile.conf.
    – Sparkette
    Feb 13, 2019 at 19:17
9

You need to use

me@mylaptop:/etc$ sudo mv MyFile.conf openvpn/

to move the MyFile.conf file into the openvpn sub-directory of /etc/.

On the other hand

me@mylaptop:/etc$ sudo mv MyFile.conf /openvpn

would move the file to the / directory as openvpn (i.e. rename as openvpn) instead (assuming /openvpn directory doesn't exist).

1
  • 3
    Also, the slash at the end of the first command is optional. But I guess it's a good habit to get into, as it'll make the command fail if it isn't a directory, rather than creating a confusing situation like OP described (or worse, overwriting an important file.)
    – Sparkette
    Feb 13, 2019 at 19:20
4

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/openvpn
  • mv MyFile.conf openvpn/ - look in my current dir, for a dir called openvpn
  • mv MyFile.conf ./openvpn - makes the "current dir" even more obvious

Short 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".

1
  • 1
    Not sure why this answer was downvoted; it's correct and I actually think it's the clearest! Feb 12, 2019 at 21:40
1

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 to cd ../../../../../etc and hope that was enough levels of .., or just keep doing cd .. 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. So mv 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 from chdir or rename system calls, and making the mv program do it just leaves room for inconsistency between mv and some other program that takes an output filename.

mv is already special because when the rename() 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 first rename() system call failing with EEXIST or EISDIR. 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.)

1

To complete the other answers :

mv file.txt /location

is the same as, for Windows

mv file.txt c:\location

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 /.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .