简体   繁体   English

Bash脚本使用动态名称重命名文件夹并替换其中的字符串

[英]Bash script to rename folder with dynamic name and replace it's strings inside

I'm just starting to use Docker, and I'm newbie in bash scripts, but now I need to write bash script that will do next thing: 我刚刚开始使用Docker,而且我是bash脚本的新手,但是现在我需要编写bash脚本来做下一步:

  1. I have 2 dirs with some subdirs: Rtp/ and Rtp-[version]/ , I need if Rtp-[version]/ dir exists rename it to the Rtp/ and override it's content. 我有2个带有一些子目录的目录: Rtp/Rtp-[version]/ ,如果Rtp-[version]/ dir存在,我需要将其重命名为Rtp/并覆盖其内容。 [version] - is dynamic number. [版本]-是动态数字。

My dir structure: 我的目录结构:

|-- Rtp
     |--- subdir 1
     |--- subdir 2
|-- Rtp-1.0 (or Rtp-1.6, Rtp-2.7)
     |--- subdir 1
     |--- subdir 2
  1. After this I need in the new Rtp/ dir find specific file app.properties , and change inside of it string: myvar=my value to string myvar=new value , and do the same thing with 3 more files 之后,我需要在新的Rtp/目录中找到特定文件app.properties ,并在其内部更改字符串: myvar=my value更改为字符串myvar=new value ,并对另外3个文件执行相同操作

I tried this link: http://stackoverflow.com/questions/15290186/ …: find . -name 'Rtp-' -exec bash -c 'mv $0 ${0/*/Rtp}' {} \\; 我尝试了以下链接: http : //stackoverflow.com/questions/15290186/…find . -name 'Rtp-' -exec bash -c 'mv $0 ${0/*/Rtp}' {} \\; find . -name 'Rtp-' -exec bash -c 'mv $0 ${0/*/Rtp}' {} \\; The problem that if dir already exists it move one directory into another. 如果dir已经存在,它将一个目录移动到另一个目录的问题。

Also I want rename it and not copy because it's big dir, and it can take some time to copy. 我也想重命名它而不是复制,因为它的目录很大,复制可能需要一些时间。

Thanks in advance, can you explain please the solution, in order to I will can change in the future if something will be changed. 在此先感谢您能解释一下解决方案,以便将来如果有什么改变我可以改变。

1. 1。

for dir in $(find Rtp-[version] -maxdepth 1 -type d): do cp -Rf $dir Rtp done

  • Find all directories in Rtp-version 在Rtp版本中查找所有目录
  • Iterate through all of the results ( for... ) 遍历所有结果( for...
  • Copy recursively to Rtp/, and -f will overwrite 递归复制到Rtp /,- -f将覆盖

2. 2。

for f in $(find Rtp -type f -name "app.properties"): do sed -ie s/myvar=myval/myvar=newval/ $f done

  • Find all files named app.properties 查找所有名为app.properties的文件
  • Use sed (the Stream editor) to -i interactively -e search for a string (by regex) and replace it (eg s/<oldval>/<newval>/ ). 使用sed(流编辑器)与-i交互-e (通过正则表达式)搜索字符串并将其替换(例如s/<oldval>/<newval>/ )。 Note that oldval and newval will need to be escaped. 请注意,oldval和newval将需要转义。 If they contain a lot of / 's,you could do something like s|<oldval>|<newval>| 如果它们包含很多/ ,则可以执行s|<oldval>|<newval>| .

Based on @Brian Hazeltine answer and Check if a file exists with wildcard in shell script 基于@Brian Hazeltine的答案并检查Shell脚本中是否存在带通配符的文件

I found next solution: 我找到了下一个解决方案:

if ls Rtp-*/ 1> /dev/null 2>&1; then
  mv -T Rtp-*/ Rtp
  find appl.properties -type f -exec sed -ie 's/myvar=my value/myvar=new value/' {} \;
fi

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM