Linux批量替换目录下文件内容字符串【测试成功】

艺帆风顺 发布于 2025-04-02 17 次阅读


一、批量查找当前文件夹包含字符串的.sh文件

    grep -rl 'sort -nr' --include='*.sh' .

二、批量查找并显示文件信息

    grep -rl 'sort -Vr' --include='*.sh' . | xargs ls -l

    [zz@0001 tomp]$ grep -rl 'sort -Vr' --include='*.sh' . | xargs ls -l-rwxr-xr-x 1 zz zz 661 8月 16 15:40 ./tomp-dm/start-tomp-dm.sh-rwxr-xr-x 1 zz zz 446 8月 22 09:54 ./tomp-dm/stop-tomp-dm.sh-rwxr-xr-x 1 zz zz 684 8月 21 21:24 ./tomp-web-autoschedule/start-tomp-web-autoschedule.sh-rwxr-xr-x 1 zz zz 466 8月 22 09:54 ./tomp-web-autoschedule/stop-tomp-web-autoschedule.sh-rwxr-xr-x 1 zz zz 722 8月 22 09:54 ./tomp-web-cm/start-tomp-web-cm.sh-rwxr-xr-x 1 zz zz 671 8月 21 18:39 ./tomp-web-hdm/start-tomp-web-hdm.sh-rwxr-xr-x 1 zz zz 454 8月 22 09:54 ./tomp-web-hdm/stop-tomp-web-hdm.sh-rwxr-xr-x 1 zz zz 676 8月 22 09:54 ./tomp-web-ra/start-tomp-web-ra.sh-rwxr-xr-x 1 zz zz 726 8月 22 09:54 ./tomp-web-report/start-tomp-web-report.sh-rwxr-xr-x 1 zz zz 508 8月 22 09:54 ./tomp-web-report/stop-tomp-web-report.sh-rwxr-x--- 1 zz zz 733 8月 22 09:54 ./tomp-web-staffuser/start-tomp-web-staffuser.sh-rwxrwxrwx 1 zz zz 670 8月 22 09:54 ./tomp-web-topo/start-tomp-web-topo.sh-rwxrwxrwx 1 zz zz 452 8月 22 09:54 ./tomp-web-topo/stop-tomp-web-topo.sh-rwxr-xr-x 1 zz zz 676 8月 22 09:48 ./tomp-web-tsdn/start-tomp-web-tsdn.sh

    三、批量查找并替换

        grep -rl 'sort -nr' --include='*.sh' . | xargs sed -i 's/sort -nr/sort -Vr/g'