Rename corrupt file like FILE0000.CHK and FILE0001.CHK to the right extention

Last week a friend of me what in a huge stress because he couldn't read the music and photos anymore from his USB-stick / pen-drive.

First make a copy of all file to you localmachine (you never know). Than place this script in the same directory as the file. chmod and run.
That's all!

Here is a Linux bash script to rename the file extension depending on the file type (mime). Useful is you have a corrupt File-system on a USD-Device.

#!/bin/bash

################################################################
#
# Rename file extention by reading the file type from the file.
#                                 
#                            by Rob Groen
#                     20140522 | info@robgroen.nl
# Used to restore a damages Filesystem on USB-device
# files where named:
# FILE0000.CHK
# FILE0001.CHK
#
# After running this script the files are named like:
# FILE0000.JPG
# FILE0000.MP#
#
#      This script only renames JPG ,MP3, MPEG, QUICKTIME
#
##################################################################
# How to use?
# Place script in dir with corrupt files like FILE0000.CHK and
# FILE0001.CHK
# chmod 755 ./rename_filename_on_filedata.sh
# ./rename_filename_on_filedata.sh
#
# Now all know files are renamed with the correct extension
##################################################################
# Version 0.01 everything works
#
##################################################################

function RENAME() {
  echo "File renamed from $1 to $2"
   mv "$1" "$2"
}


for FILENAME in `ls`; do
    # echo "Reading file name:" $entry
VAR=$(file $FILENAME -i )
# --------------------------  MP3 ------------------------  
    if  [[ $VAR =~ "mpeg" ]]
    then
     NEWFILENAME=$( echo $FILENAME | tr 'CHK' 'MP3')
     RENAME $FILENAME $NEWFILENAME
    fi
   
    # --------------------------  MP3 ------------------------  
    if  [[ $VAR =~ "octet-stream" ]]
    then
     NEWFILENAME=$( echo $FILENAME | tr 'CHK' 'MP3')
     RENAME $FILENAME $NEWFILENAME
    fi
   
    # --------------------------  MP4 ------------------------
    if  [[ $VAR =~ "mp4" ]]
    then
     NEWFILENAME=$( echo $FILENAME | tr 'CHK' 'MP4')
     RENAME $FILENAME $NEWFILENAME
    fi   
   
# --------------------------  JPEG ------------------------
    if  [[ $VAR =~ "jpeg" ]]
    then
     NEWFILENAME=$( echo $FILENAME | tr 'CHK' 'JPG')
     RENAME $FILENAME $NEWFILENAME
    fi       
   
    # --------------------------  QUICKTIME ------------------------
    if  [[ $VAR =~ "quicktime" ]]
    then
     NEWFILENAME=$( echo $FILENAME | tr 'CHK' 'MOV')
     RENAME $FILENAME $NEWFILENAME
    fi 
done

Reply
The content of this field is kept private and will not be shown publicly.
CAPTCHA
Deze vraag is om te controleren of u een persoon bent. Di is om spam berichten tegen te gaan.