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, 2019 in Linux Administration by Upasana
64 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, 2019 by Shubham
• 13,370 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, 2019 in Linux Administration by ajs3033
• 7,280 points
131 views
+1 vote
2 answers

if the File system is full how to troubleshoot ?

Check filesystem usage. df -h If /var is showing ...READ MORE

answered Nov 18, 2019 in Linux Administration by Sirajul
• 45,960 points
75 views
0 votes
1 answer

How to create a symlink in Linux?

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

answered Jan 31, 2019 in Linux Administration by Omkar
• 68,860 points
85 views
0 votes
1 answer

How to install a deb file?

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

answered Apr 1, 2019 in Linux Administration by Shaan
39 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,250 points
111 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, 2019 in Linux Administration by Shubham
• 13,370 points
85 views
0 votes
1 answer

How to extract file base name without path and extension in bash?

You don't actually have to call the ...READ MORE

answered May 31, 2019 in Linux Administration by Shubham
• 13,370 points
251 views