Search code examples
bashfor-loopls

bash, issue with for loop


I'm listing some files from an amazon s3 this way:

s3cmd ls s3://my-bucket/$(date +%Y%m%d -d "1 day ago")*

this command returns:

2015-07-20 23:51  10680004   s3://my-bucket/20150720-1437436434_ip.log.gz
2015-07-20 23:55   6180965   s3://my-bucket/20150720-1437436477_ip.log.gz

In order to loop on these files, my code is:

for file in $(s3cmd ls s3://my-bucket/$(date +%Y%m%d -d "1 day ago")*)
do echo ${file}
done

but the result is:

2015-07-20
23:51
10680004
s3://my-bucket/20150720-1437436434_ip.log.gz
2015-07-20
23:55
6180965
s3://my-bucket/20150720-1437436477_ip.log.gz

instead of the expected result:

s3://my-bucket/20150720-1437436434_ip.log.gz
s3://my-bucket/20150720-1437436477_ip.log.gz

How can i retrieve the expected result?


Solution

  • Try replacing

      $(s3cmd ls s3://my-bucket/$(date +%Y%m%d -d "1 day ago")*)
    

    with

     $(s3cmd ls s3://my-bucket/$(date +%Y%m%d -d "1 day ago")* | sed 's/^.* s3/s3/' )
    

    to replace the initial output by cutting off the date an size part.