LinuxQuestions.org
Help answer threads with 0 replies.
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 12-15-2009, 07:51 PM   #1
da_bizkit
LQ Newbie
 
Registered: Dec 2009
Posts: 3

Rep: Reputation: 0
Copy Files help script.


Hello there. I have a school asignment to write some scripts, and so far i've done them, but i meet problem with 1 particular of them. To be honest i have no idea how to do that one... can somebody more experienced with linux help me ?

The idea is, that i have to write a script, that copies a file and makes backup file with unique extension and informing the user that there is backup made.

I am totally lost .. can you tell me how its done ? :/
 
Old 12-15-2009, 08:00 PM   #2
indienick
Senior Member
 
Registered: Dec 2005
Location: London, ON, Canada
Distribution: Arch, Ubuntu, Slackware, OpenBSD, FreeBSD
Posts: 1,853

Rep: Reputation: 65
Welcome to LQ - terse homework rules are as follows:
1. Provide what you've done so far.
1.1. Show us that you have, at least, made an attempt.
2. Don't expect us to do all of your work for you.

I'm not saying you are in conflict with point 2, it's still an item worth mentioning.

When you say "unique extension", what are you referring to? Do you mean a portion of the filename has to be unique? I ask this, simply because "extensions", while more so a leftover from the DOS world (and kept around for sake of compatibility with Windows systems), you usually do not want to use a "non-standard" file extension.

If a portion of the filename (and not the file extension) needs to be unique, then look into the date(1) command.
Code:
$ date +%m-%d-%Y
12-15-2009
 
Old 12-15-2009, 08:24 PM   #3
da_bizkit
LQ Newbie
 
Registered: Dec 2009
Posts: 3

Original Poster
Rep: Reputation: 0
Well it doesn't work like it has to but i will still post what i've done.

Code:
#!/bin/bash
echo -n "Enter the name of the file to copy : "
read src
echo -n "Enter target file name : "
read targ
 
if [ -f $targ ]
then
	echo "File $targ exist. cannot overwrite"
	exit 1
fi
cp $src $targ
 
Old 12-15-2009, 08:28 PM   #4
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
to make a backup, use the same cp command but give it a different name.
 
Old 12-15-2009, 08:31 PM   #5
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,418

Rep: Reputation: 2785Reputation: 2785Reputation: 2785Reputation: 2785Reputation: 2785Reputation: 2785Reputation: 2785Reputation: 2785Reputation: 2785Reputation: 2785Reputation: 2785
You might want to look at using the date for the extension as above. If you need more than one 'copy' of the same file per day, extend to using the time as well.
See these links:
http://rute.2038bug.com/index.html.gz
http://tldp.org/LDP/Bash-Beginners-G...tml/index.html
http://www.tldp.org/LDP/abs/html/
 
Old 12-15-2009, 09:03 PM   #6
da_bizkit
LQ Newbie
 
Registered: Dec 2009
Posts: 3

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by ghostdog74 View Post
to make a backup, use the same cp command but give it a different name.
well i know it has to be done with cp, but i have no idea how to make it.. :/ how to make it to add specific to the file nor how to announce that name to the user.
What if i do it like
"cp $src $targ $backup1
echo "File $backup1 created"

i know that this won't work but if it is something similar can someone point it out for me ?
 
Old 12-15-2009, 09:10 PM   #7
Tinkster
Moderator
 
Registered: Apr 2002
Location: earth
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928
Almost there ...
cp takes TWO file arguments in this case. What you want
to achieve is for $targ to be uniquely identified; see
chrism's post about date higher up in the thread
 
  


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
Creating a script to move or copy files into multiple directories below the files matthes138 Linux - Newbie 5 08-25-2009 05:57 PM
script to copy files scofiled83 Programming 9 07-06-2009 09:18 PM
Script to copy files defend Linux - Newbie 2 12-11-2008 11:56 AM
bash script to copy files thtr2k Programming 1 02-08-2007 01:03 AM
will this script copy only certain files? verbatim Programming 5 04-28-2005 10:28 AM

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

All times are GMT -5. The time now is 05:14 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
Open Source Consulting | Domain Registration