Guest User

filecleanup.sh

a guest
Mar 21st, 2014
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 2.02 KB | None | 0 0
  1. ## Written by Julian Alistair Rutledge, November 24, 2013.
  2. ## Licensed for use under the GPL v3
  3.  
  4. #!/bin/sh
  5.  
  6. ## clean-up script for audio file dump
  7.  
  8. ## takes one argument for the path of the file dump
  9. ## i.e.:
  10. ## /usr/sbin/filecleanup.sh /path/to/audio/dump
  11.  
  12. cd $1
  13. dj_mixes="./djmixes"
  14. if [ ! -e $dj_mixes ]; then
  15. mkdir ./dj_mixes
  16. fi
  17.  
  18. touch ./filecleanup.log
  19.  
  20. timestamp=`date`
  21.  
  22. echo "******* "$timestamp" file cleanup begun *******"   >> ./filecleanup.log
  23.  
  24. for i in ./*
  25. do
  26.   file_format=`ls "$i" | tr ' ' '_' | tr '[A-Z]' '[a-z]' | cut -d'.' -f3`
  27.  
  28.   tag=`id3v2 -l "$i" | cut -d':' -f2`
  29.  
  30.   if
  31.   [ "$tag" == " No ID3 tag" ]
  32.   then
  33.     tagged="false"
  34.   else
  35.     tagged="true"
  36.   fi
  37.  
  38.   let file_size=`du -b "$i" | cut -f1`
  39.   let max_file_size=20971520
  40.  
  41.   if
  42.   [[ "$file_format" == "m4a" || "$file_format" == "mp3" || "$file_format" == "flac" || "$file_format" == "ogg" ]]
  43.   then
  44.     if [ $file_size -ge $max_file_size ]
  45.     then
  46.       mv "$i" "$dj_mixes"
  47.       echo ""$timestamp" File "$i" moved to directory "$dj_mixes"" \
  48.         >> ./filecleanup.log
  49.     elif
  50.     [[ $file_size -lt $max_file_size && $tagged == "true" ]]
  51.     then
  52.  
  53.     name_field=`id3v2 -l "$i" | grep TP1 | cut -d':' -f2`
  54.  
  55.     artist_name=`echo $name_field | tr ' ' '_' | tr '[A-Z]' '[a-z]'`
  56.  
  57.       if
  58.       [ -e ./$artist_name ]
  59.       then
  60.         mv "$i" ./$artist_name
  61.         echo ""$timestamp" File "$i" moved to directory "$artist_name"" >> ./filecleanup.log
  62.       else
  63.         mkdir ./$artist_name
  64.         mv "$i" ./$artist_name
  65.         echo ""$timestamp" Directory "$artist_name" created" >> ./filecleanup.log
  66.         echo ""$timestamp" File "$i" moved to directory "$artist_name"" >> ./filecleanup.log
  67.       fi
  68.     elif
  69.     [[ $file_size -lt $max_file_size && tagged == "false" ]]
  70.     then
  71.       echo ""$timestamp" File "$i" is not tagged. Ignoring this file." >> filecleanup.log
  72.     fi
  73.   else
  74.     echo ""$timestamp" Format of file "$i" ("$file_format") unrecognized. Ignoring this file." >> ./filecleanup.log
  75.   fi
  76. done
Advertisement
Add Comment
Please, Sign In to add comment