linux替换目录下所有文件中的某字符串

linux替换目录下所有文件中的某字符串

比如,要将目录/modules下面所有文件中的zhangsan都修改成lisi,这样做:


sed -i "s/zhangsan/lisi/g" `grep zhangsan -rl /modules`


解释一下:


-i 表示inplace edit,就地修改文件
-r 表示搜索子目录
-l 表示输出匹配的文件名


这个命令组合很强大,要注意备份文件

转自:http://blog.csdn.net/wide288/article/details/22387723

将当前目录下的test文件夹的中

sed -i "s/123qwe/new123asd/g" `grep 123qwe -rl test`

[root@hadoop2 xiaole_chk_url]# cd test/
[root@hadoop2 test]# tree 
.
├── 123qwe
├── a.txt
├── b.txt
└── t2
    └── er.txt

2 directories, 3 files
[root@hadoop2 test]# vim 123qwe.txt
[root@hadoop2 test]# ll -as
total 28
4 drwxr-xr-x 4 root root 4096 Mar 23 09:22 .
4 drwxr-xr-x 4 root root 4096 Mar 23 09:10 ..
4 drwxr-xr-x 2 root root 4096 Mar 23 09:21 123qwe
4 -rw-r--r-- 1 root root    6 Mar 23 09:22 123qwe.txt
4 -rw-r--r-- 1 root root   17 Mar 23 09:14 a.txt
4 -rw-r--r-- 1 root root   25 Mar 23 09:14 b.txt
4 drwxr-xr-x 2 root root 4096 Mar 23 09:14 t2
[root@hadoop2 test]# cd ..
[root@hadoop2 xiaole_chk_url]# sed -i "s/123qwe/new123asd/g" `grep 123qwe -rl test`
sed: no input files
[root@hadoop2 xiaole_chk_url]# ll -as
total 92472
    4 drwxr-xr-x 4 root root     4096 Mar 23 09:10 .
    4 drwxr-xr-x 4 root root     4096 Mar 22 10:45 ..
    4 -rw-r--r-- 1 root root       39 Mar 22 10:48 a.sh
42712 -rw-r--r-- 1 root root 43733593 Mar 14 13:52 bulk.del.es.json.log
  288 -rw-r--r-- 1 root root   290933 Mar 14 17:22 bulk_file
20548 -rw-r--r-- 1 root root 21033520 Mar 14 17:39 bulk_file_sub
28884 -rw-r--r-- 1 root root 29573229 Mar 15 08:52 bulk.index.del.es.json.log
    4 -rw-r--r-- 1 root root        1 Mar 15 09:23 bulk.index.del.splitfile.json
    4 -rw-r--r-- 1 root root      955 Mar 14 13:52 looh.index.sh
    4 -rw-r--r-- 1 root root      258 Mar 15 08:54 looh.index.splitfile.es.sh
    4 -rw-r--r-- 1 root root      683 Mar 15 09:22 looh.index.splitfile.sh
    4 -rw-r--r-- 1 root root      671 Mar 14 13:52 looh.sh
    4 drwxr-xr-x 2 root root     4096 Mar 15 09:38 splitfile
    4 drwxr-xr-x 4 root root     4096 Mar 23 09:22 test
[root@hadoop2 xiaole_chk_url]# cd test/
[root@hadoop2 test]# ll -as
total 28
4 drwxr-xr-x 4 root root 4096 Mar 23 09:22 .
4 drwxr-xr-x 4 root root 4096 Mar 23 09:10 ..
4 drwxr-xr-x 2 root root 4096 Mar 23 09:21 123qwe
4 -rw-r--r-- 1 root root    6 Mar 23 09:22 123qwe.txt
4 -rw-r--r-- 1 root root   17 Mar 23 09:14 a.txt
4 -rw-r--r-- 1 root root   25 Mar 23 09:14 b.txt
4 drwxr-xr-x 2 root root 4096 Mar 23 09:14 t2
[root@hadoop2 test]# tree 
.
├── 123qwe
├── 123qwe.txt
├── a.txt
├── b.txt
└── t2
    └── er.txt

2 directories, 4 files
[root@hadoop2 test]# vim as.txt
[root@hadoop2 test]# ll -as
total 32
4 drwxr-xr-x 4 root root 4096 Mar 23 09:23 .
4 drwxr-xr-x 4 root root 4096 Mar 23 09:10 ..
4 drwxr-xr-x 2 root root 4096 Mar 23 09:21 123qwe
4 -rw-r--r-- 1 root root    6 Mar 23 09:22 123qwe.txt
4 -rw-r--r-- 1 root root 1937 Mar 23 09:23 as.txt
4 -rw-r--r-- 1 root root   17 Mar 23 09:14 a.txt
4 -rw-r--r-- 1 root root   25 Mar 23 09:14 b.txt
4 drwxr-xr-x 2 root root 4096 Mar 23 09:14 t2
[root@hadoop2 test]# cd ..
[root@hadoop2 xiaole_chk_url]# sed -i "s/123qwe/new123asd/g" `grep 123qwe -rl test`
[root@hadoop2 xiaole_chk_url]# cd test/
[root@hadoop2 test]# tree
.
├── 123qwe
├── 123qwe.txt
├── as.txt
├── a.txt
├── b.txt
└── t2
    └── er.txt

2 directories, 5 files
[root@hadoop2 test]# 

  

注意:

只修改文件中的文件内容的字符串,不修改文件名、文件夹名

原文地址:https://www.cnblogs.com/rsapaper/p/8628429.html