git 重命名许多文件和文件夹 [英] git rename many files and folders

查看:51
本文介绍了git 重命名许多文件和文件夹的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试重命名我的应用程序中的许多文件,并且需要能够通过仅替换匹配文本的 git(即 git mv %filenamematch% %replacement%)在从应用程序根目录开始的所有子目录中进行重命名.不过我不擅长 bash 脚本.

I am trying to rename many files in my application and need to be able to do a rename in all subdirectories from the app root through git (i.e. git mv %filenamematch% %replacement%) that only replaces the matching text. I'm no good with bash scripting though.

更新:如果也重命名匹配的目录就好了!

update: would be good it if also renamed directories that match as well!

推荐答案

这应该可以解决问题:

for file in $(git ls-files | grep %filenamematch% | sed -e 's/(%filenamematch%[^/]*).*/1/' | uniq); do git mv $file $(echo $file | sed -e 's/%filenamematch%/%replacement%/'); done

要了解它的作用,您需要了解带有|"的管道.和命令替换为$(...)".这些强大的 shell 构造允许我们组合多个命令来获得我们需要的结果.请参阅管道命令替换.

To follow what this is doing, you'll need to understand piping with "|" and command substitution with "$(...)". These powerful shell constructs allow us to combine several commands to get the result we need. See Pipelines and Command Substitution.

这是单线中发生的事情:

Here's what's going on in this one-liner:

  1. git ls-files:这会生成 Git 存储库中的文件列表.它类似于你可以从 ls 得到的,除了它只输出 Git 项目文件.从此列表开始可确保 .git/目录中的任何内容都不会被触及.

  1. git ls-files: This produces a list of files in the Git repository. It's similar to what you could get from ls, except it only outputs Git project files. Starting from this list ensures that nothing in your .git/ directory gets touched.

<代码>|grep %filenamematch%:我们从 git ls-files 获取列表,并通过 grep 将其过滤为仅包含我们正在查找的单词或模式的文件名.

| grep %filenamematch%: We take the list from git ls-files and pipe it through grep to filter it down to only the file names containing the word or pattern we're looking for.

<代码>|sed -e 's/(%filenamematch%[^/]*).*/1/':我们通过 sed(流编辑器),执行 (-e) sed 的 s(替代)命令以截断我们匹配目录之后的任何/和后续字符(如果它恰好是一个).

| sed -e 's/(%filenamematch%[^/]*).*/1/': We pipe these matches through sed (the stream editor), executing (-e) sed's s (substitute) command to chop off any / and subsequent characters after our matching directory (if it happens to be one).

<代码>|uniq:如果匹配是一个目录,现在我们已经切断了包含的目录和文件,可能会有很多匹配的行.我们使用 uniq 将它们全部合并为一行.

| uniq: In cases where the match is a directory, now that we've chopped off contained directories and files, there could be many matching lines. We use uniq to make them all into one line.

for file in ...:shell 的 for"command 将遍历列表中的所有项目(文件名).每个文件名依次分配给变量$file".然后执行分号(;)后的命令.

for file in ...: The shell's "for" command will iterate through all the items (file names) in the list. Each filename in turn, it assigns to the variable "$file" and then executes the command after the semicolon (;).

sed -e 's/%filenamematch%/%replacement%/':我们使用 echo 将每个文件名通过 sed 进行管道传输,再次使用它的替换命令——这次对文件名执行我们的模式替换.

sed -e 's/%filenamematch%/%replacement%/': We use echo to pipe each filename through sed, using it's substitute command again--this time to perform our pattern replacement on the filename.

git mv:我们使用这个 git 命令将现有文件 ($file) 转换为新文件名(由 sed 更改的文件名).

git mv: We use this git command to mv the existing file ($file) to the new filename (the one altered by sed).

更好地理解这一点的一种方法是单独观察每个步骤.为此,请在您的 shell 中运行以下命令,并观察输出.所有这些都是非破坏性的,只会生成供您观察的列表:

One way to understand this better would be to observe each of these steps in isolation. To do that, run the commands below in your shell, and observe the output. All of these are non-destructive, only producing lists for your observation:

  1. git ls-files

git ls-files |grep %filenamematch%

git ls-files |grep %filenamematch% |sed -e 's/(%filenamematch%[^/]*).*/1/'

git ls-files |grep %filenamematch% |sed -e 's/(%filenamematch%[^/]*).*/1/' |uniq

for file in $(git ls-files | grep %filenamematch% | sed -e 's/(%filenamematch%[^/]*).*/1/' |唯一);做回声 $file;完成

for file in $(git ls-files | grep %filenamematch% | sed -e 's/(%filenamematch%[^/]*).*/1/' |唯一);做回声 $file |sed -e 's/%filenamematch%/%replacement%/';完成

这篇关于git 重命名许多文件和文件夹的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆