#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2006
    Posts
    144
    Rep Power
    8

    Automating move/compress commands


    Hey everyone. I'm relatively new to Unix but know many of the commands from using Ubuntu. If I want to automatically move files into folders and compress others, how would I achieve this?

    Basically, some files get dropped into say /fred/ everyday. At the end of the day, I'd like to take all of those files and put it into a folder in say /archive/1 day ago/ and move all of the files from /archive/1 day ago/ to /archive/2 days ago/. Then the stuff in /archive/2 days ago/ would be compressed into say /archive/2011-09-08.zip and placed in /archive/.

    How would I accomplish this? Any code, code fractions, or pointers in where I can learn would be very appreciated.
    Cheers,
    Fred Liu
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Regular (2000 - 2499 posts)

    Join Date
    Mar 2006
    Posts
    2,436
    Rep Power
    1688
    First things first, you may have got the order in which you wish to do that wrong ...!
    Zip up oldest archive and remove those files
    Move day 1 stuff to day 2
    Move current to day 1

    Having spaces in the directory names may be an issue, so take care with those.
    At the relevent time (maybe via cron?) have the 'archive process' fire off:
    Code:
    arch_date=$(date "+%F")
    zip -q /archive/${arch_date} "/archive/2 days ago"
    zip_cc=$?
    if [ $zip_cc -ne 0 ]
    then
      echo "Zip error (code $zip_cc)"
      exit 1
    fi
    rm "/archive/2 days/ago/*"
    rm_cc=$?
    if [ $zip_cc -ne 0 ]
    then
      echo "rm error (code $rm_cc)"
      exit 1
    fi
    mv "/archive/1 day ago/*" "/archive/2 days/ago/"
    mv_cc=$?
    if [ $mv_cc -ne 0 ]
    then
      echo "mv error from 1 day ago (code $mv_cc)"
      exit 1
    fi
    
    mv "/fred/*" "/archive/1 day ago/"
    mv_cc=$?
    if [ $mv_cc -ne 0 ]
    then
      echo "mv error from fred (code $mv_cc)"
      exit 1
    fi
    Not tested and, as always, before implementing anything that deletes/moves stuff about be very sure you have a working backup and that the things being deleted and moved have been identified correctly!
    The moon on the one hand, the dawn on the other:
    The moon is my sister, the dawn is my brother.
    The moon on my left and the dawn on my right.
    My brother, good morning: my sister, good night.
    -- Hilaire Belloc

IMN logo majestic logo threadwatch logo seochat tools logo