繁体   English   中英

将文件一个位置复制到另一个位置

[英]Copying file one location to another

下面是shell脚本中提到的代码

SOURCE="/my/folder"
DESTINATION="/my/destination"

cp -r "$SOURCE/subdir/"* "$DESTINATION/another_sub/"

代码给我一个错误

/my/folder: no such a file or directory 

此代码有什么问题。 我也直接在终端上检查了文件夹是否可以正常工作,但是使用Shell脚本却无法正常工作

如何在Linux操作系统中将文件从一个源位置复制到另一个目标位置。

按照您的Source目标位置,遵循以下代码和命令。

ubuntu@staging-docker:~/copyfile$ ls
copy.sh  destination_dir  source_dir
ubuntu@staging-docker:~/copyfile$ tree
.
├── copy.sh
├── destination_dir
└── source_dir
    └── source_file.txt

2 directories, 2 files
ubuntu@staging-docker:~/copyfile$ cat copy.sh

#!/bin/bash
source_location='/home/ubuntu/copyfile/source_dir/source_file.txt'
Destination_location='/home/ubuntu/copyfile/destination_dir'
`cp -r $source_location $Destination_location`

ubuntu@staging-docker:~/copyfile$ sh copy.sh
ubuntu@staging-docker:~/copyfile$ tree
.
├── copy.sh
├── destination_dir
│   └── source_file.txt
└── source_dir
    └── source_file.txt

2 directories, 3 files
ubuntu@staging-docker:~/copyfile$

在此处输入图片说明

ubuntu@staging-docker:~/copyfile$ mkdir source_dir
ubuntu@staging-docker:~/copyfile$ mkdir destination_dir
ubuntu@staging-docker:~/copyfile$ ls
destination_dir  source_dir
ubuntu@staging-docker:~/copyfile$ cat > source_dir/source_file.txt
This is a source file
ubuntu@staging-docker:~/copyfile$ tree
.
├── destination_dir
└── source_dir
    └── source_file.txt

2 directories, 1 file
ubuntu@staging-docker:~/copyfile$ cp source_dir/source_file.txt destination_dir/.
ubuntu@staging-docker:~/copyfile$ tree
.
├── destination_dir
│   └── source_file.txt
└── source_dir
    └── source_file.txt

2 directories, 2 files
ubuntu@staging-docker:~/copyfile$

在此处输入图片说明

/my/folder: no such a file or directory 

此错误表明无法从root /找到该文件夹​​。

尝试

SOURCE="./my/folder"
DESTINATION="./my/destination"

如果脚本在与文件夹相同的目录中运行。 或使用

SOURCE="~/my/folder"
DESTINATION="~/my/destination"

如果文件夹位于用户的主文件夹之外。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM