LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This 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


Reply
  Search this Thread
Old 08-09-2014, 02:43 PM   #1
ness2616
LQ Newbie
 
Registered: May 2014
Posts: 13

Rep: Reputation: Disabled
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!
 
Old 08-09-2014, 03:07 PM   #2
Firerat
Senior Member
 
Registered: Oct 2008
Distribution: Debian Jessie / sid
Posts: 1,471

Rep: Reputation: 444Reputation: 444Reputation: 444Reputation: 444Reputation: 444
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 )
 
1 members found this post helpful.
Old 08-09-2014, 05:01 PM   #3
ness2616
LQ Newbie
 
Registered: May 2014
Posts: 13

Original Poster
Rep: Reputation: Disabled
Thank you!!
 
Old 08-09-2014, 05:23 PM   #4
Firerat
Senior Member
 
Registered: Oct 2008
Distribution: Debian Jessie / sid
Posts: 1,471

Rep: Reputation: 444Reputation: 444Reputation: 444Reputation: 444Reputation: 444
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"

Last edited by Firerat; 08-09-2014 at 05:29 PM.
 
1 members found this post helpful.
Old 08-09-2014, 07:03 PM   #5
10000010010010
LQ Newbie
 
Registered: Aug 2014
Posts: 4

Rep: Reputation: Disabled
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

Last edited by 10000010010010; 08-09-2014 at 07:20 PM.
 
1 members found this post helpful.
Old 08-09-2014, 08:24 PM   #6
jpollard
Senior Member
 
Registered: Dec 2012
Location: Washington DC area
Distribution: Fedora, CentOS, Slackware
Posts: 4,714

Rep: Reputation: 1280Reputation: 1280Reputation: 1280Reputation: 1280Reputation: 1280Reputation: 1280Reputation: 1280Reputation: 1280Reputation: 1280
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.
 
Old 08-09-2014, 08:58 PM   #7
10000010010010
LQ Newbie
 
Registered: Aug 2014
Posts: 4

Rep: Reputation: Disabled
Quote:
Originally Posted by jpollard View Post
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.
 
Old 08-16-2014, 06:32 PM   #8
ness2616
LQ Newbie
 
Registered: May 2014
Posts: 13

Original Poster
Rep: Reputation: Disabled
10000010010010, that was incredibly useful and really helped me think about scripting in a different way. Thanks so much.
 
Old 08-16-2014, 06:56 PM   #9
Firerat
Senior Member
 
Registered: Oct 2008
Distribution: Debian Jessie / sid
Posts: 1,471

Rep: Reputation: 444Reputation: 444Reputation: 444Reputation: 444Reputation: 444
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
 
1 members found this post helpful.
Old 08-16-2014, 10:38 PM   #10
jpollard
Senior Member
 
Registered: Dec 2012
Location: Washington DC area
Distribution: Fedora, CentOS, Slackware
Posts: 4,714

Rep: Reputation: 1280Reputation: 1280Reputation: 1280Reputation: 1280Reputation: 1280Reputation: 1280Reputation: 1280Reputation: 1280Reputation: 1280
Quote:
Originally Posted by 10000010010010 View Post
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.

Last edited by jpollard; 08-16-2014 at 10:39 PM.
 
Old 08-17-2014, 10:14 AM   #11
ness2616
LQ Newbie
 
Registered: May 2014
Posts: 13

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by Firerat View Post
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 View Post
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.
 
Old 08-17-2014, 10:31 AM   #12
jpollard
Senior Member
 
Registered: Dec 2012
Location: Washington DC area
Distribution: Fedora, CentOS, Slackware
Posts: 4,714

Rep: Reputation: 1280Reputation: 1280Reputation: 1280Reputation: 1280Reputation: 1280Reputation: 1280Reputation: 1280Reputation: 1280Reputation: 1280
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...
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
small bash script copy one file to list of directories on file lahirushanaka Linux - Newbie 10 06-14-2014 12:00 PM
Xsetup_0.orig, Xsession.orig? no original file glorsplitz Slackware 1 07-08-2013 09:51 AM
[SOLVED] HowTo: Find a file, rename and copy new file in one script! pietmomberg Linux - Newbie 17 06-02-2013 07:08 AM
[SOLVED] Script to copy paste text from one file to another (overwriting part of the file) freet Programming 4 04-19-2011 04:23 PM
file copy in script help needed pinga123 Linux - Newbie 5 04-10-2010 02:16 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 04:12 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration