欢迎光临
我们一直在努力

linux系统中cp命令复制文件的一个脚本

脚本要求如下

(1) 按顺序分别复制/var/log目录下的每个直接文件或子目录至/tmp/test1-testn目录中;
(2) 复制目录时,才使用cp -r命令;
(3) 复制文件时使用cp命令;
(4) 复制链接文件时使用cp -d命令;

(5) 余下的所有类型,使用cp -a命令;

#!/bin/bash
declare -i j=1
for i in /var/log/*;do
    [ ! -d /tmp/test$j ] && mkdir -p /tmp/test$j
    if [ -L $i ];then
        cp -d $i /tmp/test$j
    elif [ -f $i ];then
        cp $i /tmp/test$j
    elif [ -d $i ];then
        cp -r $i /tmp/test$j
    else
        cp -a $i /tmp/test$j
    fi
    let j++
done

执行结果如下

执行结果:
[root@www tmp]# tree
.
├── test1
│   └── anaconda.ifcfg.log
├── test10
│   └── btmp
├── test11
│   └── ConsoleKit
│       └── history
├── test12
│   └── cron
├── test13
│   └── dmesg
├── test14
│   └── dmesg.old
├── test15
│   └── dracut.log
├── test16
│   └── lastlog
├── test17
│   └── maillog
├── test18
│   └── messages
├── test19
│   └── mysqld.log
├── test2
│   └── anaconda.log
├── test20
│   └── secure
├── test21
│   └── spooler
├── test22
│   └── tallylog
├── test23
│   └── wtmp
├── test24
│   └── yum.log
├── test3
│   └── anaconda.program.log
├── test4
│   └── anaconda.storage.log
├── test5
│   └── anaconda.syslog
├── test6
│   └── anaconda.xlog
├── test7
│   └── anaconda.yum.log
├── test8
│   └── audit
│       └── audit.log
└── test9
    └── boot.log
赞(0) 打赏
未经允许不得转载:运维那些事 » linux系统中cp命令复制文件的一个脚本

评论 抢沙发

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址

觉得文章有用就打赏一下文章作者

非常感谢你的打赏,我们将继续给力更多优质内容,让我们一起创建更加美好的网络世界!

支付宝扫一扫打赏

微信扫一扫打赏