How to have the cp command create only if necessary folders while copying a file to a destination

0 votes

When copying a file using cp to a folder that may/may not exist, how do I make sure to create the folder if necessary? 

I tried this;

[root@file nutch-0.9]# cp -f urls-resume /nosuchdirectory/hi.txt cp: cannot create regular file `/nosuchdirectory/hi.txt': No such file or directory
Jul 19 in Linux Administration by Upasana
36 views

1 answer to this question.

0 votes
mkdir -p /foo/bar && cp myfile "$_" 

is indeed the only way to do this reliably.

answered Jul 19 by Shubham
• 13,300 points

Related Questions In Linux Administration

0 votes
1 answer

how to loop through the content of a file using bash

One of the ways to do it ...READ MORE

answered Feb 15 in Linux Administration by ajs3033
• 7,280 points
81 views
0 votes
1 answer

How to create a symlink in Linux?

To create a symbolic link, you can ...READ MORE

answered Jan 31 in Linux Administration by Omkar
• 67,620 points
68 views
0 votes
1 answer

How to install a deb file?

You can install a .deb file with ...READ MORE

answered Apr 1 in Linux Administration by Shaan
23 views
0 votes
1 answer

How to find the group associated with a user in linux?

To list all the groups groups or to list ...READ MORE

answered Jun 20 in Linux Administration by DareDev
• 6,810 points
38 views
0 votes
1 answer
0 votes
1 answer
0 votes
1 answer

How do I set variable if a specific package version is installed in CFEngine?

Here is what you can do.Just use packagesmatching to ...READ MORE

answered Jul 12, 2018 in Other DevOps Questions by Atul
• 10,240 points
87 views
+1 vote
3 answers
0 votes
1 answer

Writing a heredoc to a file in a script

try to use tee: tee newfile <<EOF line 1 line 2 line ...READ MORE

answered May 20 in Linux Administration by Shubham
• 13,300 points
50 views
0 votes
1 answer