MFlenses - 数码时代的新生
MFLenses 2026

手动对焦镜头论坛

通过维修记录、实拍样片和图文丰富的档案帖,探索老镜头的世界。

通过维修记录、实拍样片和图文丰富的档案帖,探索老镜头的世界。
精选

论坛首页的合作伙伴展示位。

Bash recursive file rename [for Linux nerds]
查看上一个主题 :: 查看下一个主题  


Reply

I just passed the 10.000th picture and my Samsung has reset his counter ... which means that the next file to sg1s9999 is ... sg1s0001.

I decided to rename old files with 6 digits (allowing 999999 pictures, yes, I know, I'm a bit too enthusiastic), renaming sg1s0001 to jf010001 and, while at it, extracting jpeg preview from raw files.

Here is my bash script (largely inspired by another script found on the Net) :

代码:

#!/bin/sh

rename_recursive()
{
   for file in "$1"/*; do
      if [ -d "$file" ]; then
         rename_recursive "$file"
      else
         dirname=`dirname "$file"`
         basename=`basename "$file"`

         singlename=${basename%.*}
        beg_name=${basename:0:4}
         extension=${basename##*.}

    # If file begins in sg1s, meaning a Samsung GX1s file
    if [[ "$beg_name" == "sg1s" ]]; then

         # If file ends in JPG
       if [[ "$extension" == "jpg" ]]; then
             newfile="$dirname/jf01${singlename:4:10}.jpg"
             if [[ "$file" != "$newfile" && ! -f "$newfile" ]]; then
                echo "Renaming '$file' to '$newfile'..."
                mv "$file" "$newfile"
             fi
      fi

           # If file ends in PEF (Raw Pentax file)
           if [[ "$extension" == "pef" ]]; then
             newfile="$dirname/jf01${singlename:4:10}.pef"
             if [[ "$file" != "$newfile" && ! -f "$newfile" ]]; then
                echo "Renaming '$file' to '$newfile'..."
                mv "$file" "$newfile"
             fi
        /usr/local/sbin/dcraw -e -h -v -a $newfile   
           fi

         # If file ends in XCF (if I saved a raw with layers)
           if [[ "$extension" == "xcf" ]]; then
             newfile="$dirname/jf01${singlename:4:10}.xcf"
             if [[ "$file" != "$newfile" && ! -f "$newfile" ]]; then
                echo "Renaming '$file' to '$newfile'..."
                mv "$file" "$newfile"
             fi
           fi

         # If file ends in PNG (you never know :) )
           if [[ "$extension" == "png" ]]; then
             newfile="$dirname/jf01${singlename:4:10}.png"
             if [[ "$file" != "$newfile" && ! -f "$newfile" ]]; then
                echo "Renaming '$file' to '$newfile'..."
                mv "$file" "$newfile"
             fi
           fi


    fi
      fi 
   done
}

for dir in $@; do
   dirpath="$PWD/$dir"
   if [ -d "$dirpath" ]; then
      echo "Dir: $dirpath"
      rename_recursive "$dirpath"
   fi
done


It could be more elegant but it does the trick.


Reply

As a fellow LInux nerd (I collect distributions like I collect MF lenses) Thanks!

Doug.


Jieffe 写道:
I just passed the 10.000th picture and my Samsung has reset his counter ... which means that the next file to sg1s9999 is ... sg1s0001.

I decided to rename old files with 6 digits (allowing 999999 pictures, yes, I know, I'm a bit too enthusiastic), renaming sg1s0001 to jf010001 and, while at it, extracting jpeg preview from raw files.

Here is my bash script (largely inspired by another script found on the Net) :

代码:

#!/bin/sh

rename_recursive()
{
   for file in "$1"/*; do
      if [ -d "$file" ]; then
         rename_recursive "$file"
      else
         dirname=`dirname "$file"`
         basename=`basename "$file"`

         singlename=${basename%.*}
        beg_name=${basename:0:4}
         extension=${basename##*.}

    # If file begins in sg1s, meaning a Samsung GX1s file
    if [[ "$beg_name" == "sg1s" ]]; then

         # If file ends in JPG
       if [[ "$extension" == "jpg" ]]; then
             newfile="$dirname/jf01${singlename:4:10}.jpg"
             if [[ "$file" != "$newfile" && ! -f "$newfile" ]]; then
                echo "Renaming '$file' to '$newfile'..."
                mv "$file" "$newfile"
             fi
      fi

           # If file ends in PEF (Raw Pentax file)
           if [[ "$extension" == "pef" ]]; then
             newfile="$dirname/jf01${singlename:4:10}.pef"
             if [[ "$file" != "$newfile" && ! -f "$newfile" ]]; then
                echo "Renaming '$file' to '$newfile'..."
                mv "$file" "$newfile"
             fi
        /usr/local/sbin/dcraw -e -h -v -a $newfile   
           fi

         # If file ends in XCF (if I saved a raw with layers)
           if [[ "$extension" == "xcf" ]]; then
             newfile="$dirname/jf01${singlename:4:10}.xcf"
             if [[ "$file" != "$newfile" && ! -f "$newfile" ]]; then
                echo "Renaming '$file' to '$newfile'..."
                mv "$file" "$newfile"
             fi
           fi

         # If file ends in PNG (you never know :) )
           if [[ "$extension" == "png" ]]; then
             newfile="$dirname/jf01${singlename:4:10}.png"
             if [[ "$file" != "$newfile" && ! -f "$newfile" ]]; then
                echo "Renaming '$file' to '$newfile'..."
                mv "$file" "$newfile"
             fi
           fi


    fi
      fi 
   done
}

for dir in $@; do
   dirpath="$PWD/$dir"
   if [ -d "$dirpath" ]; then
      echo "Dir: $dirpath"
      rename_recursive "$dirpath"
   fi
done


It could be more elegant but it does the trick.
Smile Smile


Reply

nemesis101 写道:
As a fellow LInux nerd (I collect distributions like I collect MF lenses)

I'm a slackware fan for years (just switched to Zenwalk, but hey, it's still slackware-ish). I managed to get rid of Windows Smile No more dual-boot since years !