我有许多文件想要更改linux中的修改日期。修改日期保存在文件名中。
所以我有一些文件,比如"IMG_20180101_010101.jpg",但是修改日期是今天。我想将修改日期更改为2018-01-01 :01:01: 01:01:01,如文件名中所示。我试过寻找和触摸:
find . -iname 'IMG*' -print | while read filename; do touch -t {filename:7:8} "$filename"; done当我这样做时,我总是会得到一个错误(“无效的日期格式:{filename:7:8})。
我做错了什么?
发布于 2018-05-04 11:32:07
如果要根据文件名设置文件时间戳,可以尝试以下操作:
find -type f -name "IMG_*" -exec bash -c 'touch -t $(sed "s/.*IMG_\([0-9]\{8\}\)_\([0-9]\{4\}\)\([0-9]\{2\}\).jpg$/\1\2.\3/" <<< "$1") "$1"' _ {} \;正如在touch手册页面中提到的那样,选项-t需要一个格式[[CC]YY]MMDDhhmm[.ss]。这就是sed命令的目的。
发布于 2018-05-04 11:45:39
如果我理解得很好,你可以用你自己的格式写文件名。那么这个脚本呢:
#!/bin/bash
suffix=".jpg"
for file in "IMG*"; do # Careful, the loop will break on whitespace
fileDate=$(echo $file| cut -d'_' -f 2)
year=${fileDate:0:4}
month=${fileDate:4:2}
day=${fileDate:6:2}
fileHour=$(echo $file| cut -d'_' -f 3 | sed -e s/$suffix//)
hour=${fileHour:0:2}
min=${fileHour:2:2}
secs=${fileHour:4:2}
newName="$year-$month-$day $hour:$min:$secs$suffix"
mv $file "$newName"
donehttps://stackoverflow.com/questions/50173086
复制相似问题