谷动谷力

 找回密码
 立即注册
查看: 477|回复: 0
打印 上一主题 下一主题
收起左侧

【Shell】linux shell脚本复制代码

[复制链接]
跳转到指定楼层
楼主
发表于 2024-1-5 11:24:22 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式

【Shell】linux shell脚本复制代码


要在Linux Shell中编写一个脚本来复制文件或目录,可以使用cp命令。下面是一个示例的Shell脚本代码:

  1. #!/bin/bash

  2. source_file="/path/to/source"   # 源文件路径
  3. destination_dir="/path/to/destination/"    # 目标目录路径

  4. if [ -f "$source_file" ]; then     # 如果源文件存在且为常规文件
  5.     cp "$source_file" "$destination_dir"      # 将源文件复制到目标目录
  6. elif [ -d "$source_file" ]; then       # 如果源文件存在且为目录
  7.     cp -r "$source_file" "$destination_dir"    # 递归地将源目录及其内容复制到目标目录
  8. else
  9.     echo "无效的源文件或目录!"        # 输出错误信息
  10. fi
复制代码


注意事项:

source_file变量应该设置为要复制的源文件或目录的完整路径。

destination_dir变量应该设置为要将文件或目录复制到的目标目录的完整路径。

-r选项表示进行递归复制(当源文件是目录时)。

+10
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|Archiver|手机版|深圳市光明谷科技有限公司|光明谷商城|Sunshine Silicon Corpporation ( 粤ICP备14060730号|Sitemap

GMT+8, 2024-4-29 18:37 , Processed in 0.069537 second(s), 38 queries .

Powered by Discuz! X3.2 Licensed

© 2001-2013 Comsenz Inc.

快速回复 返回顶部 返回列表