LinuxQuestions.org
Latest LQ Deal: Linux Power User 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 10-01-2012, 02:00 AM   #1
abhinav4
Member
 
Registered: Jun 2010
Location: India
Distribution: Fedora/Cent OS
Posts: 123

Rep: Reputation: 0
Exclamation How to copy a file when destination directory does not exists.


Can I copy a file when destination directory does not exist. In below example i want to copy index.html to /home/www/example2 wherw example2 does not exists.

Quote:
cp /home/www/example/index.html /home/www/example2/
 
Old 10-01-2012, 02:13 AM   #2
pan64
LQ Guru
 
Registered: Mar 2012
Location: Hungary
Distribution: debian i686 (solaris)
Posts: 8,121

Rep: Reputation: 2270Reputation: 2270Reputation: 2270Reputation: 2270Reputation: 2270Reputation: 2270Reputation: 2270Reputation: 2270Reputation: 2270Reputation: 2270Reputation: 2270
you can test it easily (no, it won't do it)
and also you can imagine, if example2 did not exist cp cannot decide: is this the target dir or target filename.
 
Old 10-01-2012, 02:15 AM   #3
nugat
Member
 
Registered: Sep 2012
Posts: 122

Rep: Reputation: 31
Quote:
Originally Posted by abhinav4 View Post
Can I copy a file when destination directory does not exist. In below example i want to copy index.html to /home/www/example2 wherw example2 does not exists.
is there any reason you cannot use mkdir?

Code:
mkdir /home/www/example2
you can always use the install program, e.g.:

Code:
install -D /home/www/example/index.html /home/www/example2/index.html
 
Old 10-01-2012, 02:17 AM   #4
hydraMax
Member
 
Registered: Jul 2010
Location: Skynet
Distribution: Debian + Emacs
Posts: 467
Blog Entries: 60

Rep: Reputation: 51
I don't think the cp command can do that, unless I'm missing something in the manual page, though the cp command has some other interesting directory creating functionality (like the --parent command). I think you have to use mkdir -p along with the cp command, or you could create a single perl or python script to do this (which shouldn't be very difficult).
 
Old 10-01-2012, 02:56 AM   #5
abhinav4
Member
 
Registered: Jun 2010
Location: India
Distribution: Fedora/Cent OS
Posts: 123

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by pan64 View Post
is this the target dir or target filename.
example2 is the target directory name. I guess cp does not have the ability to create destination directory.
 
Old 10-01-2012, 03:09 AM   #6
pan64
LQ Guru
 
Registered: Mar 2012
Location: Hungary
Distribution: debian i686 (solaris)
Posts: 8,121

Rep: Reputation: 2270Reputation: 2270Reputation: 2270Reputation: 2270Reputation: 2270Reputation: 2270Reputation: 2270Reputation: 2270Reputation: 2270Reputation: 2270Reputation: 2270
Quote:
Originally Posted by abhinav4 View Post
example2 is the target directory name. I guess cp does not have the ability to create destination directory.
cp does not have the ability to decide if it should be a dir of file. That's why it won't create dir.
 
Old 10-01-2012, 11:59 AM   #7
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957
You could set up a short function like this to include directory creation:

Code:
cp2dir() { mkdir -p "${!#}" && cp "$@" ;}
This will run mkdir on the final argument, and then run cp if it succeeds in creating that directory.

Note too that it will also pass through all other arguments, so the traditional cp options can still be used. But beware that it only works with the traditional cp syntax where the targetdir is the final argument, so never use the "-t/-T" options with it.

Similar functions could, of course, be set up for mv or any other command, although I don't know if I'd personally trust it with anything that could potentially destroy the original.
 
  


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
Copy certain file types recursively while maintaining file structure on destination? rockf1bull Linux - Newbie 1 06-14-2011 10:28 AM
[SOLVED] Bash: No such file or directory, when file actually exists. SkyerSK Linux - Software 13 02-06-2011 05:33 AM
copy the file whose source path is mentioned in a file to the specified Destination bluepenguine Linux - General 1 06-03-2010 06:31 AM
cannot create directory even if file exists pratikvimal Linux - Newbie 7 06-15-2009 04:51 AM
no such file or directory exists when trying to execute a exe ryedawg Linux - Software 5 12-05-2005 06:42 AM


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