1

I have a Jekyll blog with a directory structure that contains lots of hidden files and directories like .DS_Store, .idea and .git. It also has intermediate build artifacts and scripts that begin with _ like _deploy.sh and _drafts.

I want to write a script that uploads everything to a bucket on Google Cloud Storage, except these hidden files and underscored artifacts.

I've tried using the -x flag, but my expressions either exclude the whole current directory, and upload nothing, or fail to exclude some of the content that I want to exclude.

Here's what I have so far:

#!/bin/sh
gsutil -m rsync -rx '\..*|./[.].*$|_*' ./ gs://my-bucket.com/path

And the output I'm observing:

$  ./_deployblog.sh
Building synchronization state...
Starting synchronization

1 Answer 1

4

A series of really specific regular expressions solves the problem:

gsutil -m rsync -rdx '\..*|.*/\.[^/]*$|.*/\..*/.*$|_.*' . gs://my-bucket.com/path

Where the exclude pattern has 4 components separated by | characters.

\..*        <- excludes .files and .directories in the current directory
.*/\.[^/]*$ <- excludes .files in subdirectories
.*/\..*/.*$ <- excludes .directories in subdirectories
_.*         <- excludes _files and _directories
1
  • This can be shortened to -x '(^|/)(_|\.)': we pick files when there is a beginning of path or a / followed by either _ or .
    – Chupaka
    Commented Oct 18, 2021 at 15:44

Not the answer you're looking for? Browse other questions tagged or ask your own question.