file name comparison within specific folders and sub-folders using bash script

时间:2018-03-25 20:31:46

标签: bash

I would like to do some file name comparison with the bash script to determine the file should run a compress routine or not.

Here what I want to do, look through the UPLOAD folder and all sub-folders (couple hundreds of folders in total), if filenameA.jpg and filenameA.orig are both exist in the same folder that means it is compressed before and no need to compress it again, otherwise will compress the filenameA.jpg file.

This way only compress the newer added file and not file already compressed before.

Can someone tell me how to do the if / loop statement using bash script? I plan to run it by Cron job.

Thank you for your help.

1 个答案:

答案 0 :(得分:0)

使用find递归搜索名为* .jpg的所有文件。

对于返回的每个文件,您将检查相应的" .orig"文件,并根据结果压缩而不是。

这样的事情或许应该让你开始:

find UPLOAD -type f -name '*.jpg' | while read JPG
do
   ORIG="${JPG%.jpg}.orig"
   if [ -s ${ORIG} ]
   then
      echo "File ${JPG} already compressed to ${ORIG}"
   else
      echo "File ${JPG} need compressing ..."
      gzip -c ${JPG} > ${ORIG}
   fi
done