Skip to content

Instantly share code, notes, and snippets.

@whunter
Last active April 10, 2025 14:09
Show Gist options
  • Save whunter/103764a8554b630fccd647b345cda98f to your computer and use it in GitHub Desktop.
Save whunter/103764a8554b630fccd647b345cda98f to your computer and use it in GitHub Desktop.
Shell script for saving stuff to github
usage="Usage: . ./save.sh < filename | all | -n string | -b string>"
if [ -z "$1" ]; then
echo $usage
return
fi
savedDir='/path/to/your/git-repo'
randos="$savedDir/randos"
notes="$savedDir/notes.txt"
bookmarks="$savedDir/bookmarks.txt"
workingDir=$(pwd)
if [ "$1" = "all" ]; then
# save all files in current directory
collapsedPath="${PWD//\//-}"
echo "Saving errthing to $savedDir/$collapsedPath"
mkdir -p "$savedDir/$collapsedPath"
cp * "$savedDir/$collapsedPath"
elif [ -f "$1" ]; then
# save specific file
echo "Saving file $1 to $randos"
mkdir -p "$randos"
cp "$1" "$randos"
elif [ "$1" = "-n" ]; then
# append string input to notes.txt
echo "Saving note to $notes"
inputString=${@:2}
echo "$inputString"
echo "$inputString" >> "$notes"
elif [ "$1" = "-b" ]; then
# append url(string) to bookmarks.txt
echo "Saving url to $bookmarks"
inputString=${2}
echo "$inputString"
echo "$inputString" >> "$bookmarks"
else
echo "Not really sure what you're trying to do with: $1"
echo $usage
return
fi
# commit and push to git
if [ -d "$savedDir/.git" ]; then
cd $savedDir
git add . --all
git commit -m "Auto commit"
git push
cd $workingDir
else
echo "Not a git repository"
fi
@whunter
Copy link
Author

whunter commented Apr 10, 2025

Copies / appends (file(s) | urls < string > | freeform notes < string >) to local clone of git repo and pushes changes to remote.

Note: All save operations try to automatically commit all git changes and push them to git repo's remote after copy/append


1. Create or clone your git repo on your dev machine (if you're creating a repo you'll need to also create the corresponding "remote" repo)

git clone <your git-repo>

2. Save script to your development machine

3. Edit line 6 in save.sh to use the actual path to your local git clone

savedDir='/actual/path/to/your/local/git-repo'

4. Create an alias in your ~/.<shell>rc or ~/.aliases or whatever init script:

save() {
   /path/to/your/save.sh
}

5. Source that init script:

. ~/.aliases # or whichever init script you chose

6. Then you can call the script by the alias name from any directory:
Again: All save operations try to automatically commit all git changes and push them to git repo's remote after copy/append

Example usage:

save <filename>
# copies individual file to "/randos" directory in your local clone (creates "git-repo/randos" if it doesn't exist)
save all 
# copies all files in current directory to directory named according to current directory path, in your local clone
# All files in "/path/to/current/dir" would be copied to directory named "git-repo/-path-to-current-dir" (creates "git-repo/-path-to-current-dir" if it doesn't exist
save -n <string> 
# appends <string> to notes.txt in your local clone (creates "git-repo/notes.txt" if it doesn't exist)
save -b <url string> 
# appends <url string> to bookmarks.txt in your local clone (creates "git-repo/bookmarks.txt" if it doesn't exist)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment