[Solved] Help for bash script to move savegames created by a game to my personal cloud and make a soft link back.

Quick to answer questions about finding your way around Linux Mint as a new user.
Forum rules
There are no such things as "stupid" questions. However if you think your question is a bit stupid, then this is the right place for you to post it. Stick to easy to-the-point questions that you feel people can answer fast. For long and complicated questions use the other forums in the support section.
Before you post read how to get help. Topics in this forum are automatically closed 6 months after creation.
Locked
Malik
Level 1
Level 1
Posts: 30
Joined: Sun Mar 17, 2019 12:04 am

[Solved] Help for bash script to move savegames created by a game to my personal cloud and make a soft link back.

Post by Malik »

Hi, this is a Linux question per-se and not Mint-specific.

I am using Resilio Sync to sync files with my home NAS, I actively use my NAS as my personal cloud game save service for syncing the DOSBOX and Windows 9x-era game saves across my laptops and desktop for continue playing my favourite games wherever I am.

For most DOS games, I can simply save the whole game directory in my NAS and a local directory in my active system, and whenever I save a game, it automatically saved in the synced directory which will update the savegame file into my NAS.

For larger games and those games which have a separate savegame directory, it is easy to upload only the savegame folder. But some games save the files directly into the working directory and I don't want to upload the whole directory to my NAS to sync only the savegame files.

I would like to create a bash script for use with my savegame files, and here is what I would like to do:


The game ( now in question, is Quest for Glory 2 VGA remake by AGDI) saves the savegame files directly into the working directory.

I would like to create a bash script to check any newly created save files and move them to my NAS-synced local folder, and then soft link these files back into my working game directory.

How can I go about accomplishing this task? (I know how to soft link, but would like to know how to detect new save game files every time the game creates and only move these latest files to the NAS synced folder and soft link them back?

I don't want to test for and move the already synced previous save games. Only the new one.

I can run the script once I exit the game.

For easy readability :

1. Detect if any new save game files has been created. (The game saves in a fixed format - agssave.001.Qfg2sav, agssave.002.Qfg2Sav... and so on.)
2. Move it/them to my NAS-synced local directory.
3. Soft link back these newly created files back to the working game directory.
4. Don't touch already synced save game files created before. (They are already being synced.)

Thank you for any help!
Cheers.
Last edited by LockBot on Wed Dec 28, 2022 7:16 am, edited 2 times in total.
Reason: Topic automatically closed 6 months after creation. New replies are no longer allowed.
User avatar
ugly
Level 5
Level 5
Posts: 592
Joined: Thu Nov 24, 2016 9:17 pm

Re: Help for bash script to move savegames created by a game to my personal cloud and make a soft link back.

Post by ugly »

I think this should be relatively easy to do. Because you know that the previously synced files are going to be symlinks, you should be able to use the find command and limit it to actual files and those will be the new save games.

I think something like this would work:

Code: Select all

#!/bin/bash

for f in $(find /path/to/QFG2Folder/*.Qfg2sav -type f );
	do
	FILENAME="$(basename "$f")"
	mv "$f" /path/to/NAS
	ln -s /path/to/NAS/"$FILENAME" /path/to/QFG2Folder
done
-type f in the find command limits your search to only files and will ignore symlinks. The *.Qfg2sav should only search for the format of the save game files.

Obviously you would need to change the /path/to/QFG2Folder and the /path/to/NAS paths to be the location of the actual directories you want.
Malik
Level 1
Level 1
Posts: 30
Joined: Sun Mar 17, 2019 12:04 am

Re: Help for bash script to move savegames created by a game to my personal cloud and make a soft link back.

Post by Malik »

Hi thanks for the help. I'm facing an error.

Here's my savetocloud.sh:

Code: Select all

#!/bin/bash

for f in $(find '/home/malik/.wine/drive_c/Program Files (x86)/AGD Interactive/Quest for Glory II/*.Qfg2Sav' -type f );
	do
	FILENAME="$(basename "$f")"
	mv "$f" '/run/media/malik/FILES/Syncloud/Game Saves Cloud/QFG2VGA/'
	ln -s '/run/media/malik/FILES/Syncloud/Game Saves Cloud/QFG2VGA/'"$FILENAME" '/home/malik/.wine/drive_c/Program Files (x86)/AGD Interactive/Quest for Glory II/'
done
Error is

Code: Select all

find: '/home/malik/.wine/drive_c/Program Files (x86)/AGD Interactive/Quest for Glory II/*.Qfg2Sav': No such file or directory
The filename is

Code: Select all

agssave.001.Qfg2Sav
(Copy and paste directly to avoid typing errors.)

I have tried creating a dummy savefile with the same nomenclature with the next in sequence 004, just in case the script couldn't find a fresh made file, but it still showed no such file or directory.
rene
Level 20
Level 20
Posts: 12212
Joined: Sun Mar 27, 2016 6:58 pm

Re: Help for bash script to move savegames created by a game to my personal cloud and make a soft link back.

Post by rene »

It's the quotes around .../*.Qfg2Sav: you are with them asking find to search for that exact file, i.e., the file named .../*.Qfg2Sav; the quotes remove the meaning of * as a wildcard. I.e., quote as for example

Code: Select all

for f in $(find '/home/malik/.wine/drive_c/Program Files (x86)/AGD Interactive/Quest for Glory II'/*.Qfg2Sav -type f);
Malik
Level 1
Level 1
Posts: 30
Joined: Sun Mar 17, 2019 12:04 am

Re: Help for bash script to move savegames created by a game to my personal cloud and make a soft link back.

Post by Malik »

Hmm...seems like the nomenclature of my spaced-naming directories are interfering with the command.

Here's the output now after adjusting the quote location:

Code: Select all

mv: cannot stat '/home/malik/.wine/drive_c/Program': No such file or directory
mv: cannot stat 'Files': No such file or directory
mv: cannot stat '(x86)/AGD': No such file or directory
mv: cannot stat 'Interactive/Quest': No such file or directory
mv: cannot stat 'for': No such file or directory
mv: cannot stat 'Glory': No such file or directory
And what it does now is create broken symlinks of the 'AGD', 'Quest', 'for', 'Glory', 'Program' and 'Files' directories in the working directory.

Surprisingly 'Interactive' is not made into a broken symlink directory.
Attachments
Screenshot_2021-04-13_16-36-37.png
Moonstone Man
Level 16
Level 16
Posts: 6054
Joined: Mon Aug 27, 2012 10:17 pm

Re: Help for bash script to move savegames created by a game to my personal cloud and make a soft link back.

Post by Moonstone Man »

Malik wrote: Tue Apr 13, 2021 4:33 am And what it does now is create broken symlinks of the 'AGD', 'Quest', 'for', 'Glory', 'Program' and 'Files' directories in the working directory.
Escape the spaces like this:

Code: Select all

for f in $(find '/home/malik/.wine/drive_c/Program\ Files\ (x86)/AGD\ Interactive/Quest\ for\ Glory\ II/*.Qfg2Sav' -type f );
rene
Level 20
Level 20
Posts: 12212
Joined: Sun Mar 27, 2016 6:58 pm

Re: Help for bash script to move savegames created by a game to my personal cloud and make a soft link back.

Post by rene »

The immediately above is not going to work; the single quotes also remove special meaning of \. And, oh, yes, crap; had noticed you quoted "$f" so glanced over it otherwise, but the find itself is now still providing the loop with the space-seperated values. Try it as

Code: Select all

for f in "$(find '/home/malik/.wine/drive_c/Program Files (x86)/AGD Interactive/Quest for Glory II'/*.Qfg2Sav -type f)";
i.e., with an additional "-pair around the entire $(find ...). Should work it seems. Pardon if I miss something again; while I'm personally quite keen on NOT deprecating spaces in filenames ("the computer adapts to the human, not the other way around.") they can admittedly make for a bit of a confusing mess as to needing quoting on the command line.

[EDIT] Yes, that would work:

Code: Select all

rene@hp8k:~$ mkdir -p '.wine/drive_c/Program Files (x86)/AGD Interactive/Quest for Glory II'
rene@hp8k:~$ touch '.wine/drive_c/Program Files (x86)/AGD Interactive/Quest for Glory II'/foo.Qfg2Sav
rene@hp8k:~$ touch '.wine/drive_c/Program Files (x86)/AGD Interactive/Quest for Glory II'/bar.Qfg2Sav
rene@hp8k:~$ for f in "$(find '.wine/drive_c/Program Files (x86)/AGD Interactive/Quest for Glory II'/*.Qfg2Sav -type f)"; do echo "$f"; done
.wine/drive_c/Program Files (x86)/AGD Interactive/Quest for Glory II/bar.Qfg2Sav
.wine/drive_c/Program Files (x86)/AGD Interactive/Quest for Glory II/foo.Qfg2Sav
Malik
Level 1
Level 1
Posts: 30
Joined: Sun Mar 17, 2019 12:04 am

Re: Help for bash script to move savegames created by a game to my personal cloud and make a soft link back.

Post by Malik »

rene wrote: Tue Apr 13, 2021 5:00 am The immediately above is not going to work; the single quotes also remove special meaning of \. And, oh, yes, crap; had noticed you quoted "$f" so glanced over it otherwise, but the find itself is now still providing the loop with the space-seperated values. Try it as

Code: Select all

for f in "$(find '/home/malik/.wine/drive_c/Program Files (x86)/AGD Interactive/Quest for Glory II'/*.Qfg2Sav -type f)";
i.e., with an additional "-pair around the entire $(find ...). Should work it seems. Pardon if I miss something again; while I'm personally quite keen on NOT deprecating spaces in filenames ("the computer adapts to the human, not the other way around.") they can admittedly make for a bit of a confusing mess as to needing quoting on the command line.

[EDIT] Yes, that would work:
Yes! This did it! Thank you so much!

The mess a misplaced quote makes...

Thanks for chiming in too, Kadaitcha!
Moonstone Man
Level 16
Level 16
Posts: 6054
Joined: Mon Aug 27, 2012 10:17 pm

Re: Help for bash script to move savegames created by a game to my personal cloud and make a soft link back.

Post by Moonstone Man »

Malik wrote: Tue Apr 13, 2021 5:24 am Thanks for chiming in too, Kadaitcha!
With my compliments.
Locked

Return to “Beginner Questions”