Linux 编辑文件时保留时间戳
我想在for循环中保留我正在编辑的文件的时间戳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 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
$
符号的文件和带有双空格的文件