Linux - NewbieThis Linux forum is for members that are new to Linux.
Just starting out and have a question?
If it is not in the man pages or the how-to's this is the place!
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
What is the best way to write a small bash script that will make a backup of a file giving it a .bak’ extension? I am getting a lot of examples but most of them are extremely long and I believe not really what I am looking for.
What is the best way to write a small bash script that will make a backup of a file giving it a .bak’ extension? I am getting a lot of examples but most of them are extremely long and I believe not really what I am looking for.
Here is an example from the xargs page( using find and xargs to to what you want:
Code:
$ find . -name "(file you want to change)" -print0 | xargs -0 -I {} mv {} ~/(what you want the file to be)
Basically you find the file your looking for with the find command and then piping the result into xargs to move it. If you want to copy change:
Code:
mv
to
Code:
cp
For actually making a bash script just Google it. There are many resources for creating bash scripts.
Also, if you want to have it automated based on time take a look at cron scripts. I think you can also have it backup whenever there is a change to the file but Im not sure how that would be done.
Distribution: K/Ubuntu 18.04-14.04, Scientific Linux 6.3-6.4, Android-x86, Pretty much all distros at one point...
Posts: 1,802
Rep:
I really don't understand the question... what can be that much harder than???;
Code:
cp filename.ext filename.ext.bak
You can use wildcards/regular expressions to make it generic...
Is the goal to make it automatic? In that case you just create a cron job...
Also, for backups, you have rsync and other utilities.
I really don't understand the question... what can be that much harder than???;
Code:
cp filename.ext filename.ext.bak
You can use wildcards/regular expressions to make it generic...
Is the goal to make it automatic? In that case you just create a cron job...
Also, for backups, you have rsync and other utilities.
Thats much better. Not sure why I even suggested the find command if you know exactly what file you want to backup.
#!/bin/bash
savepath="/home/jack" ## path where you want to save the backup file
echo -n "Enter full path of file: " ## path where file is residing
read filepath
echo -n "Enter filename to be backup: " ## filename to be backup
read file
cd $filepath
if [ -f $file ]; ## checking existance of file
then
cp -p $file $savepath/$file.`date +%Y%m%d` ## Save the file in date of backup format, i.e. file.20121020
cp -p $file $savepath/$file.bck ## Save the file in .bck format under the path $savepath
echo "Backup done for $file successfully"
else
echo "File $file not found"
fi
If you want regularly backup of the file, then better save it in date format, not in .bck format, else it will keep the file overriding. But there will be no such pb with date format.
What is the best way to write a small bash script that will make a backup of a file giving it a .bak’ extension? I am getting a lot of examples but most of them are extremely long and I believe not really what I am looking for.
case:foo mike$ ls -l
total 0
-rw-r--r-- 1 mike wheel 0 21 Oct 13:14 blah.txt
case:foo mike$ cp blah.txt{,.bak}
case:foo mike$ ls -l
total 0
-rw-r--r-- 1 mike wheel 0 21 Oct 13:14 blah.txt
-rw-r--r-- 1 mike wheel 0 21 Oct 13:14 blah.txt.bak
case:foo mike$
Code:
case:moo mike$ ls -l
total 0
-rw-r--r-- 1 mike wheel 0 21 Oct 13:16 four.txt
-rw-r--r-- 1 mike wheel 0 21 Oct 13:16 one.txt
-rw-r--r-- 1 mike wheel 0 21 Oct 13:16 three.txt
-rw-r--r-- 1 mike wheel 0 21 Oct 13:16 two.txt
case:moo mike$ for i in *;do cp ${i}{,.bak};done
case:moo mike$ ls -l
total 0
-rw-r--r-- 1 mike wheel 0 21 Oct 13:16 four.txt
-rw-r--r-- 1 mike wheel 0 21 Oct 13:16 four.txt.bak
-rw-r--r-- 1 mike wheel 0 21 Oct 13:16 one.txt
-rw-r--r-- 1 mike wheel 0 21 Oct 13:16 one.txt.bak
-rw-r--r-- 1 mike wheel 0 21 Oct 13:16 three.txt
-rw-r--r-- 1 mike wheel 0 21 Oct 13:16 three.txt.bak
-rw-r--r-- 1 mike wheel 0 21 Oct 13:16 two.txt
-rw-r--r-- 1 mike wheel 0 21 Oct 13:16 two.txt.bak
case:moo mike$
I don't think I really understand what you're asking either.
Come to think of it that second example of mine fails if there's spaces in the names of the files. And some people will keep creating files with spaces in the names
Code:
case:boo mike$ ls -l
total 0
-rw-r--r-- 1 mike wheel 0 21 Oct 15:53 four file.txt
-rw-r--r-- 1 mike wheel 0 21 Oct 15:53 one file.txt
-rw-r--r-- 1 mike wheel 0 21 Oct 15:53 three file.txt
-rw-r--r-- 1 mike wheel 0 21 Oct 15:53 two file.txt
case:boo mike$ for i in *;do cp "${i}"{,.bak};done
case:boo mike$ ls -l
total 0
-rw-r--r-- 1 mike wheel 0 21 Oct 15:53 four file.txt
-rw-r--r-- 1 mike wheel 0 21 Oct 15:53 four file.txt.bak
-rw-r--r-- 1 mike wheel 0 21 Oct 15:53 one file.txt
-rw-r--r-- 1 mike wheel 0 21 Oct 15:53 one file.txt.bak
-rw-r--r-- 1 mike wheel 0 21 Oct 15:53 three file.txt
-rw-r--r-- 1 mike wheel 0 21 Oct 15:53 three file.txt.bak
-rw-r--r-- 1 mike wheel 0 21 Oct 15:53 two file.txt
-rw-r--r-- 1 mike wheel 0 21 Oct 15:53 two file.txt.bak
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.