Skip to content

Instantly share code, notes, and snippets.

@justincbagley
Last active October 9, 2024 11:34
Show Gist options
  • Save justincbagley/02f93b03a0cd9fde6b09d9dd7c3aeba3 to your computer and use it in GitHub Desktop.
Save justincbagley/02f93b03a0cd9fde6b09d9dd7c3aeba3 to your computer and use it in GitHub Desktop.
zipper - a shell function for zipping files and folders to add to your path on macOS

zipper

A useful function to add to your ~/.bash_profile or ~/.bashrc file on macOS that automates custom zipping of files and folders. This function also checks to make sure that the resulting gzipped folder has the same date as the last date of modificationf or the folder.

Add in your path as a full shell script by copying all below and saving into a file named zipper located in your $PATH (e.g. /usr/bin), or copy and paste just the function into ~/.bash_profile or ~/.bashrc then restart Terminal.

#!/bin/sh

# zipper

## Usage: zipper "$1", where "$1" is a file or folder to gzip.

zipper () {
	FILE_B_OUT="$(file -b "$1")";
	if [[ "$FILE_B_OUT" = "directory" ]]; then
		MY_DIR="$(basename "$1")";
		echo "Directory name: ${MY_DIR}"
		# make tarball:
		echo "Creating gzipped tarball ${MY_DIR}.tar.gz ..."
		tar -zcf "$MY_DIR".tar.gz "$MY_DIR" && touch -mt "$(stat -t %Y%m%d%H%M "$MY_DIR" | cut -d\  -f11 | sed 's/\"//g')" "$MY_DIR".tar.gz
		echo "Done."
		echo "..."
	else
		MY_FILE="$(basename "$1")";
		echo "File name: ${MY_FILE}"
		# make gzipped file, not keeping original:
		MY_DATE="$(stat -t %Y%m%d%H%M "$MY_FILE" | cut -d\  -f11 | sed 's/\"//g')";
		echo "File date: ${MY_DATE}"
		echo "Creating gzipped file ${MY_FILE}.gz ..."
		# This code keeps the original:
		#gzip -c "$MY_FILE" > "$MY_FILE".gz && touch -mt "$MY_DATE" "$MY_FILE".gz
		# This code does NOT keep the original (also exiting .gz file is overwritten, if present):
		gzip -f "$MY_FILE" > "$MY_FILE".gz && touch -mt "$MY_DATE" "$MY_FILE".gz
		echo "Done."
		echo "..."
	fi
}
@Grace001472
Copy link

Creating a shell function for zipping files and folders on macOS is a wonderful way to streamline your workflow! You can write a simple function that takes a directory path as an argument and zips it for you. This saves time and keeps your files organized. If you're managing multiple projects, including needing business dissertation help, automating tasks like this can really help you stay focused and efficient!

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