Shell脚本实现从文件夹中递归复制文件
需求
前两天碰到需要在十层左右的文件夹中提取文件的需求,于是写了此脚本。
如下面这样的文件结构:
dir1 ├── a │ ├── b │ │ └── file1 │ └── file2 ├── c │ └── d │ ├── e │ │ └── file4 │ └── file3 └── file5
我们需要将其中的file1~file5提取出来放到另一个文件夹中。
脚本
脚本getfilefromdir.sh如下:
#!/bin/bash #desc: get file from directory #example: sh getfilefromdir.sh A BINIT_PATH=${1%/} SAVE_PATH=${2%/}
function checksavepath() { if [ -d $SAVE_PATH ] then rm -rf $SAVE_PATH fi
mkdir ${SAVE_PATH} touch $SAVE_PATH".log" }
function getfilefromdir(){ for file in ` ls $1` do if [ -d $1"/"$file ] then getfilefromdir $1"/"$file else local path="$1/$file" local name=$file if [ ! -f $SAVE_PATH"/"$name ] then echo "cp ${path} to ${SAVE_PATH}/${name}" cp ${path} "${SAVE_PATH}/${name}" else echo "${path} file already exists" echo "${path}" >> $SAVE_PATH".log" 2>&1 fi fi done }
checksavepath
for sfol in ${INIT_PATH} do getfilefromdir ${sfol} done
运行
sh getfilefromdir.sh dir1/ dir2
第一个参数是源文件夹,第二个是目地文件夹(不需要提前创建)。
如果有同名文件,会存在dir2.log中
结果为:
dir2 ├── file1 ├── file2 ├── file3 ├── file4 └── file5
Shell脚本美化登录界面装饰图(含农历)
今天同事闲得无聊,要我帮忙在linux登录页面里加点他认为很独特的东西,看了下他发的东西,对他表示很无语,下面来看看吧.脚本1:catclcal.sh#!/bin/bash#showChine
Shell脚本自动更新hosts实现免翻墙访问google
上次给大家发了一个python更新googlehosts的脚本,今天看到有人发出了一句用shell来获取googlehosts的脚本,我就拿来稍微简单加工了下,下面给大家shell版的更新
Shell脚本实现非法IP登陆自动报警
服务器的安全稳定是每个运维都希望达到的目标,毕竟网站一旦流量大了,访问高了,就会有一些无聊人来攻击,帮忙检测漏洞是好,但纯ddos的性质就很恶劣
标签:脚本,文件,给大家,里加,无聊