tags:

views:

1459

answers:

6

Hello,

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.

A: 

I'm not entirely clear on what it is that you want to do, but you could try the following:

for file in /mail/*; do
    mv -f $file /home/username/mail/$(basename $file)
done

This will move every file and subdirectory in /mail from there into /home/username/mail.

Adam Rosenfield
I think that is the reverse of what I want to do? I extracted a mail archive into the mail folder, but it made a subdirectory under mail which I can now not restore, because I cannot overwrite the original directories
Joshxtothe4
A: 

For copying, you should consider using cpio in 'pass' mode (-p):

cd /mail; find . -type f | cpio -pvdmB /home/username/mail

The -v is for verbose; -d creates directories as necessary; -m preserves the modification times on the files; -B means 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 -u flag that does unconditional copying, overwriting pre-existing files in target area. The cd command ensures that the path names are correct; if you just did:

find /mail -type f | cpio -pvdmB /home/username

you would achieve the same result, but only by coincidence - because the sub-directory under /home/username was the same as the absolute pathname of the original. If you needed to do:

find /var/spool/mail -type f | cpio -pvdmB /home/username/mail

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:

(cd /mail; tar -cf - . ) | (cd /home/username/mail; tar -xf - )

This copies directories, not just files. To do that, you need GNU-only facilities:

(cd /mail; find . -type f | tar -cf - -F - ) | (cd /home/username/mail; tar -xf - )

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'.

Jonathan Leffler
A: 

Is using tar an option? You could tar up the directory, and extract it under /mail/ (for I am assuming that is what you want roughly) with tar overwriting existing files and directories.

ayaz
That is what I tried, the problem is I can only overwrite files, not directories
Joshxtothe4
A: 

I'm a bit confused about what it is exactly that you want to do. But you should be able to use the approach of Adam's solution and redirect the errors to a file.

for file in /mail/*; do
    mv -f $file /home/username/mail/$(basename $file) 2> /tmp/mailbackup.username.errors
done

DIrectories will not be overwritten and you can check the file so that it only contaions errors you anticipate.

PEZ
The problem is that I can not overwrite directories, only files, and my mail archive is currently in a subdirectoy of mail. restoring it seems impossible using normal tools.
Joshxtothe4
Yes, but I think if you try something like what I suggest you'll get all the files moved and any error messages from npt being able to overwrite directories will be redirected to the log file.
PEZ
A: 

Can you untar it again? The -P option to tar will not strip leading "/", so the absolute pathnames will be respected. From your edit, it sounds like this'll fix it.

Cirno de Bergerac
I tried that, it gives the error: tar: /home/username/mail: Not found in archive after trying with tar xzvfP ~/new/moremail.tgz ~/mail
Joshxtothe4
Er, that means you only want the item "~/mail" in the archive to be extracted.
Cirno de Bergerac
I used -P in the root directory, and /mail is also in the root directory. It just extracted the folder /home/username/mail, I cannot get it to extract just the mail folder..
Joshxtothe4
A: 

Even with your clarification I'm still having a problem understanding exactly what you're doing. However, any chance you can use rsync? The src and dest hosts can be the same host for rsync. As I recall, you can tell rsync to only update files that already exist in the destination area (--existing) and also to ignore directory changes (--omit-dir-times).

Again, I'm not quite understanding your needs here, but rsync is very flexible in backing up files and directories.

Good luck.

shank
Aah, I see you only have access to ftp and bash. Well, nevermind I guess.
shank