v-move-fs-file 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. #!/bin/bash
  2. # info: move file
  3. # options: USER SRC_FILE DST_FLE
  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_file=$2
  9. dst_file=$3
  10. # Checking arguments
  11. if [ -z "$dst_file" ]; then
  12. echo "Usage: USER SRC_FILE DST_FILE"
  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 [ ! -f "$src_file" ]; then
  28. echo "Error: source file $src_file doesn't exist"
  29. exit 3
  30. fi
  31. # Checking source path
  32. rpath=$(readlink -f "$src_file")
  33. if [ -z "$(echo $rpath |egrep "^/tmp|^$homedir")" ]; then
  34. echo "Error: invalid source path $src_file"
  35. exit 2
  36. fi
  37. # Checking destination path
  38. rpath=$(readlink -f "$dst_file")
  39. if [ -z "$(echo $rpath |egrep "^/tmp|^$homedir")" ]; then
  40. echo "Error: invalid destination path $dst_file"
  41. exit 2
  42. fi
  43. # Moving file
  44. sudo -u $user mv "$src_file" "$dst_file" >/dev/null 2>&1
  45. if [ $? -ne 0 ]; then
  46. echo "Error: file $src_file was not moved"
  47. exit 3
  48. fi
  49. # Exiting
  50. exit