1

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:

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

I tried this link: http://stackoverflow.com/questions/15290186/…: find . -name 'Rtp-' -exec bash -c 'mv $0 ${0/*/Rtp}' {} \; The problem that if dir already exists it move one directory into another.

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.

Simcha
  • 3,300
  • 7
  • 29
  • 42
  • 2
    Purpose of this site is to help in solving problems, but not in doing your work for you. What did you try already? How did it fail? Failed attempt logs? – metamorphling Sep 08 '17 at 04:46
  • I tried this link: https://stackoverflow.com/questions/15290186/find-a-pattern-in-files-and-rename-them: find . -name '*Rtp-*' -exec bash -c 'mv $0 ${0/*/Rtp}' {} \; The problem that if dir already exists it move one directory in another – Simcha Sep 08 '17 at 04:51
  • 1
    please move what you have tried into the body of your Q and delete the comment. Qs should be self contained and not require going to other links. We need to see existing setup of your dir structure, and desired structure. Verbal descriptions are almost alway ambiguous (as is yours) so we need to see a real use case that can be solved. Please read http://stackoverflow.com/help/how-to-ask , http://stackoverflow.com/help/dont-ask , http://stackoverflow.com/help/mcve and take the [tour](http://stackoverflow.com/tour) before posting more Qs here. Good luck. – shellter Sep 08 '17 at 05:34
  • See https://stackoverflow.com/questions/39338196/extract-zip-file-generated-in-windows-using-python-2-7-and-zipfile-in-linux for an example of a "real" use case and how to illustrate it in the body of your Q. Good luck. – shellter Sep 08 '17 at 05:35
  • @shellter - fixed – Simcha Sep 08 '17 at 13:01
  • 1
    He wants mv -T but can't find it. – Joshua Sep 08 '17 at 15:50
  • you have 2 tasks. **One** for renaming the `directory-version` to `directory`. **Second** for substituting `myvar`. Thus you should ask 2 separate questions. Now, here, clean one of them, And both are easy. Then ask another one. – Shakiba Moshiri Sep 08 '17 at 16:05

2 Answers2

1

1.

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

  • Find all directories in Rtp-version
  • Iterate through all of the results (for...)
  • Copy recursively to Rtp/, and -f will overwrite

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
  • Use sed (the Stream editor) to -i interactively -e search for a string (by regex) and replace it (eg s/<oldval>/<newval>/). Note that oldval and newval will need to be escaped. If they contain a lot of /'s,you could do something like s|<oldval>|<newval>|.
onwsk8r
  • 395
  • 3
  • 11
1

Based on @Brian Hazeltine answer and Check if a file exists with wildcard in shell script

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
Simcha
  • 3,300
  • 7
  • 29
  • 42