Warm tip: This article is reproduced from serverfault.com, please click

How to replace all white spaces in file names in GCP Cloud Storage?

发布于 2020-11-28 18:23:25

I have a bucket on GCP cloud storage and I want to rename all files that have a white space in their filename to the name where the whitespace is removed by a hyphen (-).

I tried something like

gsutil ls gs://<bucket>/<folder_with_the_files_to_rename>/ | sed 's/ /-/g/gsutil mv & \1/'

Unfortunately, I cant get the sed command work to replace the whitespace and then use the gsutil mv command to actually rename the file.

Can someone help me?

Questioner
nikos
Viewed
0
Wiktor Stribiżew 2020-11-30 23:41:40

If you need to replace any space with - in some file names that you get with gsutil ls gs://<bucket>/<folder_with_the_files_to_rename>/, you can use

gsutil ls gs://<bucket>/<folder_with_the_files_to_rename>/ | \
  while read f; do
    gsutil -m mv "$f" "${f// /-}";
  done;

gsutil mv command moves/renames files, and ${f// /-} is a variable expansion syntax where each space is replaced with - (the // stands for all occurrences). Thus, you do not need sed here.