Score:0

Shell script to copy multiple files

gb flag

I'm trying to do a shell script that would copy a bunch of files but struggle with setting up a loop to read through the files:

aws s3 cp s3://noaa-bdp-pds/gdas.YYYYMMDD/00/atmos/hdas.t00z.sfcf000.nc s3://s3internal/raw/HDAS/hdas.YYYYMMDD_00Z.nc

Here YYYY, MM, DD are numbers that I need to loop through.

I need to loop through all years, all months, and then dates to save all files. Let me know if this can be done?

Thanks

waltinator avatar
it flag
Read `man seq`.
kr flag
add all available combinations of YYYYMMDD to an array and use a for-loop to run the copy command for each iteration?
Score:0
cn flag

You can achieve this using aws s3 sync with wildcard and --dryrun which produces the output:

$ aws s3 sync s3://noaa-bdp-pds . \
  --exclude "*" --include "gdas.*/00/atmos/hdas.t00z.sfcf000.nc"

(dryrun) download s3://noaa-bdp-pds/gdas.20210001/00/atmos/hdas.t00z.sfcf000.nc to noaa-bdp-pds /gdas.20210001/00/atmos/hdas.t00z.sfcf000.nc
 ...
 ...

Remember to use an empty directory, or it may interfere with the output.

Now, you can use this to construct a loop:

#!/bin/bash
for line in $( \
    aws s3 sync s3://noaa-bdp-pds . \
    --exclude "*" --include "gdas.*/00/atmos/hdas.t00z.sfcf000.nc" | awk '/s3:\/\//{print $3}' --dryrun \
); do
    [[ $line =~ .*/gdas.(.*)/00/.* ]] && \
    echo aws s3 cp ${BASH_REMATCH[0]} s3://s3internal/raw/HDAS/hdas.${BASH_REMATCH[1]}_00Z.nc
done

When you're satisfied with the result, remove echo to copy the files.

bac0n avatar
cn flag
related: [AWS S3 ls wildcard support](https://github.com/aws/aws-cli/issues/3784)
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.