繁体   English   中英

如何编写将一个目录下的所有文件和目录复制到另一个目录的linux脚本?

[英]How to write a linux script that copies all the files and directories under one directory to another directory?

我尝试创建的脚本有点问题。

此脚本应采用两个参数,即源目录和目标目录,如果用户输入的参数少于 2 个,则应打印出错误消息并退出。 此外,此脚本应检查源目录是否存在,如果不存在,则应打印出错误消息并退出。 此外,脚本应检查目标目录是否存在,如果不存在,则应创建该目录。 最后,脚本应该将文件从源目录复制到目标目录。

到目前为止,这是我的尝试:

if (( $# < 2 ));
   echo "Error: Too few arguments supplied"
   exit 1
if [ -d "src_dir" ]
then
    echo "Directory src_dir exists."
else
    echo "Error: Directory src_dir does not exist."
fi

if [ -d "dst_dir" ]
then
    echo "Directory dst_dir exists."
else
    mkdir dst_dir
    cp -r src_dir/* dst_dir
fi

任何帮助将非常感激。 提前致谢!

检查参数的正确数量: 检查传递给 Bash 脚本的参数数量

if [ "$#" -ne 2 ]; then
    echo "Error: Too few arguments supplied"
    exit 1
fi

检查目录是否存在: 检查shell脚本中是否存在目录

if [ ! -d "$1" ]; then
    echo "Error: Directory $1 does not exist."
    exit 1
fi

用于在第二个参数上创建目录:如果文件夹不存在,如何使用 Bash 创建文件夹?

mkdir -p $2

最后,只需将它们全部复制:

cp -r $1/* $2/

暂无
暂无

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

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