简体   繁体   中英

AWS S3 cli download the latest 2 or more files onto directory

I want to download the latest 2 files from an existing bucket folder, can anyone advise me how do I go about implementing this in a shellscript?

I know that the below command list the top 2 latest file in a directory, but how do I go about using aws cp to download the latest 2 files to my directory?

aws s3 ls $BUCKET --recursive | sort | tail -n 2 | awk '{print $4}' 

Something like this ought to do it:

LIST=$(aws s3 ls $BUCKET --recursive | sort | tail -n 2 | awk '{print $4}')

for x in $LIST; do
    aws s3 cp s3://$BUCKET/$x .
done

Using tail here is unnecessary if you use a reverse sort and force awk to only process the first 2 records. You can also use awks built in system function to save looping.

aws s3 ls $BUCKET --recursive | sort -r | awk 'NR<=2 { system("aws s3 ls "$4"/$x .")

Note the importance of printing the command before using system to ensure that the command is constructed correctly before execution.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM