It’s pretty simple:
cd ~/my_dir && tar --exclude=my_dir.tar.gz -czf my_dir.tar.gz ./*
The -c argument says to compress, z tells tar to create a gz type, f is to specify the file. The –exclude is the trick here so that tar doesn’t attempt to archive the file it is creating.
I’ve found it best to do this in the directory you are archiving, so yes cd into it first. If you don’t you will end up with problems such as longer paths in the tar archive, the exclusion specified won’t match, etc.
I like science and writing software.