Cp command does an extra copying on different Ubuntu version for folder cloning












3















Edit: Found a duplicate with solution: rsync -a --delete https://stackoverflow.com/questions/23698183/how-to-force-cp-to-overwrite-directory-instead-of-creating-another-one-inside



new script that works:



#!/bin/bash

numCopy=$1
shift
cmdline="${@}"
echo "prepare folders"
for ((i=1;i<=$numCopy;i++));
do
(rsync -a --delete $cmdline/ $cmdline$i/; ) &
(echo "preparing folder clone: $i") &
done
wait




For multithreaded folder copying,



#!/bin/bash

numCopy=$1
shift
cmdline="${@}"
echo "prepare folders"
for ((i=1;i<=$numCopy;i++));
do
(cp -rf $cmdline $cmdline$i; ) &
(echo "preparing folder clone: $i") &
done
wait


this script copies a source folder $cmdline to (non-existent) destination $cmdline$i without issue at my pc with Ubuntu 16.04 but when I run same script on a cloud computer with Ubuntu 18.04, it does this:




  • duplicates source as destination (I need only this)

  • duplicates source into destination too (destination/source I don't want this)


this doesn't break any programs but I don't want to consume unnecessary cloud space.



Why would some different version of Ubuntu add a secondary copy into destination folder?



I'm using this script as



./myscript.sh 2 foldertest


this duplicates foldertest as foldertest1 and foldertest2. If first parameter is 32 then it makes 32 copies up to foldertest32.



Documentation of cp says -r is recursive copy which is I need to do full deep cloning of source folder just like copy pasting (in-place where it produces folder2 folder3 ...) in windows or graphical terminal of Ubuntu. Also I add -f to force it copy files without asking because I'm making up to 64 clones and don't want to answer 64 questions if there are already 64 clones.



Some extra info about the behaviour I need:



Folder A: a.txt b.txt c.txt



Folder A1,A2,A3,A4.. to be:



a.txt
b.txt
c.txt


but instead it is:



a.txt
b.txt
c.txt
A


normally when I copy file to file, it overwrites(if it exists). I need same overwrite behavior on folders, not copying into.










share|improve this question

























  • What do you mean with "it duplicates the source into the destination too"? I don't understand. :(

    – Tommiie
    Jan 9 at 9:34











  • I need a folder "foldera" deep copied over a new folder "folderb" which doesn't exist or not. But it does extra copy that makes "folderb/foldera" duplicating its size. It makes a new folder but that new folder contains source as a whole too. There are N files in source folder, ther are copied which is ok but it also copies source "into" destination too not just "over".

    – huseyin tugrul buyukisik
    Jan 9 at 9:36













  • Okay. Now I understand. Thanks.

    – Tommiie
    Jan 9 at 9:39











  • There must be something very simple that I miss but can't see. Most probably I'm wrong and Linux is right but where?

    – huseyin tugrul buyukisik
    Jan 9 at 9:40








  • 1





    Ok I found a duplicate stackoverflow.com/questions/23698183/…

    – huseyin tugrul buyukisik
    Jan 9 at 9:51
















3















Edit: Found a duplicate with solution: rsync -a --delete https://stackoverflow.com/questions/23698183/how-to-force-cp-to-overwrite-directory-instead-of-creating-another-one-inside



new script that works:



#!/bin/bash

numCopy=$1
shift
cmdline="${@}"
echo "prepare folders"
for ((i=1;i<=$numCopy;i++));
do
(rsync -a --delete $cmdline/ $cmdline$i/; ) &
(echo "preparing folder clone: $i") &
done
wait




For multithreaded folder copying,



#!/bin/bash

numCopy=$1
shift
cmdline="${@}"
echo "prepare folders"
for ((i=1;i<=$numCopy;i++));
do
(cp -rf $cmdline $cmdline$i; ) &
(echo "preparing folder clone: $i") &
done
wait


this script copies a source folder $cmdline to (non-existent) destination $cmdline$i without issue at my pc with Ubuntu 16.04 but when I run same script on a cloud computer with Ubuntu 18.04, it does this:




  • duplicates source as destination (I need only this)

  • duplicates source into destination too (destination/source I don't want this)


this doesn't break any programs but I don't want to consume unnecessary cloud space.



Why would some different version of Ubuntu add a secondary copy into destination folder?



I'm using this script as



./myscript.sh 2 foldertest


this duplicates foldertest as foldertest1 and foldertest2. If first parameter is 32 then it makes 32 copies up to foldertest32.



Documentation of cp says -r is recursive copy which is I need to do full deep cloning of source folder just like copy pasting (in-place where it produces folder2 folder3 ...) in windows or graphical terminal of Ubuntu. Also I add -f to force it copy files without asking because I'm making up to 64 clones and don't want to answer 64 questions if there are already 64 clones.



Some extra info about the behaviour I need:



Folder A: a.txt b.txt c.txt



Folder A1,A2,A3,A4.. to be:



a.txt
b.txt
c.txt


but instead it is:



a.txt
b.txt
c.txt
A


normally when I copy file to file, it overwrites(if it exists). I need same overwrite behavior on folders, not copying into.










share|improve this question

























  • What do you mean with "it duplicates the source into the destination too"? I don't understand. :(

    – Tommiie
    Jan 9 at 9:34











  • I need a folder "foldera" deep copied over a new folder "folderb" which doesn't exist or not. But it does extra copy that makes "folderb/foldera" duplicating its size. It makes a new folder but that new folder contains source as a whole too. There are N files in source folder, ther are copied which is ok but it also copies source "into" destination too not just "over".

    – huseyin tugrul buyukisik
    Jan 9 at 9:36













  • Okay. Now I understand. Thanks.

    – Tommiie
    Jan 9 at 9:39











  • There must be something very simple that I miss but can't see. Most probably I'm wrong and Linux is right but where?

    – huseyin tugrul buyukisik
    Jan 9 at 9:40








  • 1





    Ok I found a duplicate stackoverflow.com/questions/23698183/…

    – huseyin tugrul buyukisik
    Jan 9 at 9:51














3












3








3


2






Edit: Found a duplicate with solution: rsync -a --delete https://stackoverflow.com/questions/23698183/how-to-force-cp-to-overwrite-directory-instead-of-creating-another-one-inside



new script that works:



#!/bin/bash

numCopy=$1
shift
cmdline="${@}"
echo "prepare folders"
for ((i=1;i<=$numCopy;i++));
do
(rsync -a --delete $cmdline/ $cmdline$i/; ) &
(echo "preparing folder clone: $i") &
done
wait




For multithreaded folder copying,



#!/bin/bash

numCopy=$1
shift
cmdline="${@}"
echo "prepare folders"
for ((i=1;i<=$numCopy;i++));
do
(cp -rf $cmdline $cmdline$i; ) &
(echo "preparing folder clone: $i") &
done
wait


this script copies a source folder $cmdline to (non-existent) destination $cmdline$i without issue at my pc with Ubuntu 16.04 but when I run same script on a cloud computer with Ubuntu 18.04, it does this:




  • duplicates source as destination (I need only this)

  • duplicates source into destination too (destination/source I don't want this)


this doesn't break any programs but I don't want to consume unnecessary cloud space.



Why would some different version of Ubuntu add a secondary copy into destination folder?



I'm using this script as



./myscript.sh 2 foldertest


this duplicates foldertest as foldertest1 and foldertest2. If first parameter is 32 then it makes 32 copies up to foldertest32.



Documentation of cp says -r is recursive copy which is I need to do full deep cloning of source folder just like copy pasting (in-place where it produces folder2 folder3 ...) in windows or graphical terminal of Ubuntu. Also I add -f to force it copy files without asking because I'm making up to 64 clones and don't want to answer 64 questions if there are already 64 clones.



Some extra info about the behaviour I need:



Folder A: a.txt b.txt c.txt



Folder A1,A2,A3,A4.. to be:



a.txt
b.txt
c.txt


but instead it is:



a.txt
b.txt
c.txt
A


normally when I copy file to file, it overwrites(if it exists). I need same overwrite behavior on folders, not copying into.










share|improve this question
















Edit: Found a duplicate with solution: rsync -a --delete https://stackoverflow.com/questions/23698183/how-to-force-cp-to-overwrite-directory-instead-of-creating-another-one-inside



new script that works:



#!/bin/bash

numCopy=$1
shift
cmdline="${@}"
echo "prepare folders"
for ((i=1;i<=$numCopy;i++));
do
(rsync -a --delete $cmdline/ $cmdline$i/; ) &
(echo "preparing folder clone: $i") &
done
wait




For multithreaded folder copying,



#!/bin/bash

numCopy=$1
shift
cmdline="${@}"
echo "prepare folders"
for ((i=1;i<=$numCopy;i++));
do
(cp -rf $cmdline $cmdline$i; ) &
(echo "preparing folder clone: $i") &
done
wait


this script copies a source folder $cmdline to (non-existent) destination $cmdline$i without issue at my pc with Ubuntu 16.04 but when I run same script on a cloud computer with Ubuntu 18.04, it does this:




  • duplicates source as destination (I need only this)

  • duplicates source into destination too (destination/source I don't want this)


this doesn't break any programs but I don't want to consume unnecessary cloud space.



Why would some different version of Ubuntu add a secondary copy into destination folder?



I'm using this script as



./myscript.sh 2 foldertest


this duplicates foldertest as foldertest1 and foldertest2. If first parameter is 32 then it makes 32 copies up to foldertest32.



Documentation of cp says -r is recursive copy which is I need to do full deep cloning of source folder just like copy pasting (in-place where it produces folder2 folder3 ...) in windows or graphical terminal of Ubuntu. Also I add -f to force it copy files without asking because I'm making up to 64 clones and don't want to answer 64 questions if there are already 64 clones.



Some extra info about the behaviour I need:



Folder A: a.txt b.txt c.txt



Folder A1,A2,A3,A4.. to be:



a.txt
b.txt
c.txt


but instead it is:



a.txt
b.txt
c.txt
A


normally when I copy file to file, it overwrites(if it exists). I need same overwrite behavior on folders, not copying into.







ubuntu cp






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Jan 9 at 9:56







huseyin tugrul buyukisik

















asked Jan 9 at 9:27









huseyin tugrul buyukisikhuseyin tugrul buyukisik

1376




1376













  • What do you mean with "it duplicates the source into the destination too"? I don't understand. :(

    – Tommiie
    Jan 9 at 9:34











  • I need a folder "foldera" deep copied over a new folder "folderb" which doesn't exist or not. But it does extra copy that makes "folderb/foldera" duplicating its size. It makes a new folder but that new folder contains source as a whole too. There are N files in source folder, ther are copied which is ok but it also copies source "into" destination too not just "over".

    – huseyin tugrul buyukisik
    Jan 9 at 9:36













  • Okay. Now I understand. Thanks.

    – Tommiie
    Jan 9 at 9:39











  • There must be something very simple that I miss but can't see. Most probably I'm wrong and Linux is right but where?

    – huseyin tugrul buyukisik
    Jan 9 at 9:40








  • 1





    Ok I found a duplicate stackoverflow.com/questions/23698183/…

    – huseyin tugrul buyukisik
    Jan 9 at 9:51



















  • What do you mean with "it duplicates the source into the destination too"? I don't understand. :(

    – Tommiie
    Jan 9 at 9:34











  • I need a folder "foldera" deep copied over a new folder "folderb" which doesn't exist or not. But it does extra copy that makes "folderb/foldera" duplicating its size. It makes a new folder but that new folder contains source as a whole too. There are N files in source folder, ther are copied which is ok but it also copies source "into" destination too not just "over".

    – huseyin tugrul buyukisik
    Jan 9 at 9:36













  • Okay. Now I understand. Thanks.

    – Tommiie
    Jan 9 at 9:39











  • There must be something very simple that I miss but can't see. Most probably I'm wrong and Linux is right but where?

    – huseyin tugrul buyukisik
    Jan 9 at 9:40








  • 1





    Ok I found a duplicate stackoverflow.com/questions/23698183/…

    – huseyin tugrul buyukisik
    Jan 9 at 9:51

















What do you mean with "it duplicates the source into the destination too"? I don't understand. :(

– Tommiie
Jan 9 at 9:34





What do you mean with "it duplicates the source into the destination too"? I don't understand. :(

– Tommiie
Jan 9 at 9:34













I need a folder "foldera" deep copied over a new folder "folderb" which doesn't exist or not. But it does extra copy that makes "folderb/foldera" duplicating its size. It makes a new folder but that new folder contains source as a whole too. There are N files in source folder, ther are copied which is ok but it also copies source "into" destination too not just "over".

– huseyin tugrul buyukisik
Jan 9 at 9:36







I need a folder "foldera" deep copied over a new folder "folderb" which doesn't exist or not. But it does extra copy that makes "folderb/foldera" duplicating its size. It makes a new folder but that new folder contains source as a whole too. There are N files in source folder, ther are copied which is ok but it also copies source "into" destination too not just "over".

– huseyin tugrul buyukisik
Jan 9 at 9:36















Okay. Now I understand. Thanks.

– Tommiie
Jan 9 at 9:39





Okay. Now I understand. Thanks.

– Tommiie
Jan 9 at 9:39













There must be something very simple that I miss but can't see. Most probably I'm wrong and Linux is right but where?

– huseyin tugrul buyukisik
Jan 9 at 9:40







There must be something very simple that I miss but can't see. Most probably I'm wrong and Linux is right but where?

– huseyin tugrul buyukisik
Jan 9 at 9:40






1




1





Ok I found a duplicate stackoverflow.com/questions/23698183/…

– huseyin tugrul buyukisik
Jan 9 at 9:51





Ok I found a duplicate stackoverflow.com/questions/23698183/…

– huseyin tugrul buyukisik
Jan 9 at 9:51










1 Answer
1






active

oldest

votes


















4














The result of cp -rf source dest is different if dest already exists.




  • If dest does not exist, it will create a copy of source with the name dest. Assuming source contains a file f1, it will create dest/f1.

  • If dest exists as a directory and if dest does not contain a file named source, it will copy source with all its contents into dest. With the example above you will get dest/source/f1.

  • If dest exists as a file and if source is a directory, the copying will fail.

  • If dest exists as a directory and if dest contains a file named source, copying will fail.

  • If dest exists as a file and if source is a file, it will overwrite dest with the contents of source.


If you want to remove any existing destination directory you could change (cp -rf $cmdline $cmdline$i; ) & into (rm -rf $cmdline$i && cp -rf $cmdline $cmdline$i; ) &.






share|improve this answer
























  • Thank you. Does rsync exist on all distros? Is cp safer to use in newly created Ubuntu images of Amazon aws for example?

    – huseyin tugrul buyukisik
    Jan 9 at 10:02













  • I think rsync is available on all distros, but you might have to install the package first. (I don't know if it gets installed by default.)

    – Bodo
    Jan 9 at 10:04











Your Answer








StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "106"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f493421%2fcp-command-does-an-extra-copying-on-different-ubuntu-version-for-folder-cloning%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









4














The result of cp -rf source dest is different if dest already exists.




  • If dest does not exist, it will create a copy of source with the name dest. Assuming source contains a file f1, it will create dest/f1.

  • If dest exists as a directory and if dest does not contain a file named source, it will copy source with all its contents into dest. With the example above you will get dest/source/f1.

  • If dest exists as a file and if source is a directory, the copying will fail.

  • If dest exists as a directory and if dest contains a file named source, copying will fail.

  • If dest exists as a file and if source is a file, it will overwrite dest with the contents of source.


If you want to remove any existing destination directory you could change (cp -rf $cmdline $cmdline$i; ) & into (rm -rf $cmdline$i && cp -rf $cmdline $cmdline$i; ) &.






share|improve this answer
























  • Thank you. Does rsync exist on all distros? Is cp safer to use in newly created Ubuntu images of Amazon aws for example?

    – huseyin tugrul buyukisik
    Jan 9 at 10:02













  • I think rsync is available on all distros, but you might have to install the package first. (I don't know if it gets installed by default.)

    – Bodo
    Jan 9 at 10:04
















4














The result of cp -rf source dest is different if dest already exists.




  • If dest does not exist, it will create a copy of source with the name dest. Assuming source contains a file f1, it will create dest/f1.

  • If dest exists as a directory and if dest does not contain a file named source, it will copy source with all its contents into dest. With the example above you will get dest/source/f1.

  • If dest exists as a file and if source is a directory, the copying will fail.

  • If dest exists as a directory and if dest contains a file named source, copying will fail.

  • If dest exists as a file and if source is a file, it will overwrite dest with the contents of source.


If you want to remove any existing destination directory you could change (cp -rf $cmdline $cmdline$i; ) & into (rm -rf $cmdline$i && cp -rf $cmdline $cmdline$i; ) &.






share|improve this answer
























  • Thank you. Does rsync exist on all distros? Is cp safer to use in newly created Ubuntu images of Amazon aws for example?

    – huseyin tugrul buyukisik
    Jan 9 at 10:02













  • I think rsync is available on all distros, but you might have to install the package first. (I don't know if it gets installed by default.)

    – Bodo
    Jan 9 at 10:04














4












4








4







The result of cp -rf source dest is different if dest already exists.




  • If dest does not exist, it will create a copy of source with the name dest. Assuming source contains a file f1, it will create dest/f1.

  • If dest exists as a directory and if dest does not contain a file named source, it will copy source with all its contents into dest. With the example above you will get dest/source/f1.

  • If dest exists as a file and if source is a directory, the copying will fail.

  • If dest exists as a directory and if dest contains a file named source, copying will fail.

  • If dest exists as a file and if source is a file, it will overwrite dest with the contents of source.


If you want to remove any existing destination directory you could change (cp -rf $cmdline $cmdline$i; ) & into (rm -rf $cmdline$i && cp -rf $cmdline $cmdline$i; ) &.






share|improve this answer













The result of cp -rf source dest is different if dest already exists.




  • If dest does not exist, it will create a copy of source with the name dest. Assuming source contains a file f1, it will create dest/f1.

  • If dest exists as a directory and if dest does not contain a file named source, it will copy source with all its contents into dest. With the example above you will get dest/source/f1.

  • If dest exists as a file and if source is a directory, the copying will fail.

  • If dest exists as a directory and if dest contains a file named source, copying will fail.

  • If dest exists as a file and if source is a file, it will overwrite dest with the contents of source.


If you want to remove any existing destination directory you could change (cp -rf $cmdline $cmdline$i; ) & into (rm -rf $cmdline$i && cp -rf $cmdline $cmdline$i; ) &.







share|improve this answer












share|improve this answer



share|improve this answer










answered Jan 9 at 9:56









BodoBodo

3536




3536













  • Thank you. Does rsync exist on all distros? Is cp safer to use in newly created Ubuntu images of Amazon aws for example?

    – huseyin tugrul buyukisik
    Jan 9 at 10:02













  • I think rsync is available on all distros, but you might have to install the package first. (I don't know if it gets installed by default.)

    – Bodo
    Jan 9 at 10:04



















  • Thank you. Does rsync exist on all distros? Is cp safer to use in newly created Ubuntu images of Amazon aws for example?

    – huseyin tugrul buyukisik
    Jan 9 at 10:02













  • I think rsync is available on all distros, but you might have to install the package first. (I don't know if it gets installed by default.)

    – Bodo
    Jan 9 at 10:04

















Thank you. Does rsync exist on all distros? Is cp safer to use in newly created Ubuntu images of Amazon aws for example?

– huseyin tugrul buyukisik
Jan 9 at 10:02







Thank you. Does rsync exist on all distros? Is cp safer to use in newly created Ubuntu images of Amazon aws for example?

– huseyin tugrul buyukisik
Jan 9 at 10:02















I think rsync is available on all distros, but you might have to install the package first. (I don't know if it gets installed by default.)

– Bodo
Jan 9 at 10:04





I think rsync is available on all distros, but you might have to install the package first. (I don't know if it gets installed by default.)

– Bodo
Jan 9 at 10:04


















draft saved

draft discarded




















































Thanks for contributing an answer to Unix & Linux Stack Exchange!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f493421%2fcp-command-does-an-extra-copying-on-different-ubuntu-version-for-folder-cloning%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Biblatex bibliography style without URLs when DOI exists (in Overleaf with Zotero bibliography)

ComboBox Display Member on multiple fields

Is it possible to collect Nectar points via Trainline?