简体   繁体   中英

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', ie 'root' & it should contain everything that the 'test' directory contains, ie 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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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