Moving and Copying

 

Moving

Introduction

Finally! Let’s talk about breaking your computer. Oh you think I’m joking, but you will break your computer. You will move or delete a file and it will be gone forever. Or your computer won’t boot anymore. This WILL happen at some point. Do not blame me. I give you the gun, you get to shoot yourself in the foot. That being said, when moving or deleting files: check, double check, triple check your typing. A small error can make a big difference.

To move a file or directory is very simple. The easiest form of this is:


#> mv sourceFile destinationFile


This will move (or rename) the file named sourceFile to destinationFile. For a directory the command is the exact same. It will move (or rename) the directory, and the contents inside that directory will be untouched.


Lets try an exercise:


The touch command in this case will create a new, blank file with the name newfile. Touch will seem like a pretty useless command to you for awhile, but remember it in the back of your mind. You might need it. Then I use mv to rename the file from newfile to differentfile with a couple of ls commands in between to prove the magic.


So if it is renaming the file, why is it called move? Because you use the same command to move the file around the hard drive of course!


This time the file doesn’t get a different filename, it gets a new location all together. How about an example of the danger I was talking about above?


In this example I have two files in a directory called Project. One is an important file that I wouldn’t want to lose, and the other one is a not so important file. If I attempt to move notAsImportant to importantFile the system, by default, does not ask me if I’d like to overwrite an existing file. It will automatically do so. This would be a problem if I was overwriting a system file or my homework. You can limit the chance of this by using the -i switch.


As you can see the -i will ask if I’m sure i want to overwrite the existing file. This is a helpful thing to use when first starting out, and I strongly encourage it’s use. However, eventually you’ll get annoyed by it I’m sure and stop using it. Then you will lose files and feel sad. The direct opposite of the -i switch is the -f switch. It will ensure that when moving a file from one location to another that you overwrite existing files. This is dangerous, but eventually a lot more useful than the -i when you get used to the terminal.


The last thing we’ll talk about when moving files is moving multiple files. If you have multiple source files you want to move to the same directory you can specify them all at the same time. The last argument of the command must be a directory name (or alias).


This example has us moving two of the three files from the Project directory to a new directory called NewProject. If NewProject did not exist, we would receive an error message.


Not entirely helpful, but basically it is indicating that we are running the program incorrectly and need to try again.

 

COPYING

As luck would have it copying a file is remarkably similar to moving a file. The basic makeup of the command is exactly the same.


When copying multiple, just like when moving, the final argument needs to be a directory or else you will receive an error message.


When copying files it is sometimes useful to see what file has been copied. This is closest we’ll get to a progress meter. This mode is called verbose mode, because instead of the program being quiet or terse, it will print out a summary of what it is doing. The option switch for verbose mode is -v.

Copying a directory can be a little trickier because by default the cp utility will not copy a directory. You have to tell it that you’d like to recursively copy the directory to the new location. This is done with the -R option, and will cause the contents of the directory be copied.

Notice that the entire srcDir directory has been fully copied into destDir. This is important to note, because if you were to put a forward slash (/) after srcDir it would actually only copy the contents of the directory.


Be very careful about that distinction, as at times it can make a huge difference. For instance if you were copying multiple directories into the same directory, you might accidentally combine the contents of all of the contents into one.