Advertisement
MestreLion

cpdeep - cp that creates destination path

Aug 6th, 2011
223
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 2.73 KB | None | 0 0
  1. #!/bin/bash
  2. #
  3. # cp-deep - Copy file from SOURCE(s) to DESTINATION, creating path if needed
  4. #
  5. #    Copyright (C) 2011 Rodrigo Silva (MestreLion) <[email protected]>
  6. #
  7. #    This program is free software: you can redistribute it and/or modify
  8. #    it under the terms of the GNU General Public License as published by
  9. #    the Free Software Foundation, either version 3 of the License, or
  10. #    (at your option) any later version.
  11. #
  12. #    This program is distributed in the hope that it will be useful,
  13. #    but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. #    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  15. #    GNU General Public License for more details.
  16. #
  17. #    You should have received a copy of the GNU General Public License
  18. #    along with this program.  If not, see <http://www.gnu.org/licenses/>.
  19. #
  20. # Huge thanks to all the gurus and friends in irc://irc.freenet.org/#bash
  21. # and the contributors of http://mywiki.wooledge.org/
  22. # Special thanks to dualbus, geirha and sn18.
  23. #
  24. # Description:
  25. #
  26. # cp-deep works like regular cp, copying files from SOURCE(S) to DESTINATION
  27. # But it also creates destination path if it does not exist - using mkdir
  28. # with --parent DEST - thus allowing copied files to be "deep" in the tree
  29. #
  30. #
  31. # USE WITH CAUTION! PAY ATTENTION TO TRAILLING "/" IN DEST!!!
  32. #
  33. #
  34. # Original cp allows optional use of trailing "/" in DEST,
  35. # as it checks if DEST is an existing dir to decide whether DEST means
  36. # "copy file to /foo/bar" or "copy to /foo dir and rename file to bar"
  37. #
  38. # Since cpdeep allows DEST dir to be created, one must explicitly use
  39. # trailing "/" in DEST when dir is to be created and file not renamed. So:
  40. #
  41. # cp-deep file /foo/bar/ = create /foo/bar path and copy file there = /foo/bar/file
  42. # cp-deep file /foo/bar  = create /foo path and copy file as bar = /foo/bar
  43. #
  44. # To avoid common errors, explicit use of trailing "/" is not needed when:
  45. # - SOURCE are multiple files (DEST assumed to be dir)
  46. # - DEST exists and is a dir = copy SOURCE there, do not rename
  47. # - DEST exists and is a file = copy SOURCE over DEST (SOURCE must be single file)
  48. #
  49. # And it is vital to properly use "/" when:
  50. # - SOURCE is single file and DEST does not exist
  51. #   ( no trailing "/" in DEST will rename SOURCE)
  52.  
  53. source "$CUSTOM_SCRIPTS/lib/common"
  54.  
  55. (( $# >= 2 )) || fatal "missing parameters. usage: $SELF SOURCE... DEST\n"
  56.  
  57. dest="${@:(-1)}" # Last argument
  58.  
  59. # Special handling of DEST only needed when SOURCE is a single file
  60. # and there is at least one "/" in it. For same dir copies, no mkdir needed
  61. [[ $# = 2 ]] && { [[ "$dest" = */* ]] && dest="${dest%/*}/" || dest="" ; }
  62.  
  63. [[ "$dest" ]] && { mkdir --parent -- "$dest" || exit ; }
  64.  
  65. cp --no-preserve=mode,ownership "$@"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement