v-move-fs-directory 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. #!/bin/bash
  2. # info: move file
  3. # options: USER SRC_DIRECTORY DST_DIRECTORY
  4. #
  5. # The function moved file or directory on the file system. This function
  6. # can also be used to rename files just like normal mv command.
  7. user=$1
  8. src_dir=$2
  9. dst_dir=$3
  10. # Checking arguments
  11. if [ -z "$dst_dir" ]; then
  12. echo "Usage: USER SRC_DIRECTORY DST_DIRECTORY"
  13. exit 1
  14. fi
  15. # Checking vesta user
  16. if [ ! -e "$VESTA/data/users/$user" ]; then
  17. echo "Error: vesta user $user doesn't exist"
  18. exit 3
  19. fi
  20. # Checking user homedir
  21. homedir=$(grep "^$user:" /etc/passwd | cut -f 6 -d :)
  22. if [ -z $homedir ]; then
  23. echo "Error: user home directory doesn't exist"
  24. exit 12
  25. fi
  26. # Checking source file
  27. if [ ! -d "$src_dir" ]; then
  28. echo "Error: source directory $src_dir doesn't exist"
  29. exit 3
  30. fi
  31. # Checking source path
  32. rpath=$(readlink -f "$src_dir")
  33. if [ -z "$(echo $rpath |egrep "^/tmp|^$homedir")" ]; then
  34. echo "Error: invalid source path $src_dir"
  35. exit 2
  36. fi
  37. # Checking destination path
  38. rpath=$(readlink -f "$dst_dir")
  39. if [ -z "$(echo $rpath |egrep "^/tmp|^$homedir")" ]; then
  40. echo "Error: invalid destination path $dst_dir"
  41. exit 2
  42. fi
  43. # Moving directory
  44. sudo -u $user mv "$src_dir" "$dst_dir" >/dev/null 2>&1
  45. if [ $? -ne 0 ]; then
  46. echo "Error: directory $src_dir was not moved"
  47. exit 3
  48. fi
  49. # Exiting
  50. exit