LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
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 09-02-2014, 04:15 PM   #1
megatron521
LQ Newbie
 
Registered: Aug 2013
Posts: 15

Rep: Reputation: Disabled
cp command help; cp: target is not a directory


Hi folks,

I am trying to run a copy command and getting this error.

The issue is I do not know if there is just one file/multiple files at /tst location, So I am using *

If it has only 1 instance it is copying properly to target location, but in this case there are multiple files in source loc.(/tst), so all the cp commands failing

Any idea how to fix this


Copy Command:
cp -r /tst/*/002/"79159503.pdf" /stg/MPN_1_28262/"datasheet_AA.pdf"

Error:
cp: target `/stg/MPN_1_28262/datasheet_AA.pdf' is not a directory


Source loc:
ls -latr /tst/*/002/"79159503.pdf"

/tst/001/002/"79159503.pdf"
/tst/003/002/"79159503.pdf"
 
Old 09-02-2014, 04:44 PM   #2
suicidaleggroll
LQ Guru
 
Registered: Nov 2010
Location: Colorado
Distribution: OpenSUSE, CentOS
Posts: 5,573

Rep: Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142
You're trying to move two files into one filename. Of course it can't do that, so what do you actually want it to do?
 
Old 09-02-2014, 04:53 PM   #3
megatron521
LQ Newbie
 
Registered: Aug 2013
Posts: 15

Original Poster
Rep: Reputation: Disabled
Lightbulb

I am trying to copy it to target location,

In my source vault, I have 10 dirs example:
/tst/001
/tst/002
..
..
/tst/010

we might not know if there is a pdf file one instance in one of the location OR same file in multiple locations. All that I need to do is copy from source to target

cp -r /tst/*/002/"79159503.pdf" /stg/MPN_1_28262/"datasheet_AA.pdf"

So I was using *, and it is failing

Any ideas for better command

Thanks in advance
 
Old 09-02-2014, 05:07 PM   #4
suicidaleggroll
LQ Guru
 
Registered: Nov 2010
Location: Colorado
Distribution: OpenSUSE, CentOS
Posts: 5,573

Rep: Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142
Once again, you're trying to move two files into one filename, which you can't do. So what do you actually want it to do?

You need to define what, in your mind, is the "correct" behavior. What do you want the end result to be? "I don't want it to throw an error" is not a valid answer, since what you're telling it to do is not allowable, it can't run without an error. You need to define your desired behavior before you or anybody else can possibly come up with a solution.

You can't move two files into one file, it won't work. Do you want to move one of them? Which one? Do you want to move both of them to different names? How do you want to name them? If you are ABSOLUTELY POSITIVE that the files are and always will be identical, then you can just grab the first one, but are you SURE this assumption will always hold? What if they're different? What do you want it to do?
 
1 members found this post helpful.
Old 09-02-2014, 06:05 PM   #5
megatron521
LQ Newbie
 
Registered: Aug 2013
Posts: 15

Original Poster
Rep: Reputation: Disabled
Thanks for the response

Yes, both the files are identical and we can grab the first occurence
 
Old 09-03-2014, 09:45 AM   #6
suicidaleggroll
LQ Guru
 
Registered: Nov 2010
Location: Colorado
Distribution: OpenSUSE, CentOS
Posts: 5,573

Rep: Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142
Then you can just do
Code:
file=$(ls /tst/*/002/79159503.pdf | head -1)
if [[ ! -z "$file" ]]; then
  cp "$file" /stg/MPN_1_28262/datasheet_AA.pdf
fi
 
Old 09-05-2014, 12:25 PM   #7
megatron521
LQ Newbie
 
Registered: Aug 2013
Posts: 15

Original Poster
Rep: Reputation: Disabled
Thank you, Will give a try
 
Old 09-23-2014, 01:22 PM   #8
mariatu20121
LQ Newbie
 
Registered: Sep 2014
Posts: 2

Rep: Reputation: Disabled
cp:target is not a directory

"You can't move two files into one file, it won't work. Do you want to move one of them? Which one? Do you want to move both of them to different names? How do you want to name them? If you are ABSOLUTELY POSITIVE that the files are and always will be identical, then you can just grab the first one, but are you SURE this assumption will always hold? What if they're different? What do you want it to do? "

If it is not sure that "the files are and always will be identical", is anyway to check it, if it is identical then copy one file (the first one), if not give some warning and option to copy source_file(s) to target_file_# (where # is 0, 1, 2 ...)

Thanks for your help,

Regards, Maria
 
Old 09-23-2014, 01:29 PM   #9
suicidaleggroll
LQ Guru
 
Registered: Nov 2010
Location: Colorado
Distribution: OpenSUSE, CentOS
Posts: 5,573

Rep: Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142
Sure, you can use the exit status of "diff -q file1 file2" to check if they're the same, and then decide accordingly.
 
Old 09-24-2014, 01:16 AM   #10
mariatu20121
LQ Newbie
 
Registered: Sep 2014
Posts: 2

Rep: Reputation: Disabled
thanks, but it is manual if using diff -q file1 file2 . Is anyway using script to copy file (with wildcard *) and inform us whether it is different and the script is continued to copy the file1 if same, or copy file1 to target_0 and copy file2 to target_1

Regards, Maria
 
Old 09-24-2014, 08:35 AM   #11
suicidaleggroll
LQ Guru
 
Registered: Nov 2010
Location: Colorado
Distribution: OpenSUSE, CentOS
Posts: 5,573

Rep: Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142
Sure, but you'll need to put in some effort of your own.
 
Old 09-26-2014, 01:21 PM   #12
Ramurd
Member
 
Registered: Mar 2009
Location: Rotterdam, the Netherlands
Distribution: Slackwarelinux
Posts: 703

Rep: Reputation: 111Reputation: 111
How would the command look if you didn't use wildcards and try to do exactly the same thing?

You'd get something like this:
Code:
cp -r /tst/001/002/"79159503.pdf" /tst/002/002/"79159503.pdf" /stg/MPN_1_28262/"datasheet_AA.pdf"
And then you immediately see the problem, if you provide more than one file as "source", the "destination" _must_ be a directory.
So if you want to copy all occurrences of said file to that location, find would be your friend in this:

Code:
find /tst -name "79159503.pdf" -exec cp -v {} /stg/MPN_1_28262/"datasheet_AA.pdf" \;
 
  


Reply



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
How do I find the target directory (not the target fs) of a mount -bind raananh Linux - Newbie 2 01-13-2013 09:18 AM
[SOLVED] mv: target `filename' is not a directory dai_bach Programming 9 12-20-2012 07:58 AM
cp: target \r' is not a directory --- URGENT thanks dnaqvi Linux - General 5 04-23-2010 03:18 PM
Specifying target directory for command in bash shell script? spectrescape Programming 1 07-22-2004 05:37 PM
samba + target directory robyso Linux - Networking 0 07-11-2003 05:07 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 12:59 PM.

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