D
D
DarkByte20152018-02-28 16:19:05
linux
DarkByte2015, 2018-02-28 16:19:05

How to change folder name when copying?

I write "cp -r mydir anotherdir". In this case, I get "anotherdir/mydir", but how can I make it, for example, get "anotherdir/mydir2" ?
And how can I make it so that if this folder where I copy does not exist, then let it be created (including recursively - there is a folder structure). I know that you can do "mkdir -p dir1 / dir2 / dir3" but if suddenly cp has an option for this, then it would be better to do without an extra command.

Answer the question

In order to leave comments, you need to log in

3 answer(s)
X
xotkot, 2018-02-28
@xotkot

I write "cp -r mydir anotherdir". In this case, I get "anotherdir/mydir", but how can I make it, for example, get "anotherdir/mydir2" ?

if there is no mydir2 folder, then mydir will be renamed to mydir2 during copying;
if the mydir2 folder exists, then mydir will be copied to the mydir2 directory.
If you need to transfer the contents of the folder from mydir to the existing mydir2, then:
cp -r mydir/* anotherdir/mydir2

E
enabl3, 2018-02-28
@enabl3

unix-tut.blogspot.com/2008/08/cp.html

V
Victor Taran, 2018-03-20
@shambler81

find . -regex ".*\.\(jpg\|jpeg\|gif\|png\|JPG\|JPEG\|GIF\|PNG\)" -print0 | xargs -0 cp --parents --target-directory ./tmp/backup

So you can copy the pictures while maintaining the structure.
All you have to do is rename the files along the way.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question