【问题标题】:Replacing every file's timestamp by their older identical counterpart's将每个文件的时间戳替换为旧的相同对应文件的时间戳
【发布时间】:2016-03-11 02:26:08
【问题描述】:

如果文件在 bash 脚本的两个目录中都存在,我将如何比较两个目录的文件并替换它们的时间戳?例如,我在 Directory1 中有 file1.txt,在 Directory2 中有 file1.txt(目录作为参数给出),它们具有不同的时间戳,我想用旧的时间戳替换较新的时间戳。我该怎么做?

我已经知道我应该为此使用 touch 命令 (https://askubuntu.com/questions/62492/how-can-i-change-the-date-modified-created-of-a-file),并且还使用 -nt 和 -ot (compare file's date bash) 来比较文件,但由于我对脚本编写相当陌生,我在弄清楚如何将所有这些放在一起时遇到了一些麻烦。

【问题讨论】:

  • “我在弄清楚如何将所有这些放在一起时遇到了一些麻烦。”然后向我们展示您的尝试。你一定可以试试。阅读语法手册。 gnu.org/software/bash/manual/bash.html
  • @4ae1e1 我很乐意但不幸的是我现在正在打电话!如果仍然需要,我会在回家时发布代码。对此感到抱歉。

标签: linux bash timestamp


【解决方案1】:

备份您的文件。试试这个:

cd "$1"
for i in *; do
  if [[ -e ../$2/$i ]]; then
    if  [[ $i -ot ../$2/$i ]]; then
      touch -r "$i" "../$2/$i"
    else
      touch -r "../$2/$i" "$i"
    fi
  fi
done

【讨论】:

  • 感谢您的快速答复!如果目录作为参数传递,我是否只需将 Directory1 替换为 $1 并将 Directory2 替换为 $2?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-06-17
  • 2018-04-02
  • 2023-03-06
  • 1970-01-01
  • 1970-01-01
  • 2020-07-06
  • 1970-01-01
相关资源
最近更新 更多