1

Background:

I have a Jenkins task that is executing and before running i want to take a copy of a folder and move it to another directory which does not exist and is a dynamic timestamp.

For the purposes of this argument i am not able to create variables to store anything, it must be accomplished without them. I have seen this other question on stackoverflow here, and this seems to confirm the approach i am going for.

Question:

What is the best way to achieve this for Linux/Unix?

I have an idea below but i want to check if this is the best approach or not. It starts by making the directory mkdir -p with the location of the directory /tmp/backup/myapp/ and the date timestamp as the final folder name $(date +%Y%m%d%H%M%S), it then calls a recursive copy cp -r and provides the source directory to be copied /var/www/html/myapp and then passes in the original location url as the destination $_

Suggested Answer

mkdir -p /tmp/backup/myapp/$(date +%Y%m%d%H%M%S) && cp -r /var/www/html/myapp $_
Community
  • 1
  • 1
Graham
  • 651
  • 2
  • 10
  • 23

1 Answers1

2

It seems correct to me, just some minor fix:

mkdir -p "/tmp/backup/myapp/$(date +%Y-%m-%d-%H:%M:%S)" && cp -a "/var/www/html/myapp" "$_"

Use double quotes to avoid space problems (in your example is not needed but better be prudent).

Use cp -a to copy directories. It works as -r|-R but in addiction it always preserves symlinks and metadata, such as timestamps and ownership, of the files.

Giuseppe Ricupero
  • 6,134
  • 3
  • 23
  • 32