Bash: duplicate + rename folder
Asked Answered
M

2

11

Suppose I have a folder named my_folder_old in /path/to/folder, how can I create a duplicate named my_folder_new in the same directory?

EDIT

Moreover if my_folder_new already exists, my_folder_old is created inside the first and not substituted. Why is this happening?

Mole answered 26/10, 2015 at 10:48 Comment(5)
cp -rp /path/to/folder/my_folder_old /path/to/folder/my_folder_new check: cyberciti.biz/faq/copy-folder-linux-command-lineAleph
The reason why is this happening because, my_folder_new already created. Doing same cp command it will see as new path, /path/to/folder/my_folder_new/. To workaround current situation, check if directory exists and copy like this: cp -frp /path/to/folder/my_folder_old/* /path/to/folder/my_folder_new/*Aleph
Ok, thanks. If you post your solution as an answer I can flag it as accepted:)Mole
Is that really worth a downvote? Because the first link doesn't really address my question while the cp man is detailed but also very general.Mole
it's not me who down-vote :) Just as example why someone down-voting link: meta.#253177Aleph
A
17

Tutorial copy files, folder link: link

Manual cp command : Link

cp -frp /path/to/folder/my_folder_old -T /path/to/folder/my_folder_new

   -f, --force
          if an existing destination file cannot be opened, remove it
          and try again (this option is ignored when the -n option is
          also used)
   -p     same as --preserve=mode,ownership,timestamps
   -R, -r, --recursive
          copy directories recursively
   -T, --no-target-directory
          treat DEST as a normal file

Though if my_folder_new already exists, my_folder_old is created inside the first and not substituted. Why is this happening?

The reason why is this happening because, my_folder_new already created. Doing same cp command it will see as new path, /path/to/folder/my_folder_new/

Aleph answered 27/10, 2015 at 11:18 Comment(1)
maybe we could make a function in .bashrc called for example dup /path/to/folder to create folder_copy_n where n was the number of copy. the function would look into the path/to/ to see if folder_copy_<n-1> already exists.Endothelioma
S
-1

I was dealing with this same issue, was going crazy ahaha, I tried cp -frp but did not work, so, before of going to do cp just remove the existing folder using rm, see below more info about this:

Remove Directory Linux

If a directory or a file within the directory is write-protected, you will be prompted to confirm the deletion. To remove a directory without being prompted, use the -f option:

rm -rf dir1
Supple answered 17/2, 2021 at 23:36 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.