【问题标题】:Linux How to copy symbolic link and preserve dateLinux 如何复制符号链接并保留日期
【发布时间】:2014-07-09 15:04:24
【问题描述】:

我有一个包含这种文件的目录:

  0 lrwxrwxrwx  1 utges_m gid36     12 May 17  2011 libedit.so -> libedit.so.2
  0 lrwxrwxrwx  1 utges_m gid36     16 Apr 16  2009 libedit.so.2 -> libedit.so.2.0.9
352 -rw-r--r--  1 utges_m gid36 358958 Mar 10  2010 libedit.so.2.0.9

我想复制文件和符号链接并保留日期。

我从这个命令开始:

  cp -dp sourceDir/* destinationDir

结果是:

  0 lrwxrwxrwx  1 siri gid33     12 Jul  9 16:38 libedit.so -> libedit.so.2
  0 lrwxrwxrwx  1 siri gid33     16 Jul  9 16:38 libedit.so.2 -> libedit.so.2.0.9
356 -rw-r--r--  1 siri gid33 358958 Mar 10  2010 libedit.so.2.0.9

所以,我写了这个简单的 bash 脚本:

 cp -dp $OLDDIR/* $NEWDIR

 ls $OLDDIR | while read f; do {
    TS=$(stat -c '%Y' "$OLDDIR/$f")
    DATE=$(date -d "UTC 1970-01-01 $TS secs")
    echo "$f  $DATE"
    touch -d "${DATE}" "$NEWDIR/$f"
 } done;

脚本输出为:

 libedit.so  Tue May 17 21:35:14 CEST 2011
 libedit.so.2  Thu Apr 16 10:30:05 CEST 2009
 libedit.so.2.0.9  Wed Mar 10 16:31:17 CET 2010

但不幸的是结果是:

  0 lrwxrwxrwx  1 siri gid33     12 Jul  9 16:55 libedit.so -> libedit.so.2
  0 lrwxrwxrwx  1 siri gid33     16 Jul  9 16:55 libedit.so.2 -> libedit.so.2.0.9
356 -rw-r--r--  1 siri gid33 358958 Mar 10  2010 libedit.so.2.0.9

我做错了什么?

我正在使用 Red Hat Enterprise Linux ES 第 4 版(Nahant 更新 3)

【问题讨论】:

    标签: linux bash rhel


    【解决方案1】:

    您的系统可能不支持更改符号链接的时间戳,因为cp -dp应该开始工作。

    此外,如果您确实使用 touch,则必须添加 (GNU touch) -h 选项来定位 链接 而不是它的目标

    来自man touch - 注意括号中的声明:

    -h, --no-dereference 影响每个符号链接而不是任何引用的文件 (仅在可以更改符号链接时间戳的系统上有用)

    试试touch -h <someSymlink>; ls -l <someSymlink>,看看原理上是否有效。

    【讨论】:

    • 非常感谢,我认为你是对的。建议的命令输出为:touch: invalid option -- h.
    • 不客气,但请确定:您是否真的在使用 GNU touchtouch --version 返回什么? invalid option 表明该选项根本不支持(不是语法的一部分),但我猜它可能已从您系统上的 GNU touch 版本中删除。跨度>
    • 这是touch --version的输出:touch (coreutils) 5.2.1uname -a的输出是:Linux itmi01vl100.milano.it 2.6.9-78.0.1.ELsmp #1 SMP Tue Jul 22 18:11:48 EDT 2008 i686 i686 i386 GNU/Linux
    • 我猜这取决于版本 4。我在“Red Hat Enterprise Linux Server 版本 6.4 (Santiago)”上尝试了 cp -dp 命令,它可以工作。再次感谢。
    • @Letizia:我的荣幸;感谢您发布您的额外发现。
    【解决方案2】:

    您可以使用 rsync 代替:

    rsync -av /folder/ /newfolder/

    示例:

    mkdir folder; ln -s /etc/hosts /tmp/folder/testfile
    
    rsync -av /tmp/folder/ /tmp/newfolder/
    sending incremental file list
    created directory /tmp/newfolder
    ./
    testfile -> /etc/hosts
    
    sent 75 bytes  received 18 bytes  186.00 bytes/sec
    total size is 10  speedup is 0.11
    
    ls -l /tmp/folder/ 
    total 0
    lrwxrwxrwx 1 tiago tiago 10 Jul  9 16:44 testfile -> /etc/hosts
    
    ls -l /tmp/newfolder/ 
    total 0
    lrwxrwxrwx 1 tiago tiago 10 Jul  9 16:44 testfile -> /etc/hosts
    

    【讨论】:

    • 很抱歉,它不起作用。我得到了与前面描述的结果相同的结果。
    • 很抱歉,-a 用于存档,它不应更改权限或修改日期,BTW cp 还提供-a 试试看。
    【解决方案3】:

    为什么要这样做?你需要的是cp -a。例如在 ~/lib 我有:

    lrwxrwxrwx   1 david david    13 May 28 05:59 libetf.so -> libetf.so.1.0
    lrwxrwxrwx   1 david david    13 May 28 05:59 libetf.so.1 -> libetf.so.1.0
    -rwxr-xr-x   1 david david  8512 May 28 05:59 libetf.so.1.0
    

    复制和保存日期:

    $ md lib2
    
    $ cp -a lib/libetf* lib2
    
    $ ls -al lib2
    total 28
    drwxr-xr-x   2 david david  4096 Jul  9 11:13 .
    drwxr-xr-x 110 david david 12288 Jul  9 11:13 ..
    lrwxrwxrwx   1 david david    13 May 28 05:59 libetf.so -> libetf.so.1.0
    lrwxrwxrwx   1 david david    13 May 28 05:59 libetf.so.1 -> libetf.so.1.0
    -rwxr-xr-x   1 david david  8512 May 28 05:59 libetf.so.1.0
    

    【讨论】:

      猜你喜欢
      • 2014-06-16
      • 1970-01-01
      • 2011-10-05
      • 1970-01-01
      • 2021-06-23
      • 2020-02-04
      • 1970-01-01
      • 2022-07-07
      • 1970-01-01
      相关资源
      最近更新 更多