Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bash: duplicate + rename folder

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?

like image 248
Manfredo Avatar asked Oct 26 '15 10:10

Manfredo


People also ask

How do I copy and rename a folder?

An obvious way to do this is to use a command like “cp file1 file1-orig.” The command is named cp from the short name of copy, which means copy. Linux system users can copy folders, directories, and files using the cp command. We can use cp commands along with destination and source only.

Can cp rename files?

Actually, we've already covered half of renaming, because when you copy or move files, you can also rename. To move and rename the file, just substitute mv for cp in the above example.


1 Answers

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/

like image 117
Noproblem Avatar answered Oct 07 '22 08:10

Noproblem