How to Rename a Folder in AWS S3

avatar

Borislav Hadzhiev

Last updated: Jul 25, 2022

banner

Photo from Unsplash

Table of Contents #

  1. How to Rename a Folder in AWS S3
  2. How to Rename an S3 Folder with the AWS CLI

How to Rename a Folder in AWS S3 #

To rename a folder in an S3 bucket, you have to:

  1. Open the AWS S3 console and click on your bucket's name
  2. In the Objects tab, optionally use the search input to find your folder
  3. Click on the checkbox next to your folder's name
  4. Click on the Actions button and select Move

aws console move

  1. In the Destination input field, enter the complete S3 URI pointing to the folder, e.g. s3://YOUR_BUCKET/YOUR_FOLDER/ and click on the Move button.

rename-folder-aws-console

The Move action creates a copy of the objects with an updated path and a new last-modified date. The original objects contained in the folder are then deleted.

The action applies to all the objects in the specified folder.

How to Rename an S3 Folder with the AWS CLI #

To rename an S3 folder with the AWS CLI, run the s3 mv command, passing in the complete S3 URI of the current folder's location and the S3 URI of the desired folder's location. To make the command apply to nested paths, set the --recursive parameter.

shell
aws s3 mv s3://YOUR_BUCKET/YOUR_FOLDER/ s3://YOUR_BUCKET/NEW_FOLDER/ --recursive

rename folder in bucket

We use the s3 mv command to move the contents of a directory recursively to a new directory, in other words to rename the directory.

The output from the command shows that the files stored in the bucket's my-folder-1 directory were moved to the destination directory of your-folder-1.

Can run the command in test mode to make sure the output is what you expect before running it in real mode.

By setting the --dryrun parameter we instruct the AWS CLI to print the command's output without actually running it.

shell
aws s3 mv s3://YOUR_BUCKET/YOUR_FOLDER/ s3://YOUR_BUCKET/NEW_FOLDER/ --recursive --dryrun

rename folder in bucket test mode

You can run the s3 ls command to verify the S3 Folder has been renamed.

shell
aws s3 ls s3://YOUR_BUCKET/NEW_FOLDER/ --recursive --human-readable --summarize

verify folder renamed

You can also use the s3 ls command to verify that the old folder does not contain any objects:

shell
aws s3 ls s3://YOUR_BUCKET/OLD_FOLDER/ --recursive --human-readable --summarize

verify old folder empty

The output from the s3 ls command shows that the old folder contains 1 object of 0 Bytes. That object is the s3 path - the "folder" itself.

If you want to delete the empty folder, run the s3 rm command. Let's first run it in test mode with the --dryrun parameter because it's a destructive operation.

shell
aws s3 rm s3://YOUR_BUCKET/OLD_FOLDER/ --dryrun

delete old folder test mode

If the output matches your expectations, run the s3 rm command in real mode by removing the --dryrun parameter.

Further Reading #

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.