my code

Neil Ludlow

Find files older than x and archive them (bash)
by Neil Ludlow

This does not work properly in all situations, e.g. if there is no downloads folder or there is a non-standard file structure.

Snippet options

Download: Download snippet as
Copy snippet: For this you need a free my code account.
Embed code : You will find the embed code for this snippet at the end of the page, if you want to embed it into a website or a blog!



# Make this dynamic to look at different directories.
dtstring=`date +%Y-%m-%d`
sudo mkdir -p "$pathtoarchive"

if [ "$hours" ]; then
    # To specify older than one day it is better to talk in hours because 
    # the days integer is just an integer so everything less than 2 days 
    # would be 1 day, so 1 day 23 hours and 59 minutes is not greater than
    # 1 day.
    # For this reason I am using mmin and using the hours in minutes.
elif [ "$mins" ]
    # The default is 72 hours
    # timeinmins=2

if [ "$pathtofolder" ]; then

    # Get the list of old files into a variable
    filelist=$( sudo find "$pathtofolder"/* -mmin +"$timeinmins" -type f -name "*.zip" -exec ls {} \;)

    # Write the list of old files to a txt file
    #echo "$filelist" > list-of-old-files-"$dtstring".txt
    echo "$filelist" > list-of-old-files.txt

    while read p; do
      # Get the name of the last directory
      IFS='/' read -r -a array <<< "$p"


      # This counts backwards to get the penultimate item in the array
      if [ "${array[-2]}" != "downloads" ] && [ "${array[-2]}" != "uploads" ]; then
        #echo "${array[-2]}"

        # Make the directory if it does not exist
        sudo mkdir -p "$pathtoarchive"/"${array[-2]}"

      elif [ "${array[-2]}" = "downloads" ] || [ "${array[-2]}" = "uploads" ] ; then
        # It is just a file inside the "sftp" folder therefore it just wants to go straight into the archives root

        # Make the directory if it does not exist
        sudo mkdir -p "$pathtoarchive"/"${array[-3]}"
        sudo mkdir -p "$pathtoarchive"/"${array[-3]}"/"${array[-2]}"


      sudo rsync -cru "$p" "$newfilelocation"
      echo "$newfilelocation"

    done <list-of-old-files.txt

    #ls "$pathtofolder" >> /var/www/prizereactor_global_neil/laravel/neil/output.txt
    #find "$pathtofolder"/* -mmin +"$timeinmins" -type f -name "*.txt" -exec cp --parents {} "$pathtoarchive" \; # Copies everything from /var/www/...
    #find "$pathtofolder"* -mmin +"$timeinmins" -exec cp {} "$pathtoarchive" \; # No directories at all
    #find "$pathtofolder" -type f -mmin +"$timeinmins" -exec bash -c 'dir="$2/$(sed "s,^$3,,;s,/[^/]\+$,," <<< "$1")"; mkdir -p "$dir"; cp -v "$1" "$dir"' _ {} "$pathtoarchive" "${pathtofolder}/folders/" \;
    #echo "$filelist" >> /var/www/prizereactor_global_neil/laravel/neil/lines.txt
    #rsync -cru "$pathtofolder"/ "$pathtoarchive" # Trailing slash is needed because we don't need the "neil" directory itself
    #output=$(find "$pathtofolder"/* -mmin +"$timeinmins" -type f -name "*.txt" -exec rm -vr {} \; | wc -l)


#echo "Files deleted: $output"
#echo "Minutes: $timeinmins"

Create a free my code account now.

my code is a free service, which allows you to save and manage code snippes of any kind and programming language. We provide many advantages for your daily work with code-snippets, also for your teamwork. Give it a try!

Find out more and register now

You can customize the height of iFrame-Codes as needed! You can find more infos in our API Reference for iframe Embeds.