I'm using a Google Drive command-line script that can return a list of files such as:

Id                             Title                       Size     Created
0Bxxxxxxxxxxxxxxxxxxxxxxxxxx   backup-2014-12-26.tar.bz2   569 MB   2014-12-26 18:23:32

I want to purge files older than 15 days.

How can I execute the following command:

drive delete --id 0Bxxxxxxxxxxxxxxxxxxxxxxxxxx

with the Id of all the lines that have a Created date older than 15 days?


You can apparently use the Google api to list and sort the files to your needs specifically (from drive --help):

    -m, --max      Max results
    -q, --query    Query (see https://developers.google.com/drive/search-parameters)

...and from the link...

  • Search for files modified after June 4th 2012
    modifiedDate > '2012-06-04T12:00:00'    // default time zone is UTC
    modifiedDate > '2012-06-04T12:00:00-08:00'

Note that the example searches for files newer than a certain date...

So this isn't very difficult at all, though, for whatever reason, drive seems capable of handling only a single argument per invocation:

mdate_list() { drive list -nq \
    "modifiedDate $1 '$(date -ud"$2" '+%FT%T')' and \
     mimeType != 'application/vnd.google-apps.folder'"
rmdrv() for dfile do drive delete -i "$dfile" || return; done

set -f; unset IFS            #no split data mangling, please
while   set -- $(mdate_list \< '15 days ago'|cut -d\  -f1)  
        [ "$#" -gt 0 ]
do      rmdrv "$@" || ! break

I only instituted the while loop at all in case you have too many drive files to handle in a single listing - most of the time you will easily do without it, but if there are a great many, this will keep populating the list until there are no more.

The rest just happens as a result of the data you feed it. Note that I specifically excluded folders here, but you will probably want to look at the link mentioned as well if there is anything else you might want to tweak.

  • Thanks for providing the answer to the question I should have asked in the first place :) – Benjamin Dec 29 '14 at 22:58
  • @Benjamin - thanks for pointing out drive to me - kinda peeved about that one file arg per invoke though... – mikeserv Dec 29 '14 at 23:02
  • @Benjamin - made the mdate...() fn take a couple parameters, and... well... it might actually work now. That's the sloppiest accept I've ever got. Anyway, thanks again, and good luck. – mikeserv Dec 29 '14 at 23:20

Very simple with awk

drive list |\
awk 'BEGIN{ "date +%s -d -15\\ days" | getline n }
        $6{ m=$5"\\ "$6
            ("date +%s -d "m) | getline t
            if(t < n) system("drive delete -id "$1) }' 
  • I get this output: awk: cmd. line:1: warning: escape sequence '\ ' treated as plain ' ' date: extra operand ‘days’ Try 'date --help' for more information. date: extra operand ‘18:23:32’ Try 'date --help' for more information. date: extra operand ‘17:49:11’ Try 'date --help' for more information. – Benjamin Dec 27 '14 at 9:55
  • @Benjamin Try with double "\" or without it at all. – Costas Dec 27 '14 at 11:37

Assuming the list of the files are in a file named x.txt, a bash script as follows could do the job:


for i in $(cat x.txt)
    fid=$(echo $i | awk '{print $1}') # extract the file id
    d=$(echo $i | awk '{print $5}') # extract the date filed
    d2=$(date -d "$d" +%s)  # convert it to epoch sec

    now=$(date +%s) # current time in epoch sec
    diff=$(( (now - d2) / 86400 )) # find diff and convert it to days
    echo "The file $fid is $diff days old"
    if (( $diff > 15 )) #if the diff is greater than 15 
      #delete file
      drive delete --id $fid
  • Thanks, I just had to replace \n with an actual newline between the quotes, or it would cut a line in the middle for an obscure reason! – Benjamin Dec 27 '14 at 10:31
  • I also noticed that [[ $diff > 15]] actually compares the numbers as strings (so '2' > '15'), I needed to replace this with (( $diff > 15 )). See comparing numbers in bash. – Benjamin Dec 29 '14 at 7:13
  • @Benjamin Thanks, I made an edit to my answer. – mkc Dec 29 '14 at 18:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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