LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 11-05-2015, 01:40 AM   #1
eulaersi
LQ Newbie
 
Registered: Jul 2007
Posts: 27

Rep: Reputation: 0
Select files on S3 since a certain date in bash


I would like to select all files on a S3 folder that have been created since a certain date. I can do that by:

Code:
    aws s3 ls --recursive s3://my-s3-folder/ | awk '$1 > "2015-11-03 15:46:37" {print $0}' | sort -n
I'm trying to get the same result in a Bash script by using

Code:
    function select_s3_files() {
      prev_run_date="2015-11-03 15:46:37"
      $AWS_BIN s3 ls --recursive $S3_FOLDER | awk ''$1' > "$prev_run_date" {print '$0'}' | sort -n
    }
But I got the following error:

Code:
    awk:  > $prev_run_date {print ./cp-s3-folder.sh}
    awk:  ^ syntax error
    [Errno 32] Broken pipe
 
Old 11-05-2015, 04:23 AM   #2
translator1111
Member
 
Registered: Jun 2010
Location: Slovakia
Distribution: Debian 8, Ubuntu 10.04 and 12.04; SLAX 6.0; ConnochaetOS 0.9.; LFS; Natty chip: VT1708S
Posts: 108
Blog Entries: 2

Rep: Reputation: 7
Quote:
aws s3 ls --recursive s3://my-s3-folder/ | awk '$1 > "2015-11-03 15:46:37" {print $0}' | sort -n
Quote:
$AWS_BIN s3 ls --recursive $S3_FOLDER | awk ''$1' > "$prev_run_date" {print '$0'}' | sort -n
Dear eulaersi
did you notice that the code is not the same?,
the quote
Code:
 '
is in a different place. Try this:
Code:
      $AWS_BIN s3 ls --recursive $S3_FOLDER | awk   '$1 > "$prev_run_date" {print '$0'}' | sort -n
Faithfully,
M.
 
Old 11-05-2015, 08:36 AM   #3
eulaersi
LQ Newbie
 
Registered: Jul 2007
Posts: 27

Original Poster
Rep: Reputation: 0
Your modification doesn't throw any error, but it doesn't filter the output files. It shows all files and not only the files that have been modified since that date.

When I echo the command

Code:
echo "$AWS_BIN s3 ls --recursive $S3_FOLDER | awk '$1 > "$prev_run_date" {print '$4'}' | sort -n"
I get

Code:
/usr/local/bin/aws s3 ls --recursive s3://my-folder/archive/ | awk ' > 2015-11-03 15:46:37 {print ''}' | sort -n
The $1 and $4 are disappeared in the echo.
 
Old 11-05-2015, 08:53 AM   #4
allend
LQ 5k Club
 
Registered: Oct 2003
Location: Melbourne
Distribution: Slackware64-15.0
Posts: 6,377

Rep: Reputation: 2757Reputation: 2757Reputation: 2757Reputation: 2757Reputation: 2757Reputation: 2757Reputation: 2757Reputation: 2757Reputation: 2757Reputation: 2757Reputation: 2757
I like using find in conjunction with a temporary file to do this.
Code:
touch -t 201511031546.37 /tmp/last
find $S3_FOLDER -newer /tmp/last
rm /tmp/last
 
Old 11-05-2015, 09:28 AM   #5
eulaersi
LQ Newbie
 
Registered: Jul 2007
Posts: 27

Original Poster
Rep: Reputation: 0
I cannot use find in aws S3. The find command is not supported.

It's working perfect in command line, but I'm struggling to get it to work in a bash script with the different $1 and $4 variables and all the different ' " quotes.
 
Old 11-05-2015, 04:23 PM   #6
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 21,141

Rep: Reputation: 4123Reputation: 4123Reputation: 4123Reputation: 4123Reputation: 4123Reputation: 4123Reputation: 4123Reputation: 4123Reputation: 4123Reputation: 4123Reputation: 4123
You can't (easily) use bash variables like that in awk - pass them in as assigned awk variables using "-v"
Code:
awk -v prev="$prev_run_date" '$1 > prev {print $4}'
Not that I think date checks using space separated fields like that will work as you want ...

Last edited by syg00; 11-05-2015 at 05:52 PM. Reason: extranious single quote
 
Old 11-05-2015, 05:21 PM   #7
Habitual
LQ Veteran
 
Registered: Jan 2011
Location: Abingdon, VA
Distribution: Catalina
Posts: 9,374
Blog Entries: 37

Rep: Reputation: Disabled
you could mount the bucket using sshfs and then use find on that mount.
 
Old 11-06-2015, 08:41 AM   #8
eulaersi
LQ Newbie
 
Registered: Jul 2007
Posts: 27

Original Poster
Rep: Reputation: 0
[SOLVED]
Thanks for the replies. I've solved it by

Code:
function select_s3_files() {
  prev_run_date="2015-11-05 23:20:34"
  echo "Previous run date: $prev_run_date"
  $AWS_BIN s3 ls --recursive $S3_FOLDER | awk -v prev="$prev_run_date" '$0 > prev {print $0}' | sort -n
}
 
Old 11-06-2015, 10:12 AM   #9
Habitual
LQ Veteran
 
Registered: Jan 2011
Location: Abingdon, VA
Distribution: Catalina
Posts: 9,374
Blog Entries: 37

Rep: Reputation: Disabled
Good job and well done.
Glad it worked out!
 
Old 03-11-2016, 01:23 AM   #10
eulaersi
LQ Newbie
 
Registered: Jul 2007
Posts: 27

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by eulaersi View Post
[SOLVED]
Thanks for the replies. I've solved it by

Code:
function select_s3_files() {
  prev_run_date="2015-11-05 23:20:34"
  echo "Previous run date: $prev_run_date"
  $AWS_BIN s3 ls --recursive $S3_FOLDER | awk -v prev="$prev_run_date" '$0 > prev {print $0}' | sort -n
}
Apparently, this line doesn't work if there are spaces in the filenames. Can you help me editing this line of code?
 
Old 03-11-2016, 01:50 AM   #11
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 21,141

Rep: Reputation: 4123Reputation: 4123Reputation: 4123Reputation: 4123Reputation: 4123Reputation: 4123Reputation: 4123Reputation: 4123Reputation: 4123Reputation: 4123Reputation: 4123
Hmmm - I did warn about that some months ago.
 
  


Reply

Tags
awk, bash, s3



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Bash script help - Capture output of ls - move files according to date wayneywoo Programming 5 04-23-2013 08:45 AM
[SOLVED] How can I purge files logarithmically by modification date (BASH)? gunnarflax Programming 31 03-10-2013 02:45 PM
[SOLVED] Bash script to move files from date cbing Programming 9 04-20-2012 05:45 AM
bash script to delete files / folders based on date and freespace nekawa Linux - Newbie 5 06-08-2009 09:00 PM
bash sort files by date in file name thedude2010 Programming 6 05-12-2006 11:07 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 12:53 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration