LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 02-01-2013, 06:39 AM   #1
ankitpandey
Member
 
Registered: Jan 2012
Location: Mumbai
Posts: 63

Rep: Reputation: Disabled
Copy files


Hello Team,

I am trying to copy file from source to target on basis of argument provided by user but it seems i am not trying correct. Please help on how to proceed.

Below is what i tried. I am sure its wrong.

Code:
#!/bin/sh

echo "Enter Source path : $1"

echo "Enter file Name : $2"

echo "Enter Destination path : $3"

cd $3
mv $2 $2_bkp

cp $1/$2 $3
Thanks
 
Old 02-01-2013, 06:43 AM   #2
acid_kewpie
Moderator
 
Registered: Jun 2001
Location: UK
Distribution: Gentoo, RHEL, Fedora, Centos
Posts: 43,414

Rep: Reputation: 1966Reputation: 1966Reputation: 1966Reputation: 1966Reputation: 1966Reputation: 1966Reputation: 1966Reputation: 1966Reputation: 1966Reputation: 1966Reputation: 1966
care to actually tell us what's not working? Error messages?
 
Old 02-01-2013, 06:45 AM   #3
ankitpandey
Member
 
Registered: Jan 2012
Location: Mumbai
Posts: 63

Original Poster
Rep: Reputation: Disabled
Below is the error message :

Quote:
Enter Source path :
Enter file Name :
Enter Destination path :
mv: Insufficient arguments (1)
Usage: mv [-f] [-i] f1 f2
mv [-f] [-i] f1 ... fn d1
mv [-f] [-i] d1 d2
cp: Insufficient arguments (1)
Usage: cp [-f] [-i] [-p] [-@] f1 f2
cp [-f] [-i] [-p] [-@] f1 ... fn d1
cp -r|-R [-H|-L|-P] [-f] [-i] [-p] [-@] d1 ... dn-1 dn
 
Old 02-01-2013, 06:49 AM   #4
acid_kewpie
Moderator
 
Registered: Jun 2001
Location: UK
Distribution: Gentoo, RHEL, Fedora, Centos
Posts: 43,414

Rep: Reputation: 1966Reputation: 1966Reputation: 1966Reputation: 1966Reputation: 1966Reputation: 1966Reputation: 1966Reputation: 1966Reputation: 1966Reputation: 1966Reputation: 1966
oh, right so... you expect it to get these values from where?? if you're not going to put them in as command line parameters then all you're doing is printing the values out in the echo command. You'd use a read command to get input from the user:

echo "Enter Source path :"

read source_path

echo $source_path


etc
 
1 members found this post helpful.
Old 02-01-2013, 07:24 AM   #5
jpollard
Senior Member
 
Registered: Dec 2012
Location: Washington DC area
Distribution: Fedora, CentOS, Slackware
Posts: 2,239

Rep: Reputation: 577Reputation: 577Reputation: 577Reputation: 577Reputation: 577Reputation: 577
It would also likely be very useful to validate the input - make sure the source file exists, and that the destination file doesn't exist, and verify that an overwrite is what was wanted.
 
Old 02-01-2013, 07:34 AM   #6
ankitpandey
Member
 
Registered: Jan 2012
Location: Mumbai
Posts: 63

Original Poster
Rep: Reputation: Disabled
Thanks acid_kewpie , that is what i was missing. Now i am able to copy it.

Hello jpollard, how can i confirm if overwrite happened or not ? Is there any build in command foer that or i need to compare time.
 
Old 02-01-2013, 08:33 AM   #7
jpollard
Senior Member
 
Registered: Dec 2012
Location: Washington DC area
Distribution: Fedora, CentOS, Slackware
Posts: 2,239

Rep: Reputation: 577Reputation: 577Reputation: 577Reputation: 577Reputation: 577Reputation: 577
Before doing it, look to see if the destination file exists...

The bash manpage has a number of functions capable of checking files listed.
 
Old 02-01-2013, 10:40 AM   #8
ankitpandey
Member
 
Registered: Jan 2012
Location: Mumbai
Posts: 63

Original Poster
Rep: Reputation: Disabled
Thanks all
 
  


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 04:57 PM
in copy files or ls files the command want to invert select some files how to?? hocheetiong Linux - Newbie 3 06-27-2008 06:32 AM
How to find files and copy the found files to the floppy in one command justmehere Linux - Newbie 11 05-04-2008 11:29 PM
Command to copy files/folders but skip bad/corrupt files?? leemoreau Linux - Newbie 2 04-02-2007 02:27 PM
Mepis, copy files to thumb drive, files deleted? vremenno Linux - Newbie 6 09-15-2006 11:21 PM


All times are GMT -5. The time now is 01:28 AM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration