ln-dir 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. #!/bin/sh
  2. # Copyright © 2013 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. #### HELP MESSAGE ##############################################
  14. hlp='Usage: ln-dir SOURCE DESTINATION
  15. Make "hard link to a directory", i.e. create a whole directory structure
  16. for SOURCE dir in DESTINATION dir and make hard links for all files in
  17. new directories.'
  18. if [ "$1" = "-h" ] || [ "$1" = "--help" ]; then
  19. echo "$hlp"
  20. exit 0
  21. fi
  22. ################################################################
  23. Dir_Check()
  24. {
  25. if [ ! -d "$1" ]; then
  26. echo "$1 - is not a directory"
  27. exit 1
  28. fi
  29. }
  30. Dir_Check "$1"
  31. Dir_Check "$2"
  32. # set source and destination vars and move to source dir
  33. dest=$(cd "$2"; pwd)
  34. cd "$1"
  35. source=$(pwd)
  36. echo "HARDLINKING \"$source\" INTO \"$dest\""
  37. dest=$dest/$(basename "$source")
  38. echo -e "\n---- Making directory structure"
  39. mkdir -v "$dest"
  40. find . -mindepth 1 -type d | xargs -I === mkdir -v "$dest/==="
  41. echo -e "\n---- Making hard links"
  42. find . -type d | \
  43. while read dir; do
  44. ln -v "$dir"/* "$dest/$dir" 2> /dev/null
  45. done