Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/linux/22.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
Linux 编辑文件时保留时间戳_Linux_Bash_Shell_Date_String Formatting - Fatal编程技术网

Linux 编辑文件时保留时间戳

Linux 编辑文件时保留时间戳,linux,bash,shell,date,string-formatting,Linux,Bash,Shell,Date,String Formatting,我想在for循环中保留我正在编辑的文件的时间戳 for files in $DIR/MDC*/$FILE do # Need to get date here! dos2unix $files if grep -q $TYPE $files; then echo 'done' else sed -i "1s/^/$TYPE\n/" $files fi $DIR/crccalc/LDDR16.py $files

我想在for循环中保留我正在编辑的文件的时间戳

for files in $DIR/MDC*/$FILE
do
    # Need to get date here!
    dos2unix $files
    if grep -q $TYPE $files; then
        echo 'done'
    else
        sed -i "1s/^/$TYPE\n/" $files
    fi
    $DIR/crccalc/LDDR16.py $files
    # Use Date variable here to change it back

done
问题是我需要从文件中获取一个格式化的日期字符串,以便在循环完成后执行
touch-r
将文件日期还原回来

stat
没有给我所需的格式

所需格式:


yymddhhmm

有一个很好的技巧:使用
touch-r reference_文件
。也就是说,使用另一个文件的时间戳作为参考来触摸该文件

手动触摸

   -r, --reference=FILE
          use this file's times instead of current time
你可能会问:这对你有什么帮助?好的,因为您可以创建一个虚拟文件
dummy
来处理:

  • 在修改文件之前,请触摸带有要修改的文件
    原始文件
    时间戳的
    虚拟
  • 您可以修改
    原始\u文件
  • 然后使用
    虚拟
    文件的时间戳触摸
    原始
总而言之:

for files in $DIR/MDC*/$FILE
do
    # copy its timestamp to `dummy_file`
    touch -r "$files" "dummy_file"

    # ...things...

    # Use Date variable here to change it back
    touch -r "dummy_file" "$files"
done

这里有一个很好的技巧:使用
touch-r reference\u文件
。也就是说,使用另一个文件的时间戳作为参考来触摸该文件

手动触摸

   -r, --reference=FILE
          use this file's times instead of current time
你可能会问:这对你有什么帮助?好的,因为您可以创建一个虚拟文件
dummy
来处理:

  • 在修改文件之前,请触摸带有要修改的文件
    原始文件
    时间戳的
    虚拟
  • 您可以修改
    原始\u文件
  • 然后使用
    虚拟
    文件的时间戳触摸
    原始
总而言之:

for files in $DIR/MDC*/$FILE
do
    # copy its timestamp to `dummy_file`
    touch -r "$files" "dummy_file"

    # ...things...

    # Use Date variable here to change it back
    touch -r "dummy_file" "$files"
done

可以直接从终端运行的另一次尝试

首先执行第一步,然后开始处理文件

  • 保留旧的时间戳。 它从当前目录运行,排除所有隐藏文件,并将其保存到
    /tmp/files
    中的临时文件中。您可以随时更改参数,但最好继续使用
    -printf'%t“%p”\n'
    ,因为后面的
    touch
    命令利用了这一点

    find-在名称“*”-printf'%t”“%p”“\n'-type f>/tmp/files

  • 随意修改文件

  • 现在创建一个文件,帮助您恢复时间戳:

    读行时
    ;do echo touch-a-d$line>>job.sh;完成

  • 最后将旧日期应用于修改过的文件

    sh job.sh


  • 警告:适用于具有名称间距、特殊字符的文件,但例如,没有带有
    $
    符号的文件和带有双空格的文件。

    另一种可以直接从终端运行的尝试

    首先执行第一步,然后开始处理文件

  • 保留旧的时间戳。 它从当前目录运行,排除所有隐藏文件,并将其保存到
    /tmp/files
    中的临时文件中。您可以随时更改参数,但最好继续使用
    -printf'%t“%p”\n'
    ,因为后面的
    touch
    命令利用了这一点

    find-在名称“*”-printf'%t”“%p”“\n'-type f>/tmp/files

  • 随意修改文件

  • 现在创建一个文件,帮助您恢复时间戳:

    读行时
    ;do echo touch-a-d$line>>job.sh;完成

  • 最后将旧日期应用于修改过的文件

    sh job.sh

  • 警告:适用于具有名称间距、特殊字符的文件,但例如,没有带有
    $
    符号的文件和带有双空格的文件