copyinorder 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. #!/bin/sh
  2. # Copyright © 2012 Alex Kost
  3. # This program is free software; you can redistribute it and/or modify
  4. # it under the terms of the GNU General Public License as published by
  5. # the Free Software Foundation, either version 3 of the License, or
  6. # (at your option) any later version.
  7. # This program is distributed in the hope that it will be useful,
  8. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. # GNU General Public License for more details.
  11. # You should have received a copy of the GNU General Public License
  12. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  13. DirCheck() {
  14. if [ ! -d "$1" ]; then
  15. echo "$1 - is not a directory"
  16. exit 1
  17. fi
  18. }
  19. DirCheck "$1"
  20. DirCheck "$2"
  21. # Getting destination variable and moving to the source directory.
  22. pushd "$2" > /dev/null
  23. dest=$(pwd)
  24. popd > /dev/null
  25. cd "$1"
  26. source=$(pwd)
  27. echo "COPYING \"$source\" INTO \"$dest\""
  28. dest=$dest/$(basename "$source")
  29. echo -e "\n---- Making directory structure"
  30. mkdir -v "$dest"
  31. find . -type d | sed 1d | sort | xargs -I === mkdir -v "$dest/==="
  32. filesAmount=$(find . -type f | wc -l)
  33. echo -e "\n---- Copying files (total $filesAmount):"
  34. num=0
  35. # TODO Do not hardcode "mp3"; make a script option to pass to "find".
  36. find . -type f -name '*.mp3' | sort | while read fl
  37. do
  38. let num+=1 #num=$(($num+1))
  39. echo "($num of $filesAmount): $dest/$fl"
  40. cp "$fl" "$dest/$fl"
  41. done
  42. echo -e "\nThe job has been done."