本文介绍了bash脚本读取EXIF,重命名JPG文件,如果文件退出检查输出 - >创建的newfile-01.JPG的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

更新:二零一四年十月一十二日 -
固定!!
脚本查找文件夹+ all_subfolders所有JPG,JPEG,JPG文件并设置文件修改时间= EXIF​​日期/时间。
+它也写图像的大小为文件名了。 (纯只需看FILNAME知道图像的像素大小)

原文:
我一直在努力的脚本很长一段时间,拿起信息和code使用的信息痕迹。
我设法使一个脚本。在文件夹中运行它,它会拿起所有的.jpg .JPG当前和子文件夹,并将其重命名为文件名。即: 2014_10_19_12_24_05_DSC-RX100_F5.6_f37.1mm_1T250s_ISO320.jpg

问题结果
该照片可能已采取在同一时间,快的快门速度。例如:结果
2014_10_19_12_24_05_DSC-RX100_F5.6_f37.1mm_1T250s_ISO320.jpg(原始文件名P00002727.jpg)结果
2014_10_19_12_24_05_DSC-RX100_F5.6_f37.1mm_1T250s_ISO320.jpg(原始文件名P00002728.jpg)

第一个文件写入行,然后的第二个文件覆盖第一个的: - /

如果有人可以帮助结果
我想剧本有一个如果文件存在,新的文件名应该改名为具有增加的数量01,02,03..etc。在文件名的末尾。像这样的:

2014_10_19_12_24_05_DSC-RX100_F5.6_f37.1mm_1T250s_ISO320.jpg结果
2014_10_19_12_24_05_DSC-RX100_F5.6_f37.1mm_1T250s_ISO320_01.jpg(加_01,02,03等)

我使用的脚本:

 #!/斌/庆典#扩展=JPG,JPEG,PNG,GIF,PSD,BMP,CRW,THM,TIF,TIFF
文件类型=JPG,JPEG,JPG了#make正则表达式来找到在$文件类型的扩展名的文件
文件类型=* \\ \\($ {//文件类型,/ \\ |} \\)。通过所有的图片文件#循环
找 。 -iregex$文件类型-print0 |排序|而读-dS
#找 。 -iregex'。* \\ \\(JPG \\ | JPG \\ | JPEG \\)$'-print0 |而读-d $'\\ 0的
#找 。 -name* .JPG-print0 |而读-d $'\\ 0的#找 。型的F -iname* .JPG-print0 |而阅读-r -d $'\\ 0'秒;#找 。 -iname* .JPG-print0 |而读-d $'\\ 0'秒;
#for S IN $(找到-iname * .JPG -print0 |阅读的同时-d $'\\ 0'秒);

回声------------开始--------------------------回声
让计数=计数+ 1
回声文件数:$计数##跳过已包含_XX增量文件
#ffn =$ {S ## * /}#完整的文件名W / O路径
#fn =$ {FFN%。*}从分机#分割的文件名
#如果[$ {FN:-3:1}='_'];那么#我们以前增加了_XX?
##继续跳到下一个文件的'
#fi标题=`jhead的$ S`#头持有约jhead的信息
justFileName =`基名$ S| SED的/ \\(。* \\)\\ .. * / \\ 1 /'`
日期时间=`回声$头| grep的日期/时间`#日期时间获取日期时间线
FILEDATE =`回声$头| grep的文件日期`PATH =`PWD$ S/`
文件=`回声$ S| sed中的/^.//`#。 = 1第一个字符是在前面去掉
pathwithfile =`$回声路径$ file`不能查找带空格的目录
pathwithspaces =$ PWD $文件#确实查找direcotyr用空格!
JUSTPATH​​ =`目录名称$ pathwithfile`exifdateTime =`回声$日期时间| AWK -F:'{$打印2_$ 3_$ 4_$ 5_$ 6}'| awk的-F'{打印$ 1_$ 2}'`dateFileTime =`回声$ FILEDATE | awk的-F_'{打印$ 1_$ 2_$ 3_$ 4_$ 5_$ 6}'| AWK -F:'{打印$ 2_$ 3_$ 4_$ 5_$ 6}'`
#|切-c 16-34`
#datefileName =`回声$ justFileName |切-c 1-19`FULLPATH =$ pathwithspaces
#删除所有的preFIX直到/字符
FILENAME = $ {FULLPATH ## * /}
#删除所有的preFIX单元1。字符
FILEEXTENSION = $ {FILENAME ## *。}
#删除后缀,在我们的CAS,文件名,这将返回包含该文件的目录名
BASEDIRECTORY = $ {FULLPATH%$ FILENAME}#echoFULLPATH = $ FULLPATH
#echoFILENAME = $ FILENAME
#echoFILEEXTENTION = $ FILEEXTENSION
#echoBASEDIRECTORY = $ BASEDIRECTORY
#echo日期时间:$日期时间
#echoexifdateTime:$ exifdateTime
#echoFILEDATE:$ FILEDATE
#echodateFileTime:$ dateFileTime
#echopathwithfile:$ pathwithfile
#echopathwithspaces:$ pathwithspacesstripedfilename =`回声$ FILENAME |切-c 1-19`如果[$ exifdateTime = $ stripedfilename];然后
回声文件\\$ S \\已经与exifdateTime存在:$ stripedfilename
回声..skip到下一个文件
回声 - 完 - 继续
        stripedfiledate = $ FILEDATE |切-c 1-19
        如果[$ stripedfiledate = $ stripedfilename];然后
        回声文件\\$ S \\已经与FILEDATE存在:$ stripedfiledate
        回声..skip到下一个文件
        回声 - 完 -
        继续
        科幻
科幻#echo破
#打破
#如果[-z$日期时间] || [[$日期时间==日期/时间:0000:00:00 00:00:00]];
如果[$日期时间=日期/时间:0000:00:00 00:00:00]
然后        如果[-z$ FILEDATE]
        然后
                #如果日期/时间= 0000:00:00检查文件日期
                日期时间=`回声$头| grep的文件日期`
#回声日期/时间(纽约州):$ FILEDATE                从EXIF#设置时间戳
#echo文件日期(纽约州):$ exifdateTime
                jheadrun =`jhead的-autorot -exonly -ft -norot$ FULLPATH`        其他
                #设置EXIF =修改时间戳。
                jheadrun =`jhead的-dsft$ FULLPATH`#echo..fixed日期/时间:0000 ......
#echo破1
#打破        科幻#设置新的datetime
标题=`jhead的$ S`#头持有约jhead的信息
日期时间=`回声$头| grep的日期/时间`
#justFileName =`基名$ S| SED的/ \\(。* \\)\\ .. * / \\ 1 /'`
exifdateTime =`回声$日期时间| AWK -F:'{$打印2_$ 3_$ 4_$ 5_$ 6}'| awk的-F'{打印$ 1_$ 2}'`
科幻
#如果-z变量是零
#如果-n变量是无零如果[$ FILEDATE=];
然后
#echo破2
#打破#echobreake 2 - 真
        #设置EXIF =修改时间戳。
        jheadrun =`jhead的-dsft$ FULLPATH`
#回声jhead的-dsft $ jheadrun
其他
#echo破2 - 假
        #设置从文件的修改时间日期/时间
#回声日期/时间(纽约州):$ exifdateTime
        jheadrun =`jhead的-autorot -exonly -ft -norot$ FULLPATH`
#回声`jhead的-autorot -exonly -ft -norot \\$ FULLPATH \\`科幻

#检查文件确实有修改日期
# 如是
#设置日期/时间=修改日期

如果[$日期时间=];
然后
#echo破3
#打破回声breake 3 - 真
        #设置EXIF =修改时间戳。
        jheadrun =`jhead的-mkexif -dsft$ FULLPATH`
        回声jhead的-mkexif -dsft $ jheadrun
科幻如果[$ exifdateTime!=$ dateFileTime]
然后#echo破4
#打破#回声$ S
 X =`jhead的$ S| \\
 awk的'BEGIN {CMT =; }
#/文件名/ {N = $ 4; GSUB(JPG,,N);}
/相机型号/ {C = $ 4 $ $ 5 6;}
/曝光时间:/ {E = $ 3;
                   如果(E = = INT(E))E = INT(E);
                   如果(e控制1){E = INT(0.5 + 1 / E); E =1TES;}其他{E = E的; }
                 }
/ ISO equiv./ {ISO =ISO$ 4; }
/焦距/ {F =F$ 4; }
/Date.Time / {D = $ 3_$ 4; GSUB(:,_,四); }
/分辨率/ {大小= $ 3 $ $ 4 $ 5 6; }
/光圈/ {AP = $ 3; GSUB(F /,F,AP); }
/评论/ {CMT = $ 3_; }
END {打印D_C_大小_AP_F_E_ISO.JPG; }'`
commentcheck =`回声$头| grep的评论| AWK -F:'{打印$ 2}'`
如果[$ commentcheck!=Original_filename]
然后
#echo破5注释=Original_filename:$ FILENAME
commentcommand =jhead的-cl \\$评论\\
回声文件..no评论,添加; $评论< - 添加注释#echocommment:$评论
#echocommentcheck:$ commentcheck< - 如果这是空白的文件没有评论
#echocommentcommand:$ commentcommandaddcomment =$ commentcommand \\$ pathwithfile \\
#addcomment =(jhead的-cl $ $评论pathwithfile)#echoaddcomment:$ addcomment在终端的eval $#addcomment运行变量其他
继续科幻#echo破6
#打破########PADDING = 2
NEWFILE =$ BASEDIRECTORY $ X如果[[-f $ NEWFILE]];然后
    BASE =`回声$ X | SEDS / \\ JPG / _ /`
#echo $ BASE
    LAST =`LS -1$ BASEDIRECTORY| grep的$基地|排序-r |头-1`
#echo $ LAST
    LASTNUM =`回声$ {LAST:$ {#X} -3:$ PADDING} | SEDS / ^ 0 * //`
#echo $ LASTNUM
    让LASTNUM = LASTNUM + 1
#echo $ LASTNUM
    FINAL = $ BASE $(printf的0%$ PADDINGD.JPG$ {} LASTNUM)
#echo $ FINAL
    NEWFILE = $ BASEDIRECTORY $ FINAL
科幻
MV$ S,$ NEWFILE
#NODUPLICATE =jhead的-n%F-%02i \\$ pathwithspaces \\\\$ NEWFILE \\
#echoMV \\$ pathwithspaces \\\\$ NEWFILE \\回声新的文件名:$ NEWFILE#echo---
#标题=`jhead的$ NEWFILE`
#echo$头
#echo---#echo破6
#打破#echo$ S如果网络连接#END [$ exifdateTime!=$ datefileName]未设置点¯x
#echo$ X#echo睡1/100秒
睡眠0.01
#SLEEP 0.05
#SLEEP 3回声结束..--------------------------------------------- ---
回声
DONE


解决方案

  MV$ S,$ NEWFILE

使用这样的:

  PADDING = 2
回声新的文件名:
NEWFILE =$ BASEDIRECTORY $ X
如果[[-f $ NEWFILE]];然后
    BASE =`回声$ X | SEDS / \\ JPG / _ /`
    LAST =`LS -1 $ BASEDIRECTORY | grep的$基地|排序-r |头-1`
    LASTNUM =`回声$ {LAST:$ {#X} -3:$ PADDING} | SEDS / ^ 0 * //`
    让LASTNUM = LASTNUM + 1
    FINAL = $ BASE $(printf的0%$ PADDINGD.JPG$ {} LASTNUM)
    NEWFILE = $ BASEDIRECTORY $ FINAL
科幻

您可以使用of'course 计数你的脚本来处理序列号,但是这个版本不会在重新运行(安全,我认为),各显神通。

Update: 10.12.2014 - Fixed!!Script finds all jpg,jpeg,JPG files in folder+all_subfolders and sets file modification timestamp=exif date/time.+it also writes the size of image into the filename too. (neat to know pixel size of image by just looking at the filname)

Original:I been working on a script for long time, picking up info traces of info and code to use.I have managed to make a script. Run it in a folder, and it will pick up all .jpg .JPG in current and subfolders and rename them to filename. i.e: 2014_10_19_12_24_05_DSC-RX100_F5.6_f37.1mm_1T250s_ISO320.jpg

Problem
The photos may have been taken at the same time with fast shutter speed. For example:
2014_10_19_12_24_05_DSC-RX100_F5.6_f37.1mm_1T250s_ISO320.jpg (original filename P00002727.jpg)
2014_10_19_12_24_05_DSC-RX100_F5.6_f37.1mm_1T250s_ISO320.jpg (original filename P00002728.jpg)

The first file is written okay, and then the second file overwrites the first one :-/

If someone could help
I would like the script to have a "if file exist, the new filename should be renamed to having an increased number 01,02,03..etc.." at the end of the filename. Like this:

2014_10_19_12_24_05_DSC-RX100_F5.6_f37.1mm_1T250s_ISO320.jpg
2014_10_19_12_24_05_DSC-RX100_F5.6_f37.1mm_1T250s_ISO320_01.jpg (added _01,02,03 etc..)

The script I'm using:

#!/bin/bash

#extensions="jpg,jpeg,png,gif,psd,bmp,crw,thm,tif,tiff"
fileTypes="jpg,jpeg,JPG"

#make regex to find files with extensions in $fileTypes
fileTypes=".*\.\(${fileTypes//,/\|}\)"

# loop through all the image files
find . -iregex "$fileTypes" -print0 | sort | while read -d "" s
#find . -iregex '.*\.\(jpg\|JPG\|jpeg\)$' -print0 | while read -d $'\0' s
#find . -name "*.jpg" -print0 | while read -d $'\0' s

#find . -type f -iname "*.jpg" -print0 | while read -r -d $'\0' s ;

#find . -iname "*.jpg" -print0 | while read -d $'\0' s ;


#for s in $(find . -iname *.jpg -print0 | while read -d $'\0' s);
do
echo "------------  Start --------------------------"

echo ""
let counting=counting+1
echo "Number of files: $counting"

## skip files that already contain _XX increment
#ffn="${s##*/}"                                        # full filename w/o path
#fn="${ffn%.*}"                                        # split filename from ext
#if [ "${fn: -3:1}" = '_' ]; then                      # have we added '_XX' before ?
#    continue                                          # skip to next file 's'
#fi

header=`jhead "$s"`                                     # header holds jhead info about
justFileName=`basename "$s" | sed 's/\(.*\)\..*/\1/'`
dateTime=`echo "$header" | grep "Date/Time"`            # dateTime fetches Date Time line
fileDate=`echo "$header" | grep "File date"`

path="`pwd "$s"/ `"
file="`echo "$s" | sed 's/^.//'`"    # . = 1. first chars is removed in front
pathwithfile="`echo $path$file`"     # does not lookup directory with spaces
pathwithspaces="$PWD$file"           # does lookup direcotyr with spaces!
justPath=`dirname "$pathwithfile"`

exifdateTime=`echo $dateTime | awk -F ":" '{print $2"_"$3"_"$4"_"$5"_"$6}' | awk -F " " '{print $1"_"$2}'`

dateFileTime=`echo $fileDate | awk -F "_" '{print $1"_"$2"_"$3"_"$4"_"$5"_"$6}' | awk -F ":" '{print $2"_"$3"_"$4"_"$5"_"$6}'`
#| cut -c 16-34`
#datefileName=`echo $justFileName | cut -c 1-19`



FULLPATH="$pathwithspaces"
# remove all the prefix until "/" character
FILENAME=${FULLPATH##*/}
# remove all the prefix unitl "." character
FILEEXTENSION=${FILENAME##*.}
# remove a suffix, in our cas, the filename, this will return the name of the directory that contains this file
BASEDIRECTORY=${FULLPATH%$FILENAME}

#echo "FULLPATH = $FULLPATH"
#echo "FILENAME = $FILENAME"
#echo "FILEEXTENTION = $FILEEXTENSION"
#echo "BASEDIRECTORY = $BASEDIRECTORY"


#echo "dateTime: $dateTime"
#echo "exifdateTime: $exifdateTime"
#echo "fileDate: $fileDate"
#echo "dateFileTime: $dateFileTime"
#echo "pathwithfile: $pathwithfile"
#echo "pathwithspaces: $pathwithspaces"



stripedfilename=`echo $FILENAME | cut -c 1-19`

if [ $exifdateTime = $stripedfilename ]; then
echo "File \"$s\" already exists with exifdateTime: $stripedfilename"
echo "..skip to next file"
echo "--- End ---"

continue
        stripedfiledate=$fileDate | cut -c 1-19
        if [ $stripedfiledate = $stripedfilename ] ; then
        echo "File \"$s\" already exists with fileDate: $stripedfiledate"
        echo "..skip to next file"
        echo "--- End ---"
        continue
        fi
fi

#echo "break"
#break




#if [ -z "$dateTime" ] || [[ $dateTime == "Date/Time    : 0000:00:00 00:00:00" ]];
if [ "$dateTime" = "Date/Time    : 0000:00:00 00:00:00" ]
then

        if [ -z "$fileDate" ]
        then
                # If Date/Time=0000:00:00 check File date
                dateTime=`echo "$header" | grep "File date"`
#               echo "Date/Time(ny): $fileDate"

                # Set timestamp from exif
#               echo "File date(ny): $exifdateTime"
                jheadrun=`jhead -autorot -exonly -ft -norot "$FULLPATH"`

        else
                # Set exif = modified timestamp.
                jheadrun=`jhead -dsft "$FULLPATH"`

#echo "..fixed Date/Time:   0000..."
#echo "break 1"
#break

        fi



# Set new dateTime
header=`jhead "$s"`                                     # header holds jhead info about
dateTime=`echo "$header" | grep "Date/Time"`
#justFileName=`basename "$s" | sed 's/\(.*\)\..*/\1/'`
exifdateTime=`echo $dateTime |  awk -F ":" '{print $2"_"$3"_"$4"_"$5"_"$6}' | awk -F " " '{print $1"_"$2}'`


fi


# if -z variable is zero
# if -n variable is none-zero

if [ "$fileDate" = "" ] ;
then
#echo "break 2"
#break

#echo "breake 2 - True"
        # Set exif = modified timestamp.
        jheadrun=`jhead -dsft "$FULLPATH"`
#       echo "jhead -dsft $jheadrun"
else
#echo "break 2 - False"
        # Set Date/Time from File-modification timestamp
#        echo "Date/Time(ny): $exifdateTime"
        jheadrun=`jhead -autorot -exonly -ft -norot "$FULLPATH"`
#       echo "`jhead -autorot -exonly -ft -norot \"$FULLPATH\"`"

fi




#
# check file do have Modification date
# if yes
# set Date/Time = modification date
#
if [ "$dateTime" = "" ] ;
then
#echo "break 3"
#break

echo "breake 3 - True"
        # Set exif = modified timestamp.
        jheadrun=`jhead -mkexif -dsft "$FULLPATH"`
        echo "jhead -mkexif -dsft $jheadrun"
fi



if [ "$exifdateTime" != "$dateFileTime" ]
then

#echo "break 4"
#break



# echo $s
 x=`jhead "$s" | \
 awk 'BEGIN { cmt=""; }
#/File name/      { n=$4; gsub(".jpg","",n);}
/Camera model/   { c=$4$5$6;}
/Exposure time:/ { e=$3;
                   if (e==int(e)) e=int(e);
                   if (e<1) {e=int(0.5+1/e); e="1T" e "s";} else { e=e "s"; }
                 }
/ISO equiv./     { iso="ISO" $4; }
/Focal length/   { f="f" $4; }
/Date.Time /     { d=$3 "_" $4; gsub(":","_",d); }
/Resolution /    { size=$3$4$5$6; }
/Aperture /      { ap=$3; gsub("f/","F",ap); }
/Comment  /      { cmt=$3 "_"; }
END { print d "_" c "_" size "_" ap "_" f "_" e "_" iso ".jpg"; }'`


commentcheck=`echo "$header" | grep Comment | awk -F ":" '{print $2}'`


if [ "$commentcheck" != " Original_filename" ]
then
#echo "break 5"

comment="Original_filename: $FILENAME"
commentcommand="jhead -cl \"$comment\""


echo "..no comment in file, adding; $comment  <-- adding comment"

#echo "commment: $comment"
#echo "commentcheck: $commentcheck      <-- if this is blank file does not have comment"
#echo "commentcommand: $commentcommand"

addcomment="$commentcommand \"$pathwithfile\""
#addcomment=("jhead -cl $comment $pathwithfile")

#echo "addcomment: $addcomment"

eval $addcomment  # Run variable in terminal

else
continue

fi

#echo "break 6"
#break

########



PADDING=2
NEWFILE="$BASEDIRECTORY$x"

if [[ -f $NEWFILE ]]; then
    BASE=`echo $x | sed "s/\.jpg/_/"`
#echo $BASE
    LAST=`ls -1 "$BASEDIRECTORY" | grep $BASE | sort -r | head -1`
#echo $LAST
    LASTNUM=`echo ${LAST:${#x}-3:$PADDING} | sed "s/^0*//"`
#echo $LASTNUM
    let LASTNUM=LASTNUM+1
#echo $LASTNUM
    FINAL=$BASE$(printf "%0"$PADDING"d.jpg" ${LASTNUM})
#echo $FINAL
    NEWFILE=$BASEDIRECTORY$FINAL
fi


mv "$s" "$NEWFILE"
#NODUPLICATE="jhead -n%f-%02i \"$pathwithspaces\" \"$NEWFILE\""
#echo "mv \"$pathwithspaces\" \"$NEWFILE\""

echo "New filename: $NEWFILE"



#echo "---"
#header=`jhead "$NEWFILE"`
#echo "$header"
#echo "---"

#echo "break 6"
#break

#echo "$s"

fi # END OF if [ "$exifdateTime" != "$datefileName" ]

unset x
#echo "$x"

#echo "Sleeping 1/100 of a second"
sleep 0.01
#sleep 0.05
#sleep 3

echo "End..------------------------------------------------"
echo ""
done
解决方案

Before:

mv "$s" "$NEWFILE"

use something like:

PADDING=2
echo "New filename: "
NEWFILE="$BASEDIRECTORY$x"
if [[ -f $NEWFILE ]]; then
    BASE=`echo $x | sed "s/\.jpg/_/"`    
    LAST=`ls -1 $BASEDIRECTORY | grep $BASE | sort -r | head -1`
    LASTNUM=`echo ${LAST:${#x}-3:$PADDING} | sed "s/^0*//"` 
    let LASTNUM=LASTNUM+1
    FINAL=$BASE$(printf "%0"$PADDING"d.jpg" ${LASTNUM})
    NEWFILE=$BASEDIRECTORY$FINAL
fi

You can of'course use counter of your script to handle sequential numbers, but this version won't recount on re-run (safer I think).

这篇关于bash脚本读取EXIF,重命名JPG文件,如果文件退出检查输出 - &GT;创建的newfile-01.JPG的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-30 04:01