I am working on a backup script (bash shell - Fedora - VMware) that should compare a file with last modified attribute and current date and should back it up if the last modified date is older than the last run of the script.

I have tried couple of things (awk,stat and grep) however i could not make something which enables the comparison and lets the script decide if the files should be backed up or not.

Please focus on 2 case statement.

#! /bin/sh
echo "welcome to backup pro"
echo -e  "\n 1:Full \n 2:Incremental"
echo "Enter your choice"
  read choice
echo "Enter the files to be backup"
  read filetype
echo "Enter the destination folder"
  read destination
  case $choice in 
      echo "The selected backup type is Full backup"
      echo "Backing up $filetype files.."
      sleep 1
      cp *.$filetype /home/uone/backup_folder
      tar -zcvf archive_backup_folder.tar.gz backup_folder/   2>full-error.log
      mv archive_backup_folder.tar.gz /home/uone/backup_folder; cd /home/uone/backup_folder ;     rm *.$filetype
      sleep 1
      tail full-error.log
      echo "The backup you chose is Increment"
      ls -l | awk 'END {print $9 "\t" $8}'
      b=data "%H%M%S"   
        if [ $a -lt  $b ]; 
          echo "This is under construction"
exit 0
  • indentation goes a long way....
    – Simply_Me
    Aug 10, 2014 at 6:00
  • can you describe the exact issue when running the script? Where does it break?
    – Simply_Me
    Aug 10, 2014 at 6:01
  • also, you dont use $destination.
    – Simply_Me
    Aug 10, 2014 at 6:03
  • I am not able to determine the last modified date of a specific file type. For example if the user chooses to backup only txt files the script should lookup the last modified date of all txt files (i guess possible using for loop and ls) and then the files which are older than the current date and time should be backed up. I agree this is very crude and needs a lot of refinement! My indent of using the destination was to allow user to place the backup on his preferred location..Will fix that too! As of now as i am missing the logic i am not sure the output would mean anything to you.
    – Atul
    Aug 10, 2014 at 6:08
  • 2
    The title asks one question, and the body asks two completely different ones. I think I've assembled what you actually want out of those, but editing the question to clarify would be helpful. Aug 10, 2014 at 6:23

1 Answer 1


While you can do this with a loop and stat if you really want, this is something that find is good at and can do in a single line. You can create a special timestamp file each time your script runs and then select all files newer/older than that file with:

find -name "*.$filetype" ! -newer timestamp.file

That will find all files in the directory tree that are named according to the pattern *.$filetype, and that are older (not ! newer) than the timestamp file, as you asked for. If you want newer files (which seems more useful), just take out the !.

Having found the files, you can ask find to do something with them using `-exec:

find -name "*.$filetype" -newer timestamp.file -exec cp '{}' "$destination" +

That will run cp with all the matched files as arguments, to be put into the directory named by $destination, copying all the matched files into that directory.

At the end of each run of your script, you can update the timestamp on your comparison file with touch:

touch timestamp.file

and that is the comparison point that will be used next time. If files might be modified while the script is running you'll want to be a bit more careful - update the timestamp file right at the start, but copy the old one with its attributes out of the way first using cp -p, and use that for comparison (or vice versa).

To compare with current time (as in the title), the -mtime test to find can be useful, but I think from the text that what you you really want is to get the changes between two executions of the script.

The tarring, user interaction, etc in your script could remain untouched. If you want only files in the current directory (not subdirectories), add -maxdepth 1.

  • Sounds like a plan Michael! I guess its more beneficial to use the timestamp file as an anchor and update it with every run. Thank you so much for taking time to understand the issue and providing a feasible resolution. Appreciate it!
    – Atul
    Aug 10, 2014 at 6:31

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.