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.

  • 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
  • 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
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; ) &.

  • 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

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.