linux批量拷贝特定文件,Linux Shell编程学习笔记——目...-linux shell批量拷贝文件-第二部分 Linux Shell高级编程技巧——第四章 几个脚本例子——终结篇_169I...

笔记

#几个脚本例子

#kill_process.sh

#编辑

[root@localhost 0421]# vi kill_processes.sh

#查看内容

[root@localhost 0421]# cat kill_processes.sh

#!/bin/bash

#kill_process.sh

current_PID=$$

#获得特定进程的进程号并重定向到一个临时文件中

ps -aux|grep "/usr/sbin/httpd"|grep -v "grep"|awk '{print $2}'>/tmp/${current_PID}.txt

#命令块开始

for pid in `cat /tmp/${current_PID}.txt`

do

{

echo "Kill -9 $pid"

kill -9 $pid

}

done

#命令块结束

#删除临时文件

#echo "rm -f /tmp/${current_PID}.txt"

rm -f /tmp/${current_PID}.txt

#cpdir.sh

[root@localhost 0421]# vi cpdir.sh

[root@localhost shell]# cat cpdir.sh

#!/bin/bash

#cpdir.sh

#此脚本用于将源目录下的子目录全部复制到目的目录中,不复制源目录中的文件。确保目的目录中的子目录是空目录

#脚本用法函数

usage()

{

echo "cpdir.sh 源目录 目的目录"

}

#判断是否为两个参数,否则提示脚本用法

if [ $# -ne 2 ]

then

{

usage

exit 0

}

fi

srcdir=$1

desdir=$2

#判断源目录${srcdir}是否为目录,否则提示错误信息和用法

if [ ! -d $srcdir ]

then

{

usage

echo "错误:源目录${srcdir}不是目录"

exit

}

fi

#判断目的目录${descdir}是否为目录,否则提示错误信息和用法

if [ ! -d $desdir ]

then

{

usage

echo "错误:目的目录${desdir}不是目录"

}

fi

processid=$$;

#查找源目录下所有的子目录,输出并保存到/tmp/srcdir_进程号.txt文件中

echo "源目录下${srcdir}所有的子目录"

echo "-----------------------------"

find $srcdir/* -type d|/usr/bin/tee /tmp/srcdir_tmp_${processid}.txt

sed "s/^${srcdir}/${desdir}/g" /tmp/srcdir_tmp_${processid}.txt >/tmp/srcdir_${processid}.txt

#在目的目录下建立空子目录

rm -rf ${desdir}/*

for subdir in `cat /tmp/srcdir_${processid}.txt`

do

{

mkdir ${subdir}

}

done

echo ""

echo "目标目录下${desdir}所有的子目录"

echo "------------------------------"

find $desdir/* -type d|/usr/bin/tee /tmp/desdir_${processid}.txt

#比较在目的目录下建立空子目录后的差异

echo ""

echo "比较目的目录和源目录的差异"

echo "-------------------------"

diff /tmp/desdir_${processid}.txt /tmp/srcdir_${processid}.txt

rm -f /tmp/srcdir_${processid}.txt

rm -f /tmp/desdir_${processid}.txt

rm -f /tmp/srcdir_tmp_${processid}.txt

[root@localhost shell]# ls -al test/

total 20

drwxr-xr-x 4 root root 4096 Apr 21 17:33 .

drwxr-xr-x 17 root root 4096 Apr 21 17:33 ..

drwxr-xr-x 2 root root 4096 Apr 21 17:33 aa

drwxr-xr-x 2 root root 4096 Apr 21 17:33 cc

-rwxr-xr-x 1 root root 391 Apr 21 17:32 test.sh

[root@localhost shell]# ls -al test/aa/bb/

total 8

drwxr-xr-x 2 root root 4096 Apr 21 17:34 .

drwxr-xr-x 3 root root 4096 Apr 21 17:34 ..

[root@localhost shell]# find test/* -type d

test/aa

test/aa/bb

test/cc

test/cc/ss

[root@localhost shell]# find test1/* -type d

find: test1/*: No such file or directory

[root@localhost shell]# ./cpdir.sh test test1

cpdir.sh 源目录 目的目录

错误:目的目录test1不是目录

源目录下test所有的子目录

-----------------------------

./cpdir.sh: line 43: usr/bin/tee: No such file or directory

./cpdir.sh: line 44: tmp/srcdir_5075.txt: No such file or directory

cat: /tmp/srcdir_5075.txt: No such file or directory

目标目录下test1所有的子目录

------------------------------

find: test1/*: No such file or directory

./cpdir.sh: line 57: usr/bin/tee: No such file or directory

比较目的目录和源目录的差异

-------------------------

diff: /tmp/desdir_5075.txt: No such file or directory

diff: /tmp/srcdir_.txt: No such file or directory

[root@localhost shell]# ./cpdir.sh test test1

源目录下test所有的子目录

-----------------------------

test/aa

test/aa/bb

test/cc

test/cc/ss

目标目录下test1所有的子目录

------------------------------

test1/aa

test1/aa/bb

test1/cc

test1/cc/ss

比较目的目录和源目录的差异

-------------------------

[root@localhost shell]# cd test

test test1

[root@localhost shell]# cd test

[root@localhost shell]# cd test/cc/ss/

[root@localhost test1]# cd ..

[root@localhost shell]# cd test

[root@localhost test]# ls

aa cc test.sh

[root@localhost test]# cd ../test1

[root@localhost test1]# ls

aa cc

[root@localhost test1]# cd cc

[root@localhost cc]# ls

ss

[root@localhost cc]# ls -al

total 12

drwxr-xr-x 3 root root 4096 Apr 21 17:48 .

drwxr-xr-x 4 root root 4096 Apr 21 17:48 ..

drwxr-xr-x 2 root root 4096 Apr 21 17:48 ss

#menu.sh

#创建文件夹

mkdir 0425

#进入

cd 0425

#拷贝

cp ../0421/cpdir.sh .

#改名

mv cpdir.sh menu.sh

#编辑

vi menu.sh

#拷贝

cp awkif.sh ../0425

#改名

mv awkif.sh functions

vi搜索内容:/要匹配的内容

#致歉,menu.sh内容过多,此处省略,若读者感兴趣可以到我的资源下载。

附图

@Wentasy 博文仅供参考,欢迎大家来访。如有错误之处,希望批评指正。原创博文如需转载请注明出处,谢谢 :) [CSDN博客]

作者:Wentasy 发表于2013-4-25 20:48:02 原文链接

阅读:38 评论:0 查看评论

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值