Copying files using $filename


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Copying files using $filename
# 1  
Old 10-12-2011
Copying files using $filename

Hi,
I've a problem. Here is the code:

Code:
#!/bin/ksh
echo "enter a file name"
read a
cd /home/linux1/sam
if [ -e $a ]
then
echo "file exists"
cp $a $a_bkp
else
echo "file doesn't exist"
fi

when executed the o/p is:
Code:
enter a file name
contact
file exists
cp: missing destination file
Try `cp --help' for more information.

The copy command using $a is not working. Could anybody please guide in this regard?

Last edited by pludi; 10-12-2011 at 04:53 PM..
# 2  
Old 10-12-2011
make your code look like the following and it will work for you. You need to put "" around the the second "$a"_bkp. You might also want to do a cp -p to preserve ownership, permissions, etc.

Code:
#!/bin/ksh

dir=/home/linux1/sam

echo "enter a file name"
read a
cd $dir
if [ -e $a ]
then
echo "file exists"
cp -p $a $dir/"$a"_bkp
else
echo "file doesn't exist"
fi

This User Gave Thanks to linuxn00b For This Post:
# 3  
Old 10-12-2011
to make it more clear

Code:
cp $a $a_bkp

You dont have the $a_bkp variable -- do proper quoting according to your shell so that you get the $a value, and concatenate it with _bkp.
# 4  
Old 10-13-2011
Thanks linuxn00b. Itz working. And thanks to thegeek for the suggestion
# 5  
Old 10-13-2011
To customize ..
Code:
$ read a; [ -e "$a" ] && echo "File exists" && cp $a ${a}_bkp || echo "File not exists"

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Copying files

All, I need to grab and rename common files from several unique directory structures. For example, the directory structures looks like: /unique_dir/common/common/common/person_name_dir/common_file.txt There are over 90,000 of these text files that I'd like to put in a single directory as... (5 Replies)
Discussion started by: hburnswell
5 Replies

2. Solaris

While copying files from windows to Solaris5.8 with filezilla length of the filename changed

Hello I am copying a tar from windows7 to Solaris with filezilla i could see, some characters in the name of the file get deleted. I could see only substring of the filename eg: original filename :abcdefghijklmnopqrstu.h i can see like abcdefghijklm solution to this problem is highly... (2 Replies)
Discussion started by: Revathi R
2 Replies

3. Shell Programming and Scripting

Using bash to separate files files based on parts of a filename

Hey guys, Sorry for the basic question but I have a lot of files that I want to separate into groups based on filenames which I can then cat together. Eg I have: (a_b_c.txt) WB34_2_SLA8.txt WB34_1_SLA8.txt WB34_1_DB10.txt WB34_2_DB10.txt WB34_1_SLA8.txt WB34_2_SLA8.txt 77_1_SLA8.txt... (1 Reply)
Discussion started by: Breentax
1 Replies

4. UNIX for Dummies Questions & Answers

Copying files with spaces in the filename in a for loop

Hi all, I've been tangoing with this one for a couple of days now and I'm still not making any progress. Basically I'm trying to match three numbers in a string from a text file with matching numbers in a jpeg, and then copying the results to another folder. Data looks like this: Model:... (4 Replies)
Discussion started by: faceonline
4 Replies

5. Shell Programming and Scripting

Files copying - [ Listed files alone. ] - Shell script

Hi All, I am doing this for svn patch making. I got the list of files to make the patch. I have the list in a file with path of all the files. To Do From Directory : /myproject/MainDir To Directory : /myproject/data List of files need to copy is in the file: /myproject/filesList.txt ... (4 Replies)
Discussion started by: linuxadmin
4 Replies

6. Shell Programming and Scripting

Filename from splitting files to have the same filename of the original file with counter value

Hi all, I have a list of xml file. I need to split the files to a different files when see the <ko> tag. The list of filename are B20090908.1100-20090908.1200_CDMA=1,NO=2,SITE=3.xml B20090908.1200-20090908.1300_CDMA=1,NO=2,SITE=3.xml B20090908.1300-20090908.1400_CDMA=1,NO=2,SITE=3.xml ... (3 Replies)
Discussion started by: natalie23
3 Replies

7. UNIX for Advanced & Expert Users

copying of files by userB, dir & files owned by userA

I am userB and have a dir /temp1 This dir is owned by me. How do I recursively copy files from another users's dir userA? I need to preserve the original user who created files, original group information, original create date, mod date etc. I tried cp -pr /home/userA/* . ... (2 Replies)
Discussion started by: Hangman2
2 Replies

8. Shell Programming and Scripting

copying files with spaces in the filename

Hi I am having difficulty copying files from one dir to another due to a space in the names of the file with an extension .rtf There are a group of files and the command am using is cp `ls -rt /wlblive/home/whiops/ops/RTFs/*.rtf|head -20` /wlblive/home/jamshed Since the files are... (3 Replies)
Discussion started by: jamshedj
3 Replies

9. Solaris

Copying Files

Hi, I understand that to copy files across server, the feasible way will be using scp command. Am I right? What if the two servers are not connected to a network? If by using a cross cable to link up both the server, what will be the best (fastest) way to copy files across? scp as well? ... (3 Replies)
Discussion started by: user50210
3 Replies

10. Shell Programming and Scripting

copying files

hi I want to copy all files from the current directory and move to .archive file. Moreover,I want to add .bak to each file name, that will be copied. How can I do that? (4 Replies)
Discussion started by: tjay83
4 Replies
Login or Register to Ask a Question