Create a simple bash backup script of a file

This is the problem: Write a script that will make a backup of a file giving it a �.bak� extension & verify that it works. I have tried a number of different scripts that haven't worked and I haven't seen anything really concise and to the point via google. For brevity's sake this is one of the scripts I have tried:

tar -cZf /home/students/fall2012/crn12143/ford/field1.bak /home/field1

I saved the script as rename and ran it from the command line. I am getting errors. The home directory is ford. All I want to do is take a file called field1 in the home directory and make a backup copy of it in the same directory and call it field1.bak.

Valencia Community College. Orlando, FL USA. Prof. D. Weeks. COP-2341-12143.

If you just want to make a copy of file: /home/field1 , here is the syntax:-

cp /home/field1  /home/field1.bak

I know I can do what you posted via the command line. What I need to be able to do is have that same functionality in a script and execute that script from the command line.

  1. Create a script file: make_copy.sh:-
cat > make_copy.sh
file_name="/home/field1"
cp ${file_name} ${file_name}.bak
  1. Run the script file: make_copy.sh:-
chmod +x make_copy.sh
./make_copy.sh
1 Like

Thank you.