Moving and Renaming Files in the Command Line

The commands to move, copy and delete files and folders in the command line, are actually pretty easy to remember. In this video, we'll move and rename files. In the next video we'll copy and delete files.

mv = move (also used to rename items)
cp = copy
rm = remove (or delete)

The syntax for "mv" and "cp" are the same. You'll type " ".

Moving Files

For example, let's move the Test.txt file up one level into the "Example1" directory.

The command I'll use is this: "mv Test.txt ../."

Remember that the ".." means "up one directory" and the "." means the current directory, so we're basically saying "Go up into the Example1 directory and place the file here." When I press Enter, you'll see the file move over in the Finder.

If you want to move it down a directory, back into the Test directory, we can move up into the Example1 directory by typing "cd .." and pressing Enter. Then, the command would be "mv Test.txt Test/.". Which is like saying, move the file into the Test directory, and place it here. Again, when I press Enter, you'll see the file move over in the Finder.

Renaming Files

The mv command is also used to rename items. You simply include the new file name in the location parameter. For example, let's rename the Test.txt file to Testing.txt. I'll move back into the Test directory by typing "cd Test/" and pressing Enter.

The command I'll use to rename the file is this: "mv Test.txt Testing.txt"

This will keep the file in the same directory, and simply rename it. When I press Enter, you'll see the name change occur in the Finder.

You can even move the file to a different directory and rename it at the same time. To demonstrate, I'll move the Testing.txt file up one directory, and rename it to Tested.txt all in a single command.

The command I'll use is this: "mv Testing.txt ../Tested.txt"

Now, when I press Enter, and we take a look in the Finder, we'll see the Tested.txt file is in the Example1 folder.