LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Script to copy file to file.orig (https://www.linuxquestions.org/questions/linux-newbie-8/script-to-copy-file-to-file-orig-4175514090/)

ness2616 08-09-2014 01:43 PM

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
nano /etc/ssh/sshd_config

That's a lot of redundant typing!

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'
It gives me the error:
Code:

cp: missing destination file operand after `.orig'
Try `cp --help' for more information.

Any ideas why this isn't working? I think this is something fundamental about programming in Linux, so this will teach me a lot... thanks in advance!

Firerat 08-09-2014 02:07 PM

Try
Code:

cp "${1}" "${1}.orig"
But.. nano has a built in backup

Code:

nano --help
Option          GNU long option        Meaning -h, -?        --help                  Show this message
 +LINE,COLUMN                          Start at line LINE, column COLUMN
 -A            --smarthome            Enable smart home key
 -B            --backup                Save backups of existing files
 -C <dir>      --backupdir=<dir>      Directory for saving unique backup files
 -D            --boldtext              Use bold instead of reverse video text
 -E            --tabstospaces          Convert typed tabs to spaces
 -F            --multibuffer          Enable multiple file buffers
 -H            --historylog            Log & read search/replace string history
 -I            --ignorercfiles        Don't look at nanorc files
 -K            --rebindkeypad          Fix numeric keypad key confusion problem
 -L            --nonewlines            Don't add newlines to the ends of files
 -N            --noconvert            Don't convert files from DOS/Mac format
 -O            --morespace            Use one more line for editing
 -Q <str>      --quotestr=<str>        Quoting string
......
....
..

Most editors will have similar feature, including gui such as kate, gedit and so on (I seem to remember kate had it on by default, but long time since I used kate )

ness2616 08-09-2014 04:01 PM

Thank you!!

Firerat 08-09-2014 04:23 PM

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"

10000010010010 08-09-2014 06:03 PM

Hi ness2616

You can create a small shell script like so
Code:

#!/bin/bash

cp $1{,.orig} # this will backup your file with a .orig extension
nano $1      # nano will then open the file specify by $1

Give the script a name and give it execute permission.

Code:

chmod +x myscript.sh
Then add this alias to your .bashrc file in your home directory

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
somefile will be copied to somefile.orig and nano will then open your original file somefile for you to edit or whatever.


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:

export PATH=$PATH:$HOME/bin
Add this line above to your .bashrc file in your home directory

Now, you can call your script like so
Code:

myscript.sh somefile

jpollard 08-09-2014 07:24 PM

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.

10000010010010 08-09-2014 07:58 PM

Quote:

Originally Posted by jpollard (Post 5218194)
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.

Thanks for your feedback.

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.

ness2616 08-16-2014 05:32 PM

10000010010010, that was incredibly useful and really helped me think about scripting in a different way. Thanks so much.

Firerat 08-16-2014 05:56 PM

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

jpollard 08-16-2014 09:38 PM

Quote:

Originally Posted by 10000010010010 (Post 5218209)
Thanks for your feedback.

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.

Edit the file - and make changes. Exit. Things look fine.

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.

ness2616 08-17-2014 09:14 AM

Quote:

Originally Posted by Firerat (Post 5222198)
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

Ugh, why are you guys so awesome, isn't this supposed to be a frustrating thing to get into or something? ;)

Quote:

Originally Posted by jpollard (Post 5222265)
Edit the file - and make changes. Exit. Things look fine.

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.

Interesting. I haven't been able to recreate this yet (or perhaps notice :P), but it sounds like my next step will be to learn how to make it so that if <filename>.orig already exists, it will not be overwritten and the command will terminate early.

jpollard 08-17-2014 09:31 AM

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.