如何使用 Perl 或 shell 以非递归方式迁移目录?
我们正在将主文件夹迁移到新的文件系统,我正在寻找一种使用 Perl 或 shell 脚本实现自动化的方法。 我在编程语言方面没有太多选择,因为系统是专有存储集群,应尽可能保持不变。
任务:在目录 /home/ 下,我有各种用户的主文件夹 aaa、bbb、ccc...,它们具有某些权限和用户/组所有权,在迁移到 /newhome/ 时需要保持不变。 以下是需要从 /home 迁移的示例:
drwxr-xr-x 3 aaaaa xxxxxxxxx 4096 Feb 26 2008 aaaaa/
drwxrwxrwx 88 bbbbbbb yyyyyy 8192 Dec 16 16:32 bbbbbbb/
drwxr-xr-x 6 ccccc yyyyyy 4096 Nov 24 04:38 ccccc/
drwxr-xrwx 36 dddddd yyyyyy 4096 Jun 20 2008 dddddd/
drwxr-xr-x 27 eee yyyyyy 4096 Dec 16 02:56 eee/
因此,应在 /newhome 下创建具有权限和所有权的完全相同的文件夹。 复制/移动文件不应该是一个问题,因为它将在稍后处理。
有人创作过这样的剧本吗? 我对 Perl 很陌生,所以我需要帮助。
We're migrating home folders to a new filesystem, and I am looking for a way to automate it using Perl or a shell script. I don't have much choice in programming languages as the systems are proprietary storage clusters that should remain as unchanged as possible.
Task: Under directory /home/ I have various users' home folders aaa, bbb, ccc, ... and they have certain permissions and user/group ownership that need to remain intact upon migration to /newhome/. Here's example of what needs to be migrated from /home:
drwxr-xr-x 3 aaaaa xxxxxxxxx 4096 Feb 26 2008 aaaaa/
drwxrwxrwx 88 bbbbbbb yyyyyy 8192 Dec 16 16:32 bbbbbbb/
drwxr-xr-x 6 ccccc yyyyyy 4096 Nov 24 04:38 ccccc/
drwxr-xrwx 36 dddddd yyyyyy 4096 Jun 20 2008 dddddd/
drwxr-xr-x 27 eee yyyyyy 4096 Dec 16 02:56 eee/
So, exact same folders with permissions and ownerships should be created under /newhome. Copying/moving files should not be a concern, as it will be handled later.
Anyone has worked on such script? I am really new to Perl, so I need help.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
cp 的 -a 标志将维护权限、修改时间等。您应该能够执行以下操作:
在一个目录上尝试一下,看看在自动化之前是否满足您的需要。
编辑:您可以使用 Paul 提到的 rsync 或 tar 来禁用递归文件复制。 使用 rsync,子目录仍被保留,但文件不会被复制:
我还没有尝试过 tar 的 --no-recursion,所以无法对其发表评论。
编辑2:另一种方式
参考
cp
's -a flag will maintain permission, modification times etc. You should for be able to do something like:Try it with one directory to see if does what you need before automating it.
EDIT: You can disable recursive file copying by using rsync or tar as mentioned by Paul. With rsync, subdirectories are still preserved, but files aren't copied:
I haven't tried tar's --no-recursion, so can't comment on it.
EDIT 2: Another way
Reference
仅当您以
root
身份执行复制操作时,才能保留所有者和组。 大多数给出的命令都可以工作 -tar
和cp -rp
选项可以。唯一需要担心的技巧是不可写目录,但这对于非 root 用户来说是一个问题。 然后,我倾向于使用:
-深度
选项意味着文件和子目录在目录本身之前被处理,因此只有在目录的所有内容之后才设置目录的不可写权限。目录已复制到其中。 您还可以使用“sort -r
”以相反的顺序列出文件,这可确保目录显示在其内容之后。You can only preserve the owner and group if you do the copying operation as
root
. Most of the commands given will work - thetar
and thecp -rp
options will.The only trick to worry about is non-writable directories, but that's an issue for non-root users. Then, I tend to use:
The
-depth
option means that file and sub-directories are processed before the directories themselves, so the no-write permission on the directory is only set after all the contents of the directory have been copied into it. You can also use a 'sort -r
' to list files in reverse order, which ensures that directories appear after their contents.这将创建目录并复制所有文件。
如果您不想复制所有文件,可以通过在第一个 tar 命令中添加“--no-recursion”来实现。
This will create the directories and copy all the files.
If you don't want to copy all the files, you might be able to do that by adding a "--no-recursion" to the first tar command.
如果这些目录位于同一文件系统上,为什么不简单地
If these directories are on the same filesystem, why not simply