How to copy files/folders and show the files/folders?

Hi,

So i know we use cp -r as a basic to copy folders/files.
I would like this BUT i would like to show the output of the files being copied.
With the amazing knowledge i have i have gone as far as this:

1)

 find source/* -exec cp -r {} target/ \;

2)

 for ObjectToBeCopied in `find source/*` ; do cp -rp $ObjectToBeCopied target/  ; echo $ObjectToBeCopied; done

I am just not successful, the command is successful but the target folder is showing files, from the source folder's sub folders, in its root folder.
If you do a test you will see what i mean.
So basically i have a folder called source and another called target.
I just want to copy the contents of the source folder to the target folder, that's it. No need to create the source folder+content within the target folder
Just the content of source in to target.

This is very simple and yet i can not accomplish this.

I know you can use rsynch , should i be ?
any unix admin would say , nah cp -rp is fine.... ?
what about cpio ? maybe find . and then passing that to cpio.
I dont know.

Anyways, i have tried what i have and not managing.

Any recommendations please.

I had a hard day so not sure what you mean... as using cp -r to copy directories , I dont because of issues with links files perms etc... and prefer using cpio or tar on the fly, now as I ma leving work I wont test what you mean to see but I will not stop you testing with cpio... e.g.

find  sourcedir  -print| cpio -pduml /<destdir> 

And say what you think of it
Will be home soon ( I hope...)

1 Like

I shall give that command a try and see if it looks good, thanks.
I will also come back to you.

---------- Post updated 10-19-16 at 11:17 AM ---------- Previous update was 10-18-16 at 11:07 PM ----------

There is a slash before the <destdir> that you mention, i am kind of worried?
Do i need that slash?

Or do i just type my "destdir" and tab which will auto complete as "destdir/"

Hello Imre,

you need to provide path there, I would prefer absolute path in there, for an example /tmp/abc/test/my_test . So if you give /abc/test/my_test because it is a directory not a mount point it will not work though if you give like abc/test/my_test should work. Please try it out into a test environment or a test directory first and once it is giving expected results then you could run it into the actual environment.

Thanks,
R. Singh

Hello MR Singh,
So i have urn that command it does exactly the same as my amazing for loop tests and cp -rp and other stuff, what a pain.

Basically they work.

I am so annoyed that i dont understand basics or dont know proper practices , i just want to do stuff the correct way. Maybe i know them and im just undermining myself i dont know.

I want to copy the files in the folder to the target folder, NOT the base folder.

so i want to copy literally a folder called "source" to "target" BUT only the content of the source folder and NOT the folder called source also.

I have tried to use

 | cut -d/ -f2 

flip i am struggling.
that cut should work but cpio is failing because it is trying to copy text and it doesnt know that it is actually objects to copy.

cpio: Error with fstatat() of "Compare", errno 2, No such file or directory
cpio: Error with fstatat() of "Compare", errno 2, No such file or directory
cpio: Error with fstatat() of "Compare", errno 2, No such file or directory
cpio: Error with fstatat() of "artest", errno 2, No such file or directory
cpio: Error with fstatat() of "artest", errno 2, No such file or directory
cpio: Error with fstatat() of "artest", errno 2, No such file or directory
target/code
cpio: Error with fstatat() of "notes.txt", errno 2, No such file or directory
cpio: Error with fstatat() of "readme.txt.txt", errno 2, No such file or directory
0 blocks
8 error(s)

Referring to vbe's post#2 and suggested command line he gives (which is the way to do it), I would comment as follows.

  1. The destination path MUST already exist before the command is run. If necessary create the destination path yourself first.
  2. If you are copying within a filesystem then don't use the 'l' switch on the cpio command otherwise it will link the files where possible and won't actually copy them (although of course the files will appear in the new path location because of the links made).
  3. Before you run the command execute a 'cd' command to the source location of the files to be copied. If you use a '.' in the find command then everything in that directory will be copied. If you only want a subset then you need to specify that subset within find. Once you have 'cd' to the source location a mirror image of the files you can see with a 'ls' command will be copied to the destination folder. An exact copy of the whole tree, sub-directories and all.
  4. If you want to see more information as the files are copied include a 'v' switch in cpio (eg, cpio -puvdm /<dest dir>)

If you're still having trouble making this work do post back your problem. We can sort it.

---------- Post updated at 05:34 PM ---------- Previous update was at 12:45 PM ----------

So, for example:

 
 # cd /a/b/c/sourcedir
 # find . -print|cpio -puvdm /d/e/f/destinationdir
 
1 Like

My way forward to complete this is indeed to actually go in to the Source folder (which does exist) and then do a loop listing the objects in that folder.... and cp and echo what i am coying... the Source Target directory does exist....
I will have to put in return code checks to make sure the change directory works, and then i can put one in for the copy as well.