Linux 如何更改目录中所有文件中出现的所有单词

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8905662/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 04:07:32  来源:igfitidea点击:

How to change all occurrences of a word in all files in a directory

linuxubuntureplace

提问by Ryan Ward

I was in the process of creating a Userclass where one of the methods was get_privileges();.

我正在创建一个User类,其中一个方法是get_privileges();.

After hours of slamming my head into the keyboard, I finally discovered that the previous coder who I inherited this particular database spelled the word "privileges" as "privelages" in the MySQL database, and thus also everywhere in the hundreds of files that access these "privelages" it is spelled that way.

之后砰我的头到键盘的时间,我终于发现,我是谁继承了这个特定的数据库先前编码拼写的单词“权限”为“ privelages在数百个文件在MySQL数据库”,因而也处处访问这些“ privelages”是这样拼写的。

Is there a way in Linux (Ubuntu Server) that I can go through every place in the /var/wwwfolder and replace "privelages" with "privileges", so that I don't have to deal with this typo and code around it?

在 Linux ( Ubuntu Server) 中有没有办法可以遍历/var/www文件夹中的每个位置并将“ privelages”替换为“权限”,这样我就不必处理这个错字和围绕它的代码?

采纳答案by Dan Fego

A variation that takes into account subdirectories (untested):

考虑子目录的变体(未经测试):

find /var/www -type f -exec sed -i 's/privelages/privileges/g' {} \;

This will findall files (not directories, specified by -type f) under /var/www, and perform a sedcommand to replace "privelages" with "privileges" on each file it finds.

这将在 下的find所有文件(不是由 指定的目录-type f/var/www,并sed在它找到的每个文件上执行将“privelages”替换为“privileges”的命令。

回答by matwilso

I generally use this short script, which will rename a string in all files and all directory names and filenames. To use it, you can copy the text below into a file called replace_string, run sudo chmod u+x replace_stringand then move it into your sudo mv replace_string /usr/local/binfolder to be able to execute it in any directory.

我通常使用这个简短的脚本,它将重命名所有文件和所有目录名和文件名中的字符串。要使用它,您可以将下面的文本复制到名为 的文件中replace_string,运行sudo chmod u+x replace_string然后将其移动到您的sudo mv replace_string /usr/local/bin文件夹中,以便能够在任何目录中执行它。

NOTE: this only works on linux (tested on ubuntu), and fails on MacOS. Also be careful with this because it can mess up things like git files. I haven't tested it on binaries either.

注意:这仅适用于 linux(在 ubuntu 上测试),在 MacOS 上失败。还要小心这一点,因为它可能会弄乱 git 文件之类的东西。我也没有在二进制文件上测试过它。

#!/usr/bin/env bash

# This will replace all instances of a string in folder names, filenames,
# and within files.  Sometimes you have to run it twice, if directory names change.


# Example usage:
# replace_string apple banana

echo 
echo 

find ./ -type f -exec sed -i -e "s///g" {} \;  # rename within files
find ./ -type d -exec rename "s///g" {} \;    # rename directories
find ./ -type f -exec rename "s///g" {} \;  # rename files

回答by leerssej

for Mac OS:

对于 Mac 操作系统:

find . -type f -name '*.sql' -exec sed -i '' s/privelages/privileges/ {} +

Matwilso's script from above would then look like this if it were designed to work on Mac OS

如果它被设计为在 Mac OS 上工作,那么上面的 Matwilso 的脚本将如下所示

#!/bin/bash

# This will replace all instances of a string in folder names, filenames,
# and within files.  Sometimes you have to run it twice, if directory names change.


# Example usage:
# replace_string apple banana

echo 
echo 

find . -type f -name '*.sql' -exec sed -i '' s/// {} + # inside sql scripts