Home > News Tips

How to Rename a File or Multiple Files in macOS Terminal?

Updated on Tuesday, August 27, 2024

iBoysoft author Jenny Zeng

Written by

Jenny Zeng
Professional tech editor

Approved by

Jessica Shee

English Français Deutsch やまと Español Português

If you want to comply with a naming convention or organize your files more efficiently for easy retrieval, you can rename them using the macOS Terminal app. Renaming files in Terminal is faster than using the graphical interface, particularly when managing multiple files.

Terminal offers greater power and flexibility compared to the graphical interface. With Terminal, you can:

  • Rename files using regular expressions.
  • Rename files based on their contents.
  • Rename files from hidden directories.

Using Terminal, you have more control over the renaming process and can accomplish tasks that may be challenging or time-consuming with the graphical interface. In this article, we'll explain how to rename a file or batch rename files in Mac Terminal.

How to rename a file in Mac Terminal?

To rename a file in Mac Terminal, you can use the mv command. This command can rename or move files and directories. Simply open Finder, then click Applications > Utilities to launch the Terminal app. Then type the following command and press Enter.

 Tips: You can also use the following Terminal command lines to rename a directory(folder) and file extension on Mac.

Rename a file in Mac Terminal: (The above command line will move your file if you change the path of the renamed file.)

mv /path/to/old_filename /path/to/new_filename

If you want to rename a file on your desktop from 20230518.jpg to 2023-05-18.jpeg, type mv into Terminal followed by a space, and drag and drop the file to Terminal to acquire its path.

How to rename a file in Mac Terminal

Then copy the path and paste it into Terminal, use the left/right arrow on your keyboard to move the cursor and change the name in the copied path. The final command should be: mv /Users/user_name/Desktop/20230518.jpg /Users/user_name/Desktop/2023-05-18.jpeg

Copy and rename a file using Mac Terminal:

cp /path/to/old_filename /path/to/new_filename

If you want to copy a file named 20230518.jpg on your desktop to the Downloads folder and rename it as 2023-05-18.jpeg, type cp into Terminal followed by a space, drag and drop the file into Terminal, then drag and drop the folder where you want to copy the file to and enter the new name. The final command should be: cp /Users/user_name/Desktop/20230518.jpeg /Users/user_name/Downloads/2023-05-18.jpeg

Copy and rename a file in Mac Terminal

 Tips: If you only need to rename a single file on Mac, it may be easier to use the rename shortcut. Just select the file and press the Enter key to enter renaming mode. Then change the name and press the Enter key again.

Share these command lines to help others rename a file/folder/extension in the macOS Terminal app.

 

How to rename multiple files in Mac Terminal?

If you want to batch rename files in Mac Terminal, you need to run a loop through the file you want to rename. Depending on how you wish to rename the file, the command line will vary. We'll list some of the common command lines you can use to rename files in bulk through Terminal.

Here's something you need to know before getting started.

When you open Terminal, it usually starts in your home directory, represented by the (~) tilde symbol. If you execute the following command at this time, it will take effect on all the files in your home directory.

Therefore, if you just need to rename all files in a specific folder, you need to specify the directory using the cd command. For instance, if you want to change to the folder named "test folder" on your desktop, run the following command before executing the renaming command.

cd ~/Desktop/test\ folder

Besides, If you want to rename only a specific type of file, you can add its extension after the wildcard ('*'), such as f"or fin * .jpg; do"

Here's how to rename files in Terminal using regular expressions:

Rename file extension:

for file in *; do 
  mv "$file" "${file%.jpg}.png" 
done

Rename file extension in Mac Terminal

This example changes files with the .jpg extension to .png.

Add a suffix to file names:

for file in *; do 
  mv "$file" "${file%.png}_new.png"
done

How to rename multiple files in Mac Terminal with suffix

This example adds the suffix "_new" to all png files.

Add a prefix to file names:

for file in *; do 
  mv "$file" "old_$file" 
done

How to rename multiple files in Mac Terminal with prefix

This example adds the prefix "_old" to all the file names.

Rename files using Date Created:

for f in *; do 
   creation_date=$(stat -f "%SB" -t "%Y%m%d%H%M%S" "$f") 
   new_name="${creation_date}_${f}" 
   mv "$f" "$new_name" 
done

How to rename files via Terminal using date created on Mac

This example renames files with the date it was created. For example, if there is a file named "image.png" that was created on January 1, 2023, at 12:00 AM, it will be renamed to "202301010000_image.png". If you don't need hour, minute, and second, delete "%H%M%S." If you want to add a hyphen between year, month, and date, change "%Y%m%d%" to "%Y-%m-%d"

Lowercase file name:

for f in *; do 
   new_name=$(echo "$f" | tr '[:upper:]' '[:lower:]') 
   mv "$f" "$new_name" 
done

How to rename all files in lowercase

This example changes your file name to lowercase.

Replace space with hyphen:

for file in *; do 
 mv "$file" "${file// /-}" 
done

How to rename all files and replace the space with hyphen

This example renames all files and replaces spaces with hyphens in Terminal.

Now that you know how to rename all files in a folder via Terminal, don't forget to share it with more people.