Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/image/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Bash 为文件/目录指定与另一文件/目录相同的修改日期_Bash_Unix - Fatal编程技术网

Bash 为文件/目录指定与另一文件/目录相同的修改日期

Bash 为文件/目录指定与另一文件/目录相同的修改日期,bash,unix,Bash,Unix,在基于Unix的系统中,如何将修改日期和时间从一个文件/dir“复制”到另一个文件/dir?使用;它包含几个可选标志,允许您设置这些属性。您有一些选项: 如果要更改时间,请使用touch-t STAMP-m file 如果要复制文件并希望保留时间,请使用cp--preserve=timestamps 使用touch-r设置“参考”文件的时间 如果您正在使用cp,请使用-p选项来保留mod时间。 cp-p您可以使用unix时间戳格式的stat获取源文件的时间戳,然后使用touch-d src_f

在基于Unix的系统中,如何将修改日期和时间从一个文件/dir“复制”到另一个文件/dir?

使用;它包含几个可选标志,允许您设置这些属性。

您有一些选项:

  • 如果要更改时间,请使用
    touch-t STAMP-m file
  • 如果要复制文件并希望保留时间,请使用
    cp--preserve=timestamps
  • 使用
    touch-r
    设置“参考”文件的时间

如果您正在使用cp,请使用-p选项来保留mod时间。
cp-p

您可以使用unix时间戳格式的
stat
获取源文件的时间戳,然后使用
touch-d

src_file=/foo/bar
dst_file=/bar/baz

touch -d @$(stat -c "%Y" "$src_file") "$dst_file"

注意:这仅适用于支持unix时间戳的
GNU coreutils
,该时间戳使用前缀
@
touch
为方便以后使用,请在.bashrc文件中放入以下行:

cptimestamp() {
  if [ -z $2 ] ; then
    echo "usage: cptimestamp <sourcefile> <destfile>"
    exit
  fi
  touch -d @$(stat -c "%Y" "$1") "$2"
}
cptimestamp(){
如果[-z$2];则
echo“用法:cptimestamp”
出口
fi
touch-d@$(stat-c“%Y”“$1”)“$2”
}

执行“source~/.bashrc”就可以开始了。如果您更喜欢脚本,请删除第一行和最后一行,然后在前面加上“#!/bin/sh”

这将完全满足您的要求:


touch-r“source\u file”“destination\u file”

这告诉我如何将戳记应用于特定文件,并复制文件并保留时间戳。但它并没有回答我最初的问题,即如何将时间戳从一个预先存在的文件复制到另一个预先存在的文件。。。