ITeye博客

Search and replace files in linux

RobertLeeJesus · 2008-07-31 19:43 · Linux - Miscellaneous · Markdown

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.csdn.net/chaiqi/archive/2007/03/05/1521276.aspx

评论(0)

暂无评论。

评论功能维护中,当前仅展示历史评论。