The mv Command

Moving files is not like copying them. Copying them leaves the source of the data in the original location in addition to creating a duplicate copy in another location, or in the same location with a different name. To move a file or directory, you need to use the mv command. The mv command will move or rename a file based on the destination. Depending on what you specify as the destination, the filename is going to specify how you used the command.

In other words, look at the following two examples of the mv command:

1.
Locate a file you want to move by its filename. This is the source file you want to move.

2.
Issue the mv command as mv <sourcefile> <directory>.

When used in this fashion, the command will move the selected file to the directory you want it to be in. You can also issue the same command to rename a file. It will also leave the source alone; however, when it copies the file (you can even do this to the same source directory), it is new and the contents are identical. To Unix, there are two completely different files located in the same directory.

To rename a file is not land keep it in the same source directory, issue the mv command as mv <currentname> <newname>.

Why could it not be in the same source directory unless you renamed it? Well, then it would be two instances of the same thing in one directory, and Unix will not allow for that—nor any other operating system. For the file system to be able to manage itself, having duplicate entries in the same source without a way to distinguish the two is not only impossible, but useless—if you edit one, then when you save it (besides for some file size changes), how would you know which is which?

You can do the same exercise with directories as well—just specify directories instead of filenames.

Where Did I Put That Thing? If you move something, you could lose it. So you must be careful and think through your actions. You could inadvertently move data that could ruin a website, or disable a service or application.

Copy is always your best choice, but then you pile up data on your hard disk. If you are not thinking about your actions, you could add storage stresses with unnecessary copying or disable your system by moving something you should have copied. Pay attention and if you make a mistake, contact your Unix system administrator for help, or revert to some of the older commands you learned to help you find what you need.


Let's take a quick look at the mv command in action:

> mv /priv/home/rob/storage/HTML/index.htm /etc/HTMLAB

Remember, this will move the source you specify (index.htm) to the HTMLAB directory located in the /etc directory. It's that simple. What if you wanted to move more than one source to a destination? That would be common, much like using the cp command; in that case, you would specify more source files to move.

To move multiple files to a directory, do the following:

> mv /priv/home/rob/storage/HTML/index.htm /priv/home/rob/storage/HTML/GRAPHICS/b1.jpg b2.
jpg /etc/HTMLLAB

This moves the index.htm from the /HTML directory as well as a couple of JPEGs (image files) to the /HTMLLAB directory within the /etc directory.

Before we end this part and move on, there is one limitation to the mv command that you need to know about. In a previous lesson, we discussed the fact that you may have data stored locally on your system, and you may have a network connection to other Unix systems that you may access for data. You accessing their local data would mean that you are accessing that data remotely. If you access data remotely, you may have issues with the mv command. Because the mv command cannot move directories between physical devices, you may need to copy the file from one location to another, and then delete the original. Up to now, we have learned all the commands needed to complete such an operation, shown here in this example of transferring data over a network.

Now that we have learned how to copy and move directories, we will do our last exercise of this lesson: learn how to create links. Remember, we discussed links in Lesson 5, “File System Navigation Techniques,” when we saw files “linked” to others.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.145.66.67