Search and replace files in linux

本文详细介绍了Linux环境下如何使用find、grep、locate等命令进行文件搜索与管理,包括替换文件及文件夹中的文本内容,并提供了实用的例子。

1. How to use child commands with find ?

     find . -name classes -exec rm -r -f {} \ ;

     Please note there is a blank between {} and \;

 

2. How to use grep ?

     grep [options] PATTERN [FILES]

 

     Recursively search

     grep -r string_pattern *

 

3. How to use locate to find file?

     updatedb

     locate **/*.xml

 

4. How to find which jar constians the specified file?

     find . -name '*.jar' -exec unzip -l {} \; | grep 'Access.java \|Archive:'

     find . -name '*.jar' -exec unzip -l {} \; | grep 'Access.java\|Archive:' | sed 's#\(.*Access.java.*\)#\x1b[1;31m\1\x1b[1;37m#'

 

5.How to replace the text in a file?

     sed -in-place -e 's/are/were/g' test.txt

 

6. How to replace the text in a folder?

    find . -name "*.txt" -exec sed -in-place -e 's/are/were/g' {} \;

 

7. \ is the escape char.So if you want to replace C:\Windows with D:\Windows

    sed -in-place -e 's/C:\/windows/D:\/Widnows/g' test.txt

 

Reference

 

http://www.linuxsir.org/main/?q=node/137

http://blog.youkuaiyun.com/chaiqi/archive/2007/03/05/1521276.aspx

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值