#!/bin/bash #note no spaces around equals (=) sign base_dir='/a_directory_path_name_with_trailing_slash/' filename='report.txt' answer='n' #note lack of concatenation point report=$base_dir$filename if (test -f $report) then echo "############## ATTENTION ###################" echo "The report file exists and it will be overwritten!" echo "Would you like me to make a copy of it and delete the existing version?" read answer if [ "$answer" == y ] then #date with seconds in case more than one in a day date=`date +%Y%m%d_%s` #concatenate with a point in between backup="$filename.$date" cp $report $base_dir$backup echo "Created...$backup" rm -rf "$report" echo "Original removed" fi #continue else echo "Data will be appended to the existing report file!" exit fi #script ends
Never been to CodeSnippets before?
Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world (or not, you can keep them private!)
Make a file backup adding date and seconds to the filename and remove the original (See related posts)
You need to create an account or log in to post comments to this site.
Related Posts
» backup files in date shell bash backup script code
» Pretty PS1 Prompt for Bash in shell bash bashrc prompt
» Bash function to copy SSH DS... in ssh shell zsh bash
» How to flush the local DNS c... in lighttpd virtualhosting shell bash hosts dns cache
» Redirect all output to a sin... in shell bash logging
» Remove all but X most recent... in shell bash
Snippets (source code soon to be available) developed by Peter Cooper and powered by Ruby On Rails