Script to copy file to file.orig
Hi guys. I'm new to Linux and trying to make a simple alias or script to back up the original version of files before I start modifying them, since I will be doing this a lot.
Here is an example of what I end up typing frequently: Code:
cp /etc/ssh/sshd_config /etc/ssh/sshd_config.orig So I'm trying to work out how to make an alias or function to do: 1. copy file to file.orig 2. nano file Here is what I placed in .bashrc as a simple alias that I can use... but it's not working: Code:
alias orig='cp $1 $1.orig && nano $1' Code:
cp: missing destination file operand after `.orig' |
Try
Code:
cp "${1}" "${1}.orig" Code:
nano --help |
Thank you!!
|
No problem ..
It is a learning curve.. look at using vim instead of nano vim is a powerful text editor when used correctly Try "vimtutor" to get started Edit: for balance is is only fair I mention "emacs" , an editor of equal power, but not one I'm familiar with. There has been many a vi/vim Vs emacs war ;) Edit2: You can use your alias trick for nano,vim... Code:
alias nano="/usr/bin/nano -B" |
Hi ness2616
You can create a small shell script like so Code:
#!/bin/bash Code:
chmod +x myscript.sh alias bkupfile="~/myscript.sh $1" In this example the myscript.sh resides in the root home directory. You can change the values in red to whatever you want. Now execute the alias like so Code:
bkupfile somefile Optional You can create a bin directory in your home folder for your scripts and then execute any script anywhere without typing the absolute path of it. Quote:
Now, you can call your script like so Code:
myscript.sh somefile |
NOTE: The shell script above will work... but it will also destroy the original when you make two edits... even if it turns out you don't want one.
This is what code management systems are for. You create your original and get it to some working form, then add it to the CMS. Now check out the file... the CMS maintains the original. Make your changes (either one or more, edit multiple times if you want). Update the CMS with a new version. and repeat. If you screw up, you can still get any version the CMS contains. |
Quote:
I did a test of this script before I posted it. I executed this script on a test file called file.txt. The script created a copy of file.txt to file.txt.orig and nano opened file.txt and not file.txt.orig. I made a few edits to file.txt and saved the changes and there were no errors. To make sure, I opened file.txt.orig and it retained the original contents of file.txt. Only file.txt had the new changes. |
10000010010010, that was incredibly useful and really helped me think about scripting in a different way. Thanks so much.
|
Some links for bash scripting
http://mywiki.wooledge.org/BashGuide http://www.tldp.org/LDP/Bash-Beginners-Guide/html/ http://www.tldp.org/LDP/abs/html/ http://www.gnu.org/software/bash/manual/bashref.html also , try and resolve any script related posts here on LQ, good experience ;) I gained very much from such activity, not only my own research and testing but from the feedback I get from other, more experienced members |
Quote:
Edit the file again, then quit. Original file is now gone. This is what a change management system is for - it will preserve any changes, and allow you to recover from any version recorded. |
Quote:
Quote:
|
You could always just implement a file rotation, one way was just to add a ".n" where n is a number. You might want to decide that if you do use a number you might want to specify the number of digits in the number. That way you always get the file versions listed in order...
|
All times are GMT -5. The time now is 02:12 AM. |