[英]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脚本来做下一步:
Rtp/
and Rtp-[version]/
, I need if Rtp-[version]/
dir exists rename it to the Rtp/
and override it's content. 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
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
for...
) for...
) -f
will overwrite -f
将覆盖 2. 2。
for f in $(find Rtp -type f -name "app.properties"): do sed -ie s/myvar=myval/myvar=newval/ $f done
-i
interactively -e
search for a string (by regex) and replace it (eg s/<oldval>/<newval>/
). -i
交互-e
(通过正则表达式)搜索字符串并将其替换(例如s/<oldval>/<newval>/
)。 Note that oldval and newval will need to be escaped. /
'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.