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.
-
The
sharefilefunction is defined. This function takes a file path as an argument. -
filepath=$(realpath "$1"): Gets the passed file path and stores it in$filepath. Therealpathcommand converts relative paths to absolute paths. -
cp "${filepath}" "${filepath}.tmp": Copies the original file and creates a temporary file with a.tmpextension. This temporarily backs up the original file. -
filedir=$(dirname "$filepath"): Stores the file’s directory path infiledir. -
filename=$(basename -- "$filepath"): Stores the file’s base name (filename) infilename. -
extension="${filename##*.}": Stores the file’s extension inextension. This line performs a series of string operations to extract the extension. -
random=$(random|chomp): Generates a random string and stores it in therandomvariable. However, since therandomfunction is not defined in the script, this line will generate an error. -
if [[ $string =~ .*\..* ]]: A conditional statement begins, but$stringis not defined in the script, so this contains an unknown variable. The purpose of this conditional statement is unclear. -
mv "$filepath" "$filedir/${random}": Renames the original file to a random name. After renaming, stores the new file path innewFilepathand the new file name innewFilename. Additionally, uses thetouchcommand to tag the file. -
rsync -e 'ssh -p 10022 -i ~/.ssh/yuis.key' -r "${newFilepath}" [email protected]:"~/yuis.xsrv.jp/public_html/data/": Uses thersynccommand to copy the new file to the remote server. This command connects via SSH and copies the file to the specified remote directory. -
mv "${filepath}.tmp" "${filepath}": Restores the temporarily created backup file (with.tmpextension) to the original filename. -
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"