簡體   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