How to Rename a Folder in AWS S3

avatar

Borislav Hadzhiev

Wed Sep 22 20213 min read

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 of 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 - your-folder-1.

Before executing the command you can run it in test mode to make sure that the output is what you expect.

By setting the --dryrun parameter we instruct the AWS CLI to print the commands 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

To verify the S3 Folder has been renamed, you can run the s3 ls command.

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, you can run the s3 rm command in real mode by removing the --dryrun parameter.

Further Reading #

Join my newsletter

I'll send you 1 email a week with links to all of the articles I've written that week

Buy Me A Coffee