I would like to know if it is possible to copy/move files to a destination based on the origin name.
Basically, I have a /mail folder, which has several subfolders such as cur and new etc. I then have an extracted backup in /mail/home/username that is a duplicate. mv -f will not work, as I do not have permission to overwrite the directories, but only the files within.
I get errors such as mv: cannot overwrite directory `/home/username/mail/username.com’
What I want to do is for each file in the directory username.com, move it to the folder of the same name in /mail. There could be any number of folders in place of username.com, with seperate sub sirectories of their own.
What is the best way to do this?
I have to do it this way as due to circumstances I only have access to my host with ftp and bash via php.
edit: clarification
I think I need to clarify what happened. I am on a shared host, and apparently do not have write access to the directories themselves. At least the main ones such as mail and public_html. I made a backup of ~/mail with tar, but when trying to extract it extracted to ~/mail/home/mail etc, as I forgot about the full path. Now, I cannot simply untar because the path is wrong, and I cannot mv -f because I only have write access to files, not directories.
For copying, you should consider using
cpioin ‘pass’ mode (-p):The
-vis for verbose;-dcreates directories as necessary;-mpreserves the modification times on the files;-Bmeans use a larger block size, and may be irrelevant here (it used to make a difference when messing with tape devices). Omitted from this list is the-uflag that does unconditional copying, overwriting pre-existing files in target area. Thecdcommand ensures that the path names are correct; if you just did:you would achieve the same result, but only by coincidence – because the sub-directory under
/home/usernamewas the same as the absolute pathname of the original. If you needed to do:then the copied files would be found under
/home/username/mail/var/spool/mail, which is unlikely to be what you had in mind.You can achieve a similar effect with (GNU)
tar:This copies directories, not just files. To do that, you need GNU-only facilities:
The first solo dash means ‘write to stdout’; the second means ‘read from stdin’; the ‘-F’ option means ‘read the file names to copy from the named file’.