庆典scripting..copying文件,而不会覆盖 [英] bash scripting..copying files without overwriting
问题描述
我想知道是否可以复制/移动文件的基础上的名字由来的目的地。
I would like to know if it is possible to copy/move files to a destination based on the origin name.
基本上,我有一个/邮件文件夹,里面有几个子文件夹,如恶狗,新等。然后我在/电子邮件/主页/用户名即是重复提取的备份。 MV -f将无法正常工作,因为我没有权限覆盖范围内的目录,但只有文件。
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.
我得到的错误,如MV:无法覆盖目录`/home/username/mail/username.com
I get errors such as mv: cannot overwrite directory `/home/username/mail/username.com'
我想要做的就是在目录username.com的每个文件,将其移动到/邮件同名的文件夹。有可能是任何数量的地方username.com的文件夹,用自己的独立子sirectories。
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?
我必须这样做是由于情况下,我只能访问到我的主机通过FTP PHP和bash。
I have to do it this way as due to circumstances I only have access to my host with ftp and bash via php.
编辑:澄清
我想我需要澄清所发生的事情。我是一个共享的主机上,显然不具备这些目录本身的写权限。至少主要的,如邮件和的public_html。我做的〜/邮件备份与焦油,但试图提取时,提取到〜/电子邮件/主页/邮件等,因为我忘了完整路径。现在,我不能简单地解压缩,因为道路是错误的,我不能MV -f,因为我只需要文件,而不是目录的写权限。
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.
推荐答案
有关复制,您应该考虑使用的cpio
在及格模式( -p
)
For copying, you should consider using cpio
in 'pass' mode (-p
):
cd /mail; find . -type f | cpio -pvdmB /home/username/mail
的 -v
是冗长; -d
创建目录,必要的; -m
preserves对文件的修改时间; -B
表示使用较大的块大小,并且可以是这里无关紧要(它用于与磁带设备搞乱时有所作为)。从这个列表中省略是 -u
标志,确实无条件的复制,并覆盖目标区pre-现有文件。在 CD
命令确保路径名是否正确;如果你只是做:
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
然后复制的文件将根据中找到的/ home /用户名/邮件的/ var / spool / 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.
可以实现与(GNU)焦油
类似的效果:
You can achieve a similar effect with (GNU) tar
:
(cd /mail; tar -cf - . ) | (cd /home/username/mail; tar -xf - )
本拷贝目录,而不仅仅是文件。要做到这一点,你需要GNU的唯一设施:
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 - )
第一个独奏破折号意味着写入stdout';第二个意思是从标准输入读;在-F选项意味着读文件名从指定的文件复制。
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'.
这篇关于庆典scripting..copying文件,而不会覆盖的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!