Bash Remote Server File Copy Shared URL

Bash: Copying Files to Remote Server and Getting Shared URL

Introduces how to copy files to a remote server and get a URL for sharing in Bash. After copying local files to a remote server, it copies the URL to the clipboard. Convenient when you want to quickly share text files or media files. The server name is for my Xserver environment...

Shou Arisaka
3 min read
Nov 7, 2025

Introduces a program to copy files to a remote server and get a URL for sharing in Bash.

After copying local files to a remote server, it copies the URL to the clipboard. Convenient when you want to quickly share text files or media files. The server name is for my Xserver environment, so please change it appropriately.


# Copy files to remote server and get URL for sharing e.g. images,videos,files,etc
sharefile(){

  : e.g. sharefile "/mnt/e/_downloads/MOV_0070.mp4"

  # filepath="/mnt/e/_downloads/MOV_0070.mp4"
    filepath=$(realpath "$1")

  cp "${filepath}" "${filepath}.tmp"

  filedir=$(dirname "$filepath")
  filename=$(basename -- "$filepath")
  extension="${filename##*.}"

  random=$(random|chomp)

  if [[ $string =~ .*\..* ]]
  then
  mv "$filepath" "$filedir/${random}"
  newFilepath="$filedir/${random}"
  newFilename="${random}"
  touch "${filedir}/${filename} is ${random}.is.tag"
  else
  mv "$filepath" "$filedir/${random}.$extension"
  newFilepath="$filedir/${random}.$extension"
  newFilename="${random}.$extension"
  # set -x
  touch "${filedir}/${filename} is ${random}.${extension}.is.tag"
  # set +x
  fi

  rsync -e 'ssh -p 10022 -i ~/.ssh/yuis.key' -r "${newFilepath}" [email protected]:"~/yuis.xsrv.jp/public_html/data/"

  mv "${filepath}.tmp" "${filepath}"

  echo "https://yuis.xsrv.jp/data/${newFilename}" | clip && printf "\n\n${blu}https://yuis.xsrv.jp/data/${newFilename}${end} is clipped.\n\n"

}

This shell script defines a function sharefile to copy files to a remote server and get a URL for sharing. Below is a step-by-step explanation of how the script works.

  1. The sharefile function is defined. This function takes a file path as an argument.

  2. filepath=$(realpath "$1"): Gets the passed file path and stores it in $filepath. The realpath command converts relative paths to absolute paths.

  3. cp "${filepath}" "${filepath}.tmp": Copies the original file and creates a temporary file with a .tmp extension. This temporarily backs up the original file.

  4. filedir=$(dirname "$filepath"): Stores the file’s directory path in filedir.

  5. filename=$(basename -- "$filepath"): Stores the file’s base name (filename) in filename.

  6. extension="${filename##*.}": Stores the file’s extension in extension. This line performs a series of string operations to extract the extension.

  7. random=$(random|chomp): Generates a random string and stores it in the random variable. However, since the random function is not defined in the script, this line will generate an error.

  8. if [[ $string =~ .*\..* ]]: A conditional statement begins, but $string is not defined in the script, so this contains an unknown variable. The purpose of this conditional statement is unclear.

  9. mv "$filepath" "$filedir/${random}": Renames the original file to a random name. After renaming, stores the new file path in newFilepath and the new file name in newFilename. Additionally, uses the touch command to tag the file.

  10. rsync -e 'ssh -p 10022 -i ~/.ssh/yuis.key' -r "${newFilepath}" [email protected]:"~/yuis.xsrv.jp/public_html/data/": Uses the rsync command to copy the new file to the remote server. This command connects via SSH and copies the file to the specified remote directory.

  11. mv "${filepath}.tmp" "${filepath}": Restores the temporarily created backup file (with .tmp extension) to the original filename.

  12. echo "https://yuis.xsrv.jp/data/${newFilename}" | clip && printf "\n\n${blu}https://yuis.xsrv.jp/data/${newFilename}${end} is clipped.\n\n": Outputs the shared URL of the new file and copies it to the clipboard. Also displays a message, but ${blu} and ${end} represent color codes used to change the text color.

As described above, this shell script defines a function sharefile to copy files to a remote server and get a URL for sharing. This function takes a file path as an argument. To use this function, do as follows:

sharefile "/mnt/e/_downloads/MOV_0070.mp4"

Share this article

Shou Arisaka Nov 7, 2025

🔗 Copy Links