Conquer The Command Line: The mv Command

Jim Homme
3 min readDec 15, 2020

--

In this installment of “Conquer The Command Line” we talk about how to use the Linux mv command.

When ever you want to move or rename files or directories, use the mv command.

Here is how the mv command works. You type mv <item1> <item2> ..., where the items are either files or directories.

Let’s say the command contains the mv keyword followed by two items. This is how to rename a file or directory.

If the first item is the name of a file that exists and the second item is a new name of a file that doesn’t exist, mv renames the file, giving the first name the second name. Let’s say you have a document named daily and you want to rename it to journal. To do that, type mv daily journal.

The same thing happens if the first item is a directory and the second item is a name that doesn’t exist. If you have a directory named data and you don’t have one named docs, you can rename the data directory like this. mv data docs.

Again let’s take an example where the command line contains the mv keyword and two items, but this time the first item is a file name and the second item is a directory name. If that happens, you would move the file into the directory. For instance, let's say you have a file named water.log and you want to move it into the docs directory. Type mv water.log docs.

You can do the same thing if you want to move a directory into another one. For instance, if you have a journals directory that you want to move inside your docs directory, you could type mv journals docs.

Using Paths With mv

I intentionally made the above examples simple. You can also include paths on the command line and rename and move files and directories. The idea is to grasp how this works before you get fancy.

Essential Options

-i makes mv prompt you to confirm that you want to overwrite files and directories regardless of file permission. You might want to consider using it to keep yourself from accidentally destroying something important.

-n: Don't overwrite files. This is another way to keep yourself from deleting something you want to keep. The difference between this option and -i is that Linux doesn't prompt you. It skips replacing files automatically.

You can make directories with the mkdir command. You can remove directories with the rmdir. You can use cd to change to, or move into directories. You can use the pwd to display or print the name of the current directory. You can use the ls command to list the contents of a directory.

Remember to use the man command to find all the options for any command on your system. And remember that for a less technical explanation of commands to check out tldr pages.

Hopefully this post has helped you take one more step in conquering the command line. I welcome any feedback.

This post was originally written at https://www.jimhomme.com

--

--

Jim Homme
Jim Homme

Written by Jim Homme

Musician, father of three, husband. Information Technology veteran. Bookworm. Chess player. Accessibility consultant. Plain language advocate