Reputation: 115
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?
Upvotes: 3
Views: 1832
Reputation: 627101
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.
Upvotes: 6