用Shell按照相片拍摄日期来组织目录结构

刚学了点bash的皮毛,马上就利用起来:)

拍摄的相片每次都随便乱放,时间常了,整理起来比较麻烦,一次搞PS3时,发现Sony的那个电子相册的创意不错,自动的把相片按照当时的拍摄时间给组织起来,看起来很有意思。

一句话,其实就是根据读取文件的修改日期来进行分类!

这里我的做法是先创建目录完整的目录来保存处理后的相片,然后在这个目录下利用各个文件的日期创建目录,最后把照片一个个拷贝进去。

代码截图如下,哈哈:

可打印代码:

#!/bin/bash # check the given parameters [ $# -eq 0 ] && echo 'Please specify the directory where your photoes exist!' && exit 1 [ ! -e $1 ] && echo 'The directory you specified is valid.' && exit 2 || set $1/ # make a backup directory name result_dir=`date +'%F %T' | sed 's/[-: ]/_/g'` # change the current directory to the target photo directory, # then create a directory for backing up original photoes cd $1 && mkdir "$result_dir" echo ok # iterate each photo and sort them to directories by their modification date for photo_name in `vdir | egrep '^-' | egrep -io '/w+/.(JPG|PNG|GIF|BMP)$'` do # according to the current photo's modification date generate date name new_dir=`stat -c %y $photo_name | egrep -o '^.{10}'` # make the directory if [ ! -e "./$result_dir/$new_dir" ] then mkdir -p "./$result_dir/$new_dir" fi # copy the photo to directory which name is same as the photo's modification date cp $photo_name "./$result_dir/$new_dir" done

开始运行:bash script_name.sh,之后就能得到下面的结果,第一行代表本次创建的目录,这个目录是脚本的运行起始时间,下面的两个日期文件件,才是真正的照片所属文件夹。

./2010_06_12_13_23_42
./2010_06_12_13_23_42/2010-05-16
./2010_06_12_13_23_42/2010-06-06

现在我们来用下面的命令统计下文件个数:

for myDir in `find -type d | egrep '[-][0-9]{2}'`; do echo "$myDir $((`ls -l $myDir | wc -l` - 1))"; done
结果如下:
./2010_06_12_13_23_42/2010-05-16 57
./2010_06_12_13_23_42/2010-06-06 64

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值