Example: Unsync Files by Extension, Size, or Days Old

If you'd like to script unsynchronizing your files to free up disk space, you can use these scripts. Note that unsync is a premium feature, so you need to have an odrive Premium account to use it.

Usage: unsync_by [-e <extension>] [-s <size in kilobytes>] [-d <days>] [-p <directory path>] [-r]

Help: Unsync files by extension, size exceeded, or days old for a given directory

Options:
-e, -extension Unsyncs files with the specified extension
-s, -size Unsyncs files larger than the specified size in kilobytes
-d, -days Unsyncs files older than the specified day
-p, -dirpath The specified path
-r, -recursive Unsyncs files recursively through the specified path
-h, -help Help

These instructions were originally posted in this blog post which you can read for additional information.

NOTE: The Windows PowerShell version of the script will detect to make sure your odrive CLI binary is in the correct path (and if not, it will download it automatically). The bash version for Mac and Linux does not have this capability.

Param(
   [parameter()][alias("e")][string]$EXTENSION,
   [parameter()][alias("s")][string]$SIZE,
   [parameter()][alias("d")][string]$DAYS,
   [parameter()][alias("p")][string]$DIRPATH,
   [parameter()][alias("r")][switch]$RECURSIVE,
   [parameter()][alias("h")][switch]$HELP
)
$O="$HOME\.odrive\common"
$UNSYNC_BIN="$o\odrive.exe"

if ($RECURSIVE) {
   $PARAM = @{Recurse = $true}
}
else {
   $PARAM = @{Recurse = $false}
}

if ($HELP) {
   echo "Usage: unsync_by [-e <extension>] [-s <size in kilobytes>] [-d <days>] [-p <directory path>] [-r]"
   echo "Help: Unsync files by extension, size, or days old for a given directory"
   echo "Options:"
   echo "-e -extension Unsyncs files with the specified extension"
   echo "-s -size Unsyncs files larger than the specified size in kilobytes"
   echo "-d -days Unsyncs files older than the specified day"
   echo "-p -dirpath The specified path"
   echo "-r -recursive Unsyncs files recursively through the specified path"
   echo "-h -help Help"
   break
}

if(!(Test-Path -Path $UNSYNC_BIN)) {
    echo "Downloading CLI binary ... "
    (New-Object System.Net.WebClient).DownloadFile("https://dl.odrive.com/odrivecli-win", "$O\oc.zip")
    $shl=new-object -com shell.application
    $shl.namespace("$O").copyhere($shl.namespace("$O\oc.zip").items(),0x10)
    del "$O\oc.zip"
    echo "Done!"
}

if (-Not ($DIRPATH)){
   echo "Invalid arguments. Please consult help for usage details (-h, -help)." 
   break
}
elseif ($EXTENSION) {
   echo "unsyncing all files of $EXTENSION type in $DIRPATH"
   Get-ChildItem -File -Path "$DIRPATH\*" -Filter "*.$EXTENSION" @PARAM | % {& "$UNSYNC_BIN" unsync "$($_.FullName)"}
}
elseif ($SIZE) {
   echo "unsyncing all files larger than $SIZE kilobytes in $DIRPATH"
   Get-ChildItem -File -Path "$DIRPATH\*" -Exclude *.cloud* @PARAM | Where-Object {$($_.Length) -gt "$($SIZE)KB"} | % {& "$UNSYNC_BIN" unsync "$($_.FullName)"}
}
elseif ($DAYS) {
   echo "unsyncing all files older than $DAYS days in $DIRPATH"
   $SUBDATE = $((Get-Date).AddDays(-$($DAYS)))
   echo $SUBDATE
   Get-ChildItem -File -Path "$DIRPATH\*" -Exclude *.cloud* @PARAM | Where-Object {$($_.LastWriteTime) -lt $SUBDATE} | % {& "$UNSYNC_BIN" unsync "$($_.FullName)"}
}
else {
   echo "Invalid arguments. Please consult help for usage details (-h, -help)."
}
#!/bin/bash
set -e
ODRIVE_PY="$HOME/temp/odrive python/odrive.py"
RECURSIVE="-maxdepth 1"
if [[ $1 == "-h" ]] || [[ $1 == "--help" ]] ; then
        echo "Usage: unsync_by [-e <extension>] [-s <size in kilobytes>] [-d <days>] [directory path] [-r]"
        echo "Help: Unsync files by extension, size, or days old for a given directory"
        echo "Options:"
        echo "-e --extension  Unsyncs files with the specified extension"
        echo "-s --size       Unsyncs files larger than the specified size in kilobytes"
        echo "-d --days       Unsyncs files older than the specified day"
 echo "-r --recursive  Unsyncs files recursively through the specified path"
        echo "-h --help       Help"
elif [[ $# -gt 2 ]] ; then
    key="$1"
    case $key in
        -e|--extension)
        EXTENSION="$2"
        ;;
        -s|--size)
        SIZE="$2"
        ;;
        -d|--days)
        DAYS="$2"
        ;;
 *)
        echo "Invalid arguments. Please consult help for usage details (-h, --help)."
     exit 1
 ;;
     esac
else
     echo "Invalid arguments. Please consult help for usage details (-h, --help)."
     exit 1
fi
DIRPATH="$3"
if [[ $4 == "-r" ]] || [[ $4 == "--recursive" ]] ; then
     RECURSIVE=""
fi
if [[ $EXTENSION ]] ; then
     echo unsyncing all files of "$EXTENSION" type in "$DIRPATH"
     find "$DIRPATH" $RECURSIVE -name "*$EXTENSION" -type f -exec python "$ODRIVE_PY" unsync {} \;
elif [[ $SIZE ]] ; then
     echo unsyncing all files larger than "$SIZE" kilobytes in "$DIRPATH"
     find "$DIRPATH" $RECURSIVE -size "+${SIZE}k" -type f -exec python "$ODRIVE_PY" unsync {} \;
elif [[ $DAYS ]] ; then
     echo unsyncing all files older than "$DAYS" days in "$DIRPATH"
     find "$DIRPATH" $RECURSIVE -mtime "+${DAYS}" -type f -exec python "$ODRIVE_PY" unsync {} \;
fi

Learn how to configure your computer to run the script automatically.


Did this page help you?