2013-10-27 51 views
1

我一直在努力没有成功创建日期从一个文件传送到另一个OS X 10.8(山狮)使用bash。这可能是stattouch的一些组合,但我还没有弄明白,因为stat使用的格式与触摸所需的格式不匹配。转移创建日期从一个文件到另一个

这是我到目前为止所尝试的。这是其抹杀创建日期的视频转换脚本的一部分:

for f in "[email protected]" 
do 
    # convert video 
    HandBrakeCLI -i "$f" -o "/Users/J/Desktop/$(basename $f .AVI).mp4" -e x264 -q 20 -B 160 

    # read out creation date from source file 
    date_transfer=$(stat -f "%Sm" "$f")  # output e.g.: Oct 27 16:33:41 2013 

    # write creation date of source to converted file 
    touch -t $date_transfer /Users/J/Desktop/$(basename $f .AVI).mp4 # requires 201310271633 instead 
done 

回答

1

时间格式的转换可以通过date实用程序完成:

在Linux(GNU的coreutils):

$ date -d 'Oct 27 16:33:41 2013' '+%Y%m%d%H%M' 
201310271633 

在OS X(从Darwin联机手册可在网上​​采取date options):

$ date -j -f '%b %d %T %Y' 'Oct 27 16:33:41 2013' '+%Y%m%d%H%M' 
201310271633 

您的代码应该是这样的(在OS X):

for f in "[email protected]" 
do 
    # convert video 
    HandBrakeCLI -i "$f" -o "/Users/J/Desktop/$(basename "$f" .AVI).mp4" -e x264 -q 20 -B 160 

    # read out creation date from source file 
    date_transfer=$(stat -f '%Sm' "$f") 

    # write creation date of source to converted file 
    touch -t $(date -j -f '%b %d %T %Y' "$date_transfer" '+%Y%m%d%H%M') /Users/J/Desktop/$(basename "$f" .AVI).mp4 
done 

请注意报价大约$date_transfer。日期想要将日期作为一个参数,如果引号不存在,shell会将空间分割为date_transfer

0

尝试:日期-d $date_transfer +%Y%M%d%H%M

formatted_date=$(date -d$date_transfer +%Y%m%d%H%M)

touch -t $formatted_date /Users/J/Desktop/$(basename $f .AVI).mp4

+0

这将不会在OS X上工作,由于'date'实用程序选项differencies。我已经提供了更完整的答案。 – Palec

1

你并不需要打日期格式:使用touch -r refFile fileToBeChanged

你的代码是这样:

for f in "[email protected]" 
do 
    # convert video 
    HandBrakeCLI -i "$f" -o "/Users/J/Desktop/$(basename $f .AVI).mp4" -e x264 -q 20 -B 160 

    # transfer creation date of source to converted file 
    touch -r "$f" /Users/J/Desktop/$(basename $f .AVI).mp4 
done 
+0

嗨格伦,斑点。这使得脚本更简单,并避免了日期转换的混乱。谢谢。 – Jax

相关问题