How to Rename a File, Folder, or Directory via Terminal on Mac

If you are a new user of command line interfaces such as Mac’s Terminal application, you are probably starting to learn some of the basic commands. Learning how to rename files and folders or directories is important because it can be a key task you may need to do often.

Renaming a file, folder, or directory is straightforward in the Terminal application. The shell environment does not provide us with a command called rename, but you can use the mv command to rename a file, folder, or directory. It will move the file or directory from one name to another.

My name is Eric, and I have worked as a software Engineer for over 25 years. I use Mac Terminal daily, and renaming a file or directory is a common command for me, and I can help you quickly learn how to do this.

If you would like to see how to rename items using the Terminal application, continue reading below. We will cover the basics, provide a few tips and maybe learn more about the mv command.

Using the mv Command

Before we get started, I just want to mention that the term folder and directory are one and the same for our purposes here. When working from the command line, most old schoolers like me use the word directory, which we will use for the rest of this article.

As I mentioned above, Mac’s Terminal application and its shell, Z shell, do not have a rename command. We simply use the mv (move) command to rename a file or directory. 

Of course, you can use this command to move a file or directory from one location to another, which is technically what we do when we rename it. We are moving it from one name to another. It is just that the item usually remains in the same location, but it does not have to.

As we go through the different scenarios below, I will use examples with a file, but renaming a directory would be the exact same command and process. Just substitute the directory name for the file name, and you will get the same results.

Simple Rename

Let’s start with a simple renaming in which we are in the directory where the file exists, and we will just rename it right there in the same directory. All you need to do is run the mv command with the following format.

mv OldFileName NewFileName

It’s that quick! You can use the ls command to view your current directory and see the newly renamed file.

Rename from a Different Directory

If I want to rename a file that is not in my current directory, I don’t necessarily have to navigate to that directory to rename it. If I know the path, I can specify the complete path in the mv command. 

For example, if I used the find command to find a file and get its path, I can then rename it using that path. The command would look something like the format shown below.

mv /FilePath/OldFileName /FilePath/NewFileName

Let’s take a look at a quick example. I have a file called test.txt somewhere on my computer. I want to rename it, but I don’t even know where it is. I will use the find command to locate it and then rename it using the following steps.

Step 1: Find the test.txt file.

I will use the find command to find the file test.txt.

find . -name test.txt

Step 2: Copy the path and filename

Step 3: Use the mv command to rename it.

mv ./Documents/test/test.txt ./Documents/test/testcomplete.txt

Rename and Move to a Different Directory

In some cases, you might want to rename a file but also move it to a different directory at the same time. It’s basically the same steps; the only difference is that you will put the new path and new file name in the second parameter. 

We will use the example above, but we will have the newly renamed file put into a new directory under the Documents folder called CompletedTests. The command will have the format shown below.

mv /OldPath/OldFileName /NewPath/NewFileName

Using our example, the command would be the following.

mv ./Documents/test/test.txt ./Documents/CompletedTests/testcomplete.txt

FAQ

Below are some questions I frequently hear people ask about renaming files using the mv command.

What if I see a permission denied error message?

This can happen if you are not the owner of the file or directory. The file or directory could be owned by another user or by the root user. If your login has admin privileges, all you need to do is prefix the mv command with sudo and type in your password after hitting return.

sudo mv OldFileName NewFileName

If I move a directory, does it also move all the files inside of it?

Yes. Unlike the cp (copy) command, where you need to specify that it copies recursively, the mv command will automatically move everything underneath the directory to the new directory location and name.

Does moving a file change its date or permissions?

No. When you move or rename a file, it will keep all of its original attributes except for the name and possibly the path if you put it in a new location. The date/time stamp, owner, and permissions will all remain the same.

Conclusion

As you have seen above, the mv command is what you need to use to rename a file or directory. It can be a simple command to use. However, there are several ways you can use it within your current directory, a directory you specify, or renaming and moving to a new directory.

I hope the information I have provided above can help you to be able to rename files, folders, or directories when you need to do so. As usual, let me know if you have any questions.

Leave a Reply

Your email address will not be published. Required fields are marked *