以下是shell脚本中提到的代码
SOURCE="/my/folder"
DESTINATION="/my/destination"
cp -r "$SOURCE/subdir/"* "$DESTINATION/another_sub/"
代码给我一个错误
/my/folder: no such a file or directory
此代码有什么问题。我也直接在终端上检查了文件夹是否可以正常工作,但是使用Shell脚本却无法正常工作
答案 0 :(得分:2)
如何在Linux操作系统中将文件从一个源位置复制到另一个目标位置。
按照您的 源 的以下代码和命令,到 目标 位置。
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$
答案 1 :(得分:1)
/my/folder: no such a file or directory
此错误表明它无法从根/
中找到该文件夹。
尝试
SOURCE="./my/folder"
DESTINATION="./my/destination"
如果脚本在与文件夹相同的目录中运行。或使用
SOURCE="~/my/folder"
DESTINATION="~/my/destination"
如果文件夹位于用户的主文件夹之外。