You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Google Cloud Storage中批量去除多个文件的扩展名?

Batch Remove .jpg Extensions from Google Cloud Storage Files

Hey there! I totally get what you're trying to do—strip the .jpg suffix from those thumbnail files in your GCS bucket, and it's frustrating that gsutil doesn't have a direct batch rename command. Let's fix that with a simple shell script that combines gsutil with basic bash string manipulation.

Here's a step-by-step solution:

  1. Create a shell script (you can name it rename_thumbnails.sh) with the following content:
#!/bin/bash
# Replace these with your actual bucket name and path prefix
BUCKET="bucketname"
PREFIX="thumbnails/"

# Loop through all .jpg files in the target GCS path
gsutil ls gs://${BUCKET}/${PREFIX}*.jpg | while read FILE_PATH; do
    # Remove the .jpg extension from the file path
    NEW_FILE_PATH="${FILE_PATH%.jpg}"
    # Perform the "rename" (in GCS, this is a move operation within the same bucket)
    gsutil mv "$FILE_PATH" "$NEW_FILE_PATH"
done
  1. Make the script executable:
chmod +x rename_thumbnails.sh
  1. Test first (optional but recommended!):
    Before running the actual rename, modify the script to print the commands instead of executing them—this lets you verify everything looks right:
#!/bin/bash
BUCKET="bucketname"
PREFIX="thumbnails/"

gsutil ls gs://${BUCKET}/${PREFIX}*.jpg | while read FILE_PATH; do
    NEW_FILE_PATH="${FILE_PATH%.jpg}"
    echo "Would rename: $FILE_PATH -> $NEW_FILE_PATH"
done
  1. Run the script:
./rename_thumbnails.sh

Key notes:

  • In Google Cloud Storage, there's no "true" rename operation—renaming is just moving a file to the same bucket with a new name, which is exactly what gsutil mv does here.
  • If you have a huge number of files, add the -m flag to gsutil mv to enable parallel processing and speed things up:
    gsutil -m mv "$FILE_PATH" "$NEW_FILE_PATH"
    
  • Ensure the account running the script has write permissions on the target bucket (you can check this with gsutil iam get gs://bucketname).

内容的提问来源于stack exchange,提问作者Tabish Rizvi

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.25 03:43:03