LinuxQuestions.org
Latest LQ Deal: Complete CCNA, CCNP & Red Hat Certification Training Bundle
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 02-21-2010, 09:01 PM   #1
akareh
LQ Newbie
 
Registered: Feb 2010
Posts: 1

Rep: Reputation: 0
Copy and Renaming files


Hi there

I'm quite new to Linux and bash scripting, and I've hit a roadblock in a script I'm writing. At the moment I have it so that a user can enter a file path and the name of an editor, like pico, and it will automatically bring up their file for them to edit. I am trying to figure out how to add code to this so that the file will be copied before any edits and saved as a separate file from the main file. I don't know how to copy the contents of a file, or a file before it's been edited, and I don't know how to write code to automatically generate a name for the previous versions of the file.

Below is the code that I have written so far:
Code:
echo "Please Enter a Text Editor: "
read TEXT_EDITOR
echo "Please Enter a Filepath: "
read FILEPATH
echo ""
if [ -e $FILEPATH ]
  then
    $TEXT_EDITOR $FILEPATH
else
    echo "That is not a valid filepath or editor."
fi
Basically, I am aiming to have a directory with the main version of a file, eg, Story.txt, alongside the previous versions of it, eg Story_a.txt, Story_b.txt, etc.

Any help would be greatly appreciated
 
Old 02-21-2010, 10:18 PM   #2
evo2
LQ Guru
 
Registered: Jan 2009
Location: Japan
Distribution: Mostly Debian and Scientific Linux
Posts: 5,753

Rep: Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288
How about:
Code:
#!/bin/bash
echo "Please Enter a Text Editor: "
read TEXT_EDITOR
echo "Please Enter a Filepath: "
read FILEPATH
echo ""
if [ -e $FILEPATH ]
  then
    NEWFILE="${FILEPATH%*.[^.]}_a${FILEPATH/*./}"
    cp "$FILEPATH" "$NEWFILE"
    $TEXT_EDITOR $NEWFILE
else
    echo "That is not a valid filepath or editor."
fi
Generating the name of the new file could be done a bit more simply, but this should work even in the case where the file name contains multiple '.'s. This script does *not* check that the '_a' version of the file already exists.

Evo2.
 
  


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
Renaming files tekmann33 Linux - Newbie 6 05-22-2009 11:08 AM
Renaming files mashcaster Linux - General 3 05-08-2009 04:14 PM
Renaming files leupi Linux - General 5 09-16-2008 04:22 AM
renaming files starwarsfan982 Linux - Software 7 10-30-2006 03:06 PM
Renaming files in one go saurya_s Linux - Software 1 01-12-2004 02:16 PM


All times are GMT -5. The time now is 01:34 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