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

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

问题描述

我正尝试重命名应用程序中的许多文件,并且需要能够通过git(即git mv%filenamematch%%replacement%)从应用程序根目录执行所有子目录中的重命名操作,这些文件只替换匹配的文本。但是我对bash脚本编程并不满意。



update:如果也重命名的目录匹配的话,会更好!

 对于$($)中的文件来说, git ls-files | grep%filenamematch%| sed -e's / \(%filenamematch%[^ /] * \)。* / \ 1 /'| uniq); git mv $ file $(echo $ file | sed -e's /%filenamematch%/%replacement%/')

要了解这是做什么的,你需要用|来理解管道并用$(...)替换命令。这些强大的shell构造允许我们结合几个命令来获得我们需要的结果。请参阅管道命令替换



这是一行代码:


  1. git ls-files :这产生一个文件列表在Git仓库中。这与您可以从 ls 中获得的相似,只是它只输出Git项目文件。从这个列表开始,可以确保您的.git /目录中的任何内容都不会被触及。

  2. grep%filenamematch%:我们从 git ls-files 获取列表,并通过grep将其过滤为只包含文件名我们正在寻找的单词或模式。 sed -e's / \(%filenamematch%[^ /] * \)。* / \ 1 /':我们通过 sed (流编辑器),执行(-e)sed's(替代)命令以切断我们匹配的目录后面的任何/和后续字符(如果它恰好是一个)。 uniq :在匹配是目录的情况下,现在我们已经截断了包含的目录和文件,可能有很多匹配的行。我们使用uniq将它们全部合并为一行。 shell的命令将迭代所有项目(文件名)。每个文件名依次分配给变量$ file,然后在分号(;)后执行该命令。


  3. sed -e's /%filenamematch%/%replacement%/':我们使用回声通过sed管道每个文件名,再次使用它的替代命令 - 这次是在文件名上执行我们的模式替换。

  4. c $ c> git mv :我们使用这个git命令将现有文件($ file)映射到新文件名(由sed修改的文件名)。

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


  1. git ls-files


  2. git ls-files | grep%filenamematch%


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

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


  4. 用于$(git ls-files | grep%filenamematch%| sed -e 's / \(%filenamematch%[^ /] * \)。* / \ 1 /'| uniq); echo $ file


  5. 用于$(git ls-files | grep%filenamematch%| sed -e's / \(%filenamematch%[^ /] * \)。* / \ 1 /'| uniq); echo $ file | sed -e's /%filenamematch%/%replacement%/'



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!

解决方案

This should do the trick:

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

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: 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.

  2. | 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.

  3. | 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).

  4. | 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.

  5. 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 (;).

  6. 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.

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

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

  2. git ls-files | grep %filenamematch%

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

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

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

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

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

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