使用 cp
将文件复制到可能存在或不存在的文件夹时,如果需要,我如何让 cp
创建文件夹?这是我尝试过的:
[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
最佳答案
扩展 Christian's answer ,唯一可靠的方法是结合 mkdir
和 cp
:
mkdir -p /foo/bar && cp myfile "$_"
顺便说一句,当您只需要在现有层次结构中创建单个目录时,rsync
可以在一个操作中完成。我非常喜欢 rsync
作为更通用的 cp
替代品,事实上:
rsync -a myfile /foo/bar/ # works if /foo exists but /foo/bar doesn't. bar is created.
https://stackoverflow.com/questions/947954/