脚本要求如下
(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
执行结果如下
执行结果: [[email protected] 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
转载请注明:LAOV博客 » linux系统中cp命令复制文件的一个脚本