linux - 如何让 cp 命令创建任何必要的文件夹以将文件复制到目标

使用 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 ,唯一可靠的方法是结合 mkdircp:

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/

相关文章:

linux - chmod 777 到一个文件夹和所有内容

linux - 在 Linux 系统上快速创建大文件

linux - 如果任何命令返回非零值,则中止 shell 脚本

python - 从第 2 行读取文件或跳过标题行

linux - 如何从 shell 将一个文件 append 到 Linux 中的另一个文件?

python - 我应该在 Python 中使用 "camel case"还是下划线?

linux - 使用 cURL 执行 HTTP 请求(使用 PROXY)

python - Python 和 IPython 有什么区别?

python - Python进程使用的总内存?

python - 如何获取方法参数名称?