Monday, December 2, 2013

Shell script: Clean up a folder based on file extension

#!/bin/bash

directory="/home/flip/Desktop"
suffix="in"

browsefolders () {
  for i in "$1"/*; 
  do
    echo "dir :$directory"
    echo "filename: $i"
    #   echo ${i#*.}
    extension=`echo "$i" | cut -d'.' -f2`
    echo "Erweiterung $extension"
    if     [ -f "$i" ]; then        

        if [ $extension == $suffix ]; then { 
            echo "$i ends with $in"
        }
        else {
            echo "$i does NOT end with $in" 
            rm -f $i; 
        } fi
  
    elif [ -d "$i" ]; then  
        browsefolders "$i"
    fi
  done
}
browsefolders  "$directory"

No comments:

Post a Comment