如何在Linux命令行中替换多个文件中的字符串
本文翻译自:How to replace a string in multiple files in linux command line
I need to replace a string in a lot of files in a folder, with only ssh
access to the server. 我需要替换文件夹中许多文件中的字符串,而只能通过ssh
访问服务器。 How can I do this? 我怎样才能做到这一点?
#1楼
参考:https://stackoom.com/question/lnhe/如何在Linux命令行中替换多个文件中的字符串
#2楼
cd /path/to/your/folder
sed -i 's/foo/bar/g' *
Occurrences of "foo" will be replaced with "bar". 出现的“ foo”将替换为“ bar”。
#3楼
This worked for me: 这为我工作:
find ./ -type f -exec sed -i 's/string1/string2/' {} \;
Howerver, this did not: sed -i 's/string1/string2/g' *
. 但是,这没有: sed -i 's/string1/string2/g' *
。 Maybe "foo" was not meant to be string1 and "bar" not string2. 也许“ foo”不是字符串1,“ bar”不是string2。
#4楼
@kev's answer is good, but only affects files in the immediate directory.The example below uses grep to recursively find files. @kev的答案很好,但只影响即时目录中的文件。下面的示例使用grep递归查找文件。 It works for me everytime. 每次都对我有用。
grep -rli 'old-word' * | xargs -i@ sed -i 's/old-word/new-word/g' @
Command breakdown 命令细目
grep -r : --recursive , recursively read all files under each directory. grep的-r:--recursive递归地读每一目录下的所有文件。
grep -l : --print-with-matches , prints the name of each file that has a match, instead of printing matching lines. grep -l : -- print -with-matches ,打印每个具有匹配项的文件的名称,而不是打印匹配的行。
grep -i : --ignore-case . grep -i :-- ignore-case 。
xargs : transform the STDIN to arguments, follow this answer . xargs :将STDIN转换为参数,请遵循以下答案 。
xargs -i@ ~command contains @~ : a placeholder for the argument to be used in a specific position in the ~command~ , the @ sign is a placeholder which could replaced by any string. xargs的-i @〜命令中包含@〜:到在〜命令〜特定位置被用于参数的占位符,@符号是这可以通过任何字符串替换的占位符。
sed -i : edit files in place, without backups. sed -i :就地编辑文件, 不进行备份。
sed s/regexp/replacement/ : substitute string matching regexp with replacement . sed s / regexp / replacement / :用替换匹配regexp的字符串。
sed s/regexp/replacement/ g : global , make the substitution for each match instead of only the first match. sed s / regexp / replacement / g : global ,对每个匹配项进行替换,而不是仅对第一个匹配项进行替换。
#5楼
Similar to Kaspar's answer but with the g flag to replace all the occurrences on a line. 与Kaspar的答案类似,但带有g标志来替换一行中的所有匹配项。
find ./ -type f -exec sed -i 's/string1/string2/g' {} \;
For global case insensitive: 对于不区分大小写的全局变量:
find ./ -type f -exec sed -i 's/string1/string2/gI' {} \;
#6楼
grep --include={*.php,*.html} -rnl './' -e "old" | xargs -i@ sed -i 's/old/new/g' @
如何在Linux命令行中替换多个文件中的字符串相关推荐
- linux命令行的操作符,如何在Linux命令行中进行基本的数学运算
原标题:如何在Linux命令行中进行基本的数学运算 Linux bash或命令行允许您执行基本和复杂的算术和布尔运算.像expr,jot,bc和factor等命令可以帮助您找到复杂问题的最优数学解决方 ...
- linux命令操作改变图片大小,如何在Linux命令行中优化和压缩JPEG或PNG图像
原标题:如何在Linux命令行中优化和压缩JPEG或PNG图像 来自:Linux迷https://www.linuxmi.com/linux-yasuo-jpeg-png.html 您有很多图像,并且 ...
- 如何在 Linux 命令行中切换用户
如何在 Linux 命令行中切换用户 我在cnaaa上购买了云服务器. Linux 系统有不同类型的用户,用户有不同类型的权限. 并非所有用户都可以执行所有命令,也不是所有用户都可以切换到其他用户.我 ...
- 【转】Linux命令行下svn ignore忽略文件或文件夹用法
转自:Linux命令行下svn ignore忽略文件或文件夹用法 - 陌上归人的博客 - 博客园 一.忽略单个目录 1.忽略文件夹 假如目录oa.youxi.com是从svn checkout出来的, ...
- linux命令行安装 php_如何在 Linux 命令行中使用和执行 PHP 代码
PHP是一个开源服务器端脚本语言,最初这三个字母代表的是"Personal Home Page",而现在则代表的是"PHP:Hypertext Preprocessor& ...
- linux裁剪图片的软件,ImageMagick教程:如何在Linux命令行中剪裁图像
问题:我想要去除图像文件中的白色空白,有没有什么便捷的方法能在Linux命令行中对图像文件进行剪裁? 当涉及到在Linux中转换或编辑图像文件时,ImageMagick毫无疑问是最为熟知的一体化软件之 ...
- linux裁剪图片的软件,技术|Linux有问必答——如何在Linux命令行中剪裁图像
问题:我想要去除图像文件中的白色空白,有没有什么便捷的方法能在Linux命令行中对图像文件进行剪裁? 当涉及到在Linux中转换或编辑图像文件时,ImageMagick毫无疑问是最为熟知的一体化软件之 ...
- 如何在 Linux 命令行中终止进程?
如果你想在linux上停止某个进程,你会怎么操作? 如果命令/进程在前台运行,您可以使用 Ctrl+C 终端快捷方式,但是,如果进程不可见(在后台运行),您可以使用专用命令"杀死它" ...
- 如何在Linux命令行下收听网络电台
对于花大量时间在Linux屏幕前的系统管理员和Linux爱好者来说,网络/本地电台上的音乐可以极大提高生产力,你们知道吗?不管你们相信与 否,,在工作环境中有适当的声音,如音乐或者闲聊声包围,都可以提 ...
最新文章
- FragmentManager的简单使用
- 让ASP程序在服务器中自动运行
- Docker中安装Jenkins实时发布.net core 项目(二)
- 【牛客 - 301哈尔滨理工大学软件与微电子学院第八届程序设计竞赛同步赛(高年级)】小乐乐下象棋(记忆化搜索dp,dfs)
- Flink的Table API 与SQL的流处理
- 【2017001】IList转DataTable、DataTable转IList
- JAVA零碎要点008---tomcat启动的时候报错了严重: End event threw exception java.lang.reflect.InvocationTargetExcepti
- java 使用适当的签名_java11教程--类SignatureSpi用法
- selenium(待补充)
- python机器学习-糖尿病数据挖掘
- Java并发编程之安全发布对象的四种方法
- 一阶惯性环节如何实现跟踪性能与滤波性能共存(总)
- macBigSur使用mathtype数学公式编辑
- 马王堆汉墓帛书‧老子——甲本释文(德经)
- 如何减少PDF文件的大小,为pdf瘦身?
- 用百数教培管理系统轻松实现在线排课,优化机构日常管理
- P0.9COB小间距LED显示技术的优势。
- Windows 10和Linux脚本启动jar包服务器,并设置开机启动
- 视频编辑器-MovieMator 简洁使用-在时间线上拆分,修剪,连接剪辑
- 告诉你一个真实的二线城市创业生态