返回

linux-将文件复制到目标,如果目标不存在,则创建目标

发布时间:2022-04-22 12:44:33 288
# 运维

我正在写一个bash脚本来复制不止一个全部的将文件发送到目标,如果目标不存在,则创建该目标。

我已经经历了这个解决方案,但它不能满足我的要求。

我的代码(因idk原因无效):

# copy files and make a directory if does not exist
mkcp() {
    # last argument is destination
    dir="${@: -1}"
    # create directory if does not exist
    mkdir -p "$dir"
    # loop over all arguments
    for file in "$@"
    do
        # if file is last argument (directory) then break loop
        if [ file == "$dir" ]; then
            break
        fi
        # else keep copying files
        cp -R "$file" "$dir"
    done
}

我希望所有这些命令都能正常工作:

# copies "text.txt" to "testdir" (testdir may or may not exist so it must be created first)
$ mkcp test.txt ~/desktop/testdir

# copies "test1.txt" and "test2.txt" to "testdir" (conditions are same)
$ mkcp test1.txt test2.txt ~/desktop/testdir

# copies "all files" to "testdir" (conditions are same)
$ mkcp * ~/desktop/testdir

如果有任何其他解决方案可以满足我的要求,我也同意。

注:该mkcp函数存储在.zshrc.

特别声明:以上内容(图片及文字)均为互联网收集或者用户上传发布,本站仅提供信息存储服务!如有侵权或有涉及法律问题请联系我们。
举报
评论区(0)
按点赞数排序
用户头像