Chinaunix首页 | 论坛 | 博客
  • 博客访问: 806015
  • 博文数量: 244
  • 博客积分: 10000
  • 博客等级: 上将
  • 技术积分: 2420
  • 用 户 组: 普通用户
  • 注册时间: 2007-09-29 09:07
文章分类

全部博文(244)

文章存档

2011年(4)

2010年(3)

2009年(72)

2008年(119)

2007年(46)

我的朋友

分类: LINUX

2007-12-30 18:00:06


Shell程序集锦五——有用的小脚本[收集]
http://blog.chinaunix.net/u/26011/showart_443577.html


Linux程序设计(第3版)
          
          


删除命令的使用
循环删除一个目录下的所有某文件或目录
find ./ -name ".svn" | rm -rf

find ./ -name ".svn" | xargs rm -rf
都可以删除操作,如果找到的文件非常多,那么必须用第二个命令
find . -type d -iname ".svn" -exec rm -rf {} \;
参考:http://onlypython.group.javaeye.com/group/blog/53926

想删除ca20061101至ca20061103开头的文件。
find -type f -name "ca2006110[1-3]*" -exec rm {} \;
find -type f -name "ca2006110[1-3]*" |xargs -I $ rm -rf $

touch -t 07102300 temp
find . ! -newer temp|xargs rm
上面命令查找7月10日23点前的文件并删除 find . ! -newer temp|xargs rm -rf

把命令执行过程的信息保存到一个文件中

ls -ls  | tee log.txt

find命令使用
1. find /path -name filename
    filename可以是一个具体的文件名,或者可以这样“*string*”,其中星号表明任意字符,如要查找swapfile文件但又不知道该文件完整的名称只知道apfi,那么你可以输入:*apfi*,这样就可以了,同理,如果输入:swap*,那么只查找以swap开头的文件名。

2. find /path -size +1000000c
-size:     表示要用文件大小来进行查找
+1000000c: 表示查找文件大小,+表示大于1000000byte,c表示byte(字节),也可以使用-,表示小于1000000byte,当然了,文件大小由你自己来指定。

3. find /path -amin -5           //查找在系统中最后5分钟访问的文件
   find /path -mmin -5           //查找在系统中最后5分钟修改的文件
   find /path -atime -1          //查找在系统中最后1小时访问的文件
   find /path -mtime -1          //查找在系统中最后1小时修改的文件
   find /path -empty            //查找在系统中空的文件或文件夹
   find /path -user dotnetlee      //查找在系统中属于dotnetlee用户的文件
   find /path -nouser            //查找在系统中已经没有属主的文件

4. 使用逻辑运算符:or、 !、 and
   find /path -user dotnetlee -o -user AnotherUser
   //查找在系统中属于dotnetlee用户的文件或者属于AnotherUser用户的文件
   find /path ! -user AnotherUser
   //查找在系统中不属于AnotherUser用户的文件
   find /path -size -1000000c -a -mtime -1
   //查找在系统中小于1000000byte并最后1小时修改的文件

5. find命令还可以联合系统自有的一些命令来执行,如:
   find /path -name filename -ls
   //查找文件并列出该文件的属性信息

注:
find [起始目录] 寻找条件 操作
-prune
    If -depth is not given, true; if the file is a directory, do not descend into it. If -depth is given, false; no effect.
    prune 删除;削减

$ find ./ -name "*.txt"
./spi.txt
./cmmb/i2c.txt
./cmmb/gpio.txt
./cmmb/spi.txt
$ find ./ -path "./cmmb" -prune -o -name "*.txt" -print
./spi.txt
在当前目录下,忽略./cmmb目录,寻找名为"*.txt"的文件

字符串替换

批量文件内容替换

sed -i 's/moban/sty_ss_ntw/g'  `find ./ -type f`

查找当前目录下的普通文件,把moban 用sty_ss_ntw替换

sed -i 's/moban/sty_ss_ntw/g'  `find ./ -name Makefile.am`

sed -i 's/moban/sty_ss_ntw/g' *

批量文件更名

把当前目录下的所有文件加上prefix-
ls |while read file;do echo "mv "$file"  -> prefix-$file"; mv "$file" prefix-$file;done

当前目录下所有文件名中的moban字符更改为sty_ss_ntw
for i in `find ./ -name '*moban*'`; do mv "$i" $(echo "$i" | sed  's/moban/sty_ss_ntw/g'); done


project_moban使用的脚本

for i in `find ./ -name '*moban*'`; do mv "$i" $(echo "$i" | sed  's/moban/sty_ss_ntw/g'); done;sed -i 's/moban/sty_ss_ntw/g'  `find ./ -type f`
for i in `find ./ -name '*sty_ss_ntw*'`; do mv "$i" $(echo "$i" | sed  's/sty_ss_ntw/moban/g'); done;sed -i 's/sty_ss_ntw/moban/g'  `find ./ -type f`


巧用find命令清除系统垃圾
find / -name core -print -exec rm -rf {} \;


do_patch.sh 批量打补丁
#!/bin/sh
fileslist=`ls *.diff`
for diff in $fileslist
do
        echo "$file"
        patch -p1 <$diff
done

启动移动硬盘上的svn服务
#!/bin/sh
sudo mount /media/REPOS/ -o remount,exec
sudo mount /media/linux/ -o remount,exec
svnserve -d -r /media/REPOS/svn


删除文件中的 ^M 符号

sed -i 's/^M//g'  `find ./ -type f`

^M 符号是通过
Ctrl+v 和Ctrl+M 来输入的。


shell脚本——Learning the bash Shell , 3rd 读书笔记篇
Linux Bash Shell学习(一):开始看书《Learning the bash Shell,3rd》
Linux Bash Shell学习(二):目录和通配符
Linux Bash Shell学习(三):基本I/O操作、特殊字符、控制键和帮助
Linux Bash Shell学习(四):编辑命令
Linux Bash Shell学习(五):特殊文件、别名、选项和参数
Linux Bash Shell学习(六):设置环境参数
Linux Bash Shell学习(七):shell编程基础——运行Shell脚本、function
Linux Bash Shell学习(八):shell编程基础——string操作
Linux Bash Shell学习(九):流程控制——if/else
Linux Bash Shell学习(十):流程控制——for
Linux Bash Shell学习(十一):流程控制——case
Linux Bash Shell学习(十二):流程控制——select
Linux Bash Shell学习(十三):流程控制——while/util
Linux Bash Shell学习(十四):命令行选项
Linux Bash Shell学习(十五):变量类型和整型运算
Linux Bash Shell学习(十六):数组
Linux Bash Shell学习(十七):I/O重定向
Linux Bash Shell学习(十八):String I/O——echo和printf
Linux Bash Shell学习(十九):String I/O——read
Linux Bash Shell学习(二十):命令行处理


阅读(786) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~