Slug
Slug

Reputation: 105

Copy a directory on the current path to the same path with a different name in bash

How to copy a directory under a new name, lets say, to make a backup of the directory.

For an example, imagine, there is a folder called 'root'.

inside it, a folder called, 'test' exists, and there are some files and folders existing under the folder 'test'. The present working directory of bash is 'root'

What I want to be done is

  1. Copy the content of the directory 'test' to a new directory called 'test.bak', which should be created in the same directory as 'test', i.e. 'root' & it should contain everything that the 'test' directory contains, i.e. a backup of 'test'.

Can this be done with a single command? If so, how?

The only way I can think of is creating a new directory named 'test.bak' & copy everything to it, but it takes two commands.

Thank you.

Upvotes: 1

Views: 473

Answers (1)

user1467267
user1467267

Reputation:

Yes you can. You can use the recursive- combined with the parent parameter;

$ cp -RP test test.bak

When with the terminal inside the root folder.

And when in the parent folder of the root folder;

$ cp -RP root/test root/test.bak

Or with an absolute path, or in this case, a placeholder call from the user's home folder;

$ cp -RP ~/Desktop/root/test ~/Desktop/root/test.bak

Upvotes: 1

Related Questions