fant0men

Organize log files in FLAC music library (bash script)

Jun 6th, 2018
150
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #!/bin/bash
  2. # The purpose of this script is to organize the log files in my
  3. # music library. The log files are moved from empty directories to
  4. # the correct directories (containing the FLAC files). After this,
  5. # the empty directories are removed.
  6.  
  7. start="$PWD"
  8. library='/home/lucifer/Music/Songbird Music'
  9. #library='/home/lucifer/whipper/Adams'
  10.  
  11. #### Makes a backup of $IFS to $IFS_backup.
  12. IFS_backup="${IFS}"
  13.  
  14. #### Sets the Internal Field Separator variable to only
  15. #### consider newlines.
  16. IFS=$'\n'
  17.  
  18. dirs=( $(find "$library" -mindepth 2 -maxdepth 2 -type d -iname "*") )
  19.  
  20. #### Resets $IFS to its default value, as kept by $IFS_backup.
  21. IFS="${IFS_backup}"
  22.  
  23. dirs_n=${#dirs[@]}
  24.  
  25. for (( n = 0; n < dirs_n; n++ )); do
  26.  
  27.     unset -v dir
  28.     dir="${dirs[${n}]}"
  29.  
  30.     #### Sets the Internal Field Separator variable to only
  31.     #### consider newlines.
  32.     IFS=$'\n'
  33.  
  34.     files=( $(find "$dir" -type f -iname "*.flac") )
  35.  
  36.     #### Resets $IFS to its default value, as kept by $IFS_backup.
  37.     IFS="${IFS_backup}"
  38.  
  39.     files_n=${#files[@]}
  40.  
  41.     if [[ $files_n -eq 0 ]]; then
  42.  
  43.         #### Sets the Internal Field Separator variable to only
  44.         #### consider newlines.
  45.         IFS=$'\n'
  46.  
  47.         lfiles=( $(find "$dir" -type f -iname "*.log") )
  48.  
  49.         #### Resets $IFS to its default value, as kept by $IFS_backup.
  50.         IFS="${IFS_backup}"
  51.  
  52.         lfiles_n=${#lfiles[@]}
  53.  
  54.         if [[ $lfiles_n -ne 0 ]]; then
  55.  
  56.             unset -v newdir
  57.  
  58.             until [[ -d $newdir ]]; do
  59.  
  60.                 for (( c = 0; c < lfiles_n; c++ )); do
  61.                     log="${lfiles[${c}]}"
  62.                     echo -e "\nPaste the correct directory for '${log}'!\n"
  63.                 done
  64.  
  65.                 read newdir
  66.             done
  67.  
  68.             echo -e "\nMoving log files from '${dir}' to '${newdir}'!\n"
  69.  
  70.             for (( c = 0; c < lfiles_n; c++ )); do
  71.                 log="${lfiles[${c}]}"
  72.                 echo -e "\nMoving '${log}' to '${newdir}'!\n"
  73.                 mv -n "${log}" "${newdir}" || exit
  74.             done
  75.  
  76.         fi
  77.  
  78.         #echo -e "\n'${dir}' is empty!\n"
  79.         #echo -e "\nRemoving '${dir}'!\n"
  80.         #rm -rf "${dir}" || exit
  81.  
  82.     fi
  83.  
  84. done
  85.  
  86. find "$library" -type d -empty -delete
  87.  
  88. unset -v dirs files lfiles
  89.  
  90. #### Sets the Internal Field Separator variable to only
  91. #### consider newlines.
  92. IFS=$'\n'
  93.  
  94. dirs=( $(find "$library" -mindepth 2 -maxdepth 2 -type d -iname "*") )
  95.  
  96. #### Resets $IFS to its default value, as kept by $IFS_backup.
  97. IFS="${IFS_backup}"
  98.  
  99. #### Rename the log files to the proper album name.
  100. for (( n = 0; n < dirs_n; n++ )); do
  101.  
  102.     dir="${dirs[${n}]}"
  103.  
  104.  
  105.     #### Sets the Internal Field Separator variable to only
  106.     #### consider newlines.
  107.     IFS=$'\n'
  108.  
  109.     lfiles=( $(find "$dir" -type f -iname "*.log") )
  110.  
  111.     #### Resets $IFS to its default value, as kept by $IFS_backup.
  112.     IFS="${IFS_backup}"
  113.  
  114.     lfiles_n=${#lfiles[@]}
  115.  
  116.  
  117.     if [[ $lfiles_n -ne 0 ]]; then
  118.  
  119.         basedir=$(basename "${dir}")
  120.  
  121.         for (( c = 0; c < lfiles_n; c++ )); do
  122.  
  123.             log="${lfiles[${c}]}"
  124.  
  125.             lc=$(( c + 1 ))
  126.  
  127.             #newlog=$(echo "${log}" | sed "s/^[0-9]\-/${n}\-/")
  128.  
  129.             newlog="${dir}/${lc}-${basedir}.log"
  130.  
  131.             if [[ ! -f $newlog ]]; then
  132.                 echo -e "\nRenaming '${log}' to '${newlog}'!\n"
  133.                 mv -n "${log}" "${newlog}" || exit
  134.             fi
  135.         done
  136.     fi
  137.  
  138. done
RAW Paste Data