Cool Bash Tricks for Your Terminal’s “Dotfiles”

You may have noticed the recent trend of techies posting their “dotfiles” on GitHub for the world to see. These usually contain shortcuts compatible with Bash terminals to automate convoluted commands that, I’ll admit, I needed to Google every single time. 

My full dotfiles are posted at this Git repository, but here’s a summary of the ones I find most helpful that you can add to your own .bash_profile or .bashrc file.


Check your current IP address (IPv4 or IPv6):

alias ip4="dig +short myip.opendns.com A @resolver1.opendns.com"
alias ip6="dig +short -6 myip.opendns.com AAAA @resolver1.ipv6-sandbox.opendns.com"

Check your current local IP address:

alias iplocal="ipconfig getifaddr en0"

Check, clear, set (Google DNS or Cloudflare DNS or custom), and flush your computer’s DNS, overriding your router:

alias dns-check="networksetup -setdnsservers Wi-Fi"
alias dns-clear="networksetup -getdnsservers Wi-Fi"

alias dns-set-cloudflare="dns-set 1.1.1.1 1.0.0.1"
alias dns-set-google="dns-set 8.8.8.8 8.8.4.4"
alias dns-set-custom="networksetup -setdnsservers Wi-Fi " # example: dns-set-custom 208.67.222.222 208.67.220.220

alias dns-flush="sudo killall -HUP mDNSResponder; sudo killall mDNSResponderHelper; sudo dscacheutil -flushcache"

Start a simple local web server in current directory:

alias serve="python -c 'import SimpleHTTPServer; SimpleHTTPServer.test()'"

Test your internet connection’s speed (uses 100MB of data):

alias speed="wget -O /dev/null http://cachefly.cachefly.net/100mb.test"

Query DNS records of a domain:

alias digg="dig @8.8.8.8 +nocmd any +multiline +noall +answer"   # example: digg google.com

Make a new directory and change directories into it.

mkcdir() {
mkdir -p -- "$1" &&
cd -P -- "$1"
}

Unhide and rehide hidden files and folders on macOS:

alias unhide="defaults write com.apple.finder AppleShowAllFiles -bool true && killall Finder"
alias rehide="defaults write com.apple.finder AppleShowAllFiles -bool false && killall Finder"

Force empty trash on macOS:

alias forcetrash="sudo rm -rf ~/.Trash /Volumes/*/.Trashes"

Quickly lock your screen on macOS:

alias afk="/System/Library/CoreServices/Menu\ Extras/User.menu/Contents/Resources/CGSession -suspend"

Update Homebrew packages, global NPM packages, Ruby Gems, and macOS in all one swoop:

alias update="brew update; brew upgrade; brew cleanup; npm install npm -g; npm update -g; sudo gem update --system; sudo gem update; sudo gem cleanup; sudo softwareupdate -i -a;"

Copy your public key to the clipboard:

alias pubkey="more ~/.ssh/id_rsa.pub | pbcopy | echo '=> Public key copied to pasteboard.'"

Undo the most recent commit in current Git repo:

alias gundo="git push -f origin HEAD^:master"

Un-quarantine an “unidentified developer’s” application blocked by Gatekeeper on macOS’s walled prison garden:

alias unq="sudo xattr -rd com.apple.quarantine"

Quickly open a Bash prompt in a running Docker container:

docker-bash() {
docker exec -ti $1 /bin/bash
}

Pull updates for all Docker images with the tag “latest”:

docker images --format "{{.Repository}}:{{.Tag}}" | grep :latest | xargs -L1 docker pull

This odd hack is needed to run any of these aliases as sudo:

alias sudo="sudo "

View all of my dotfiles here or check out other cool programmers’ dotfiles over at this amazing collection.

How To: Shrink a Linux Virtual Machine Disk with VMware

VMware Workstation and Fusion normally work hard to minimize the size of virtual hard disks for optimizing the amount of storage needed on your host machine . On Windows virtual machines, VMware has a “clean up” function, which detects newly unused space and makes the size of the virtual hard disk smaller accordingly. You’ll notice that even if you create a virtual machine with a capacity of 60 GB, for example, the actual size of the VMDK file will dynamically resize to fit the usage of the guest operating system. 60 GB is simply the maximum amount of storage allowed; if your guest operating system and its files amount to 20 GB, the VMDK file will simply be 20 GB. 

VMware can be set to automatically optimize and shrink virtual hard disks as you add and, more importantly, remove files – but this automatic “clean up” setting is disabled by default. Either way, cleaning up virtual machines works like a charm…when you have Windows as a guest operating system with an NTFS disk.

As a developer, I have several VMs with various Linux-based guest OSes – and, for some reason, VMware doesn’t know how to optimize these. If you poke around in VMware, you’ll find that the clean up button is greyed-out under the settings of a Linux VM.

Commonly, I’ll use a few gigabytes of storage for a project and then delete the files from the guest when I’m done.  Let’s say that my Debian guest starts at 10 GB and I use 5 GB for my project, totaling 15 GB. The VMDK file will be, obviously, 15 GB. I finish the project and delete the 5 GB of its files. On a Windows guest, VMware would be able to shrink the volume back down to 10 GB – but you’ll quickly notice, annoyingly, that a Linux disk will remain at 15 GB, even though you’re no longer using that much. On a portable machine like my MacBook Air, this can be a huge waste!

The “clean up” feature that VMware has developed for Windows guests can be applied to Linux guests as well, but it’s pretty convoluted – we need to essentially clean up the VM ourselves, trick VMware to detect the free space, and manually shrink the volume.

A tiny caveat: This only works on VMs without any snapshots. Sadly, you either need to delete them or, if you care about keeping snapshots, you can backup the VM as-is to an external disk and then delete the local snapshots.

Once you’re ready, here’s how to shrink your Linux-based VM: 

Step 1: Clean up time

Boot up your Linux virtual machine. We’ll start by optimizing the OS as much as possible before shrinking it. In addition to manually deleting files you no longer use, running this command in your terminal can free up a little more space by removing some installation caches left behind by old versions of software you’ve installed and updated: 

sudo apt-get clean

Step 2: Make “empty” space actually empty

This step is the crucial one. In order for VMware to detect the newly free space, we need to free it up ourselves using a little trickery. We’re going to have Linux overwrite the free space with a file full of zeros – the size of this file will be the size of however much space we’re freeing up (5 GB, in the example above) – and then delete it. These commands will create the file, wait a moment, and then delete the file:

cat /dev/zero > zero.fill
sync
sleep 1
sync
rm -f zero.fill

Depending on how much space we’re freeing, this could take a while. Let it finish or else you’ll be left with an actual, real file that will occupy a ton of space – the opposite of what we’re trying to accomplish!  

Step 3: Letting VMware know we’ve done its dirty work

The final step is to tell VMware we’ve done this, and manually trigger the clean up function that works so well on Windows VMs. You’ll do this step outside of the virtual machine, so shut it down fully and exit VMware.  These directions are for macOS hosts specifically – if you’re on a Linux host, I’ll assume you are able to find the VMDK file, but here’s some help if you need.

VMware on macOS makes this a little tricky, since it packages VMs in what looks like a “.vmwarevm” file, which is actually a folder. Browse to wherever you’ve saved your virtual machines, probably somewhere in your home folder, and find the location of this “.vmwarevm” androgynous item. If you click on this folder, though, it’ll just open VMware again.

We need to right click on the .vmwarevm “file,” and select Show Package Contents to see what’s really in there. You should see the actual .VMDK file sitting there – normally we’re looking for the plain VMDK file (named Virtual Disk.vmdk by default) without a bunch of numbers after it, but if you have snapshots associated with your VM, this might not be the file we actually want. But run the command below with it anyways, and the output will tell you if you need to use a different file.

Now, we’re going to run our final command in our host terminal, so open that up. Linux installations of VMware Workstation should have a simple map to the vmware-vdiskmanager utility that you can run anywhere, but on macOS we need to tell it exactly where that’s located: in the Applications folder, where Fusion is installed.

We’re going to feed this command the exact location of the VMDK file we’re shrinking. You can either do this by typing the full path to it, or by simply dragging the VMDK file onto the terminal after typing the first part of the command (up to and including “-d”). The “-d” argument will defragment the disk.

/Applications/VMware\ Fusion.app/Contents/Library/vmware-vdiskmanager -d <path to your .VMDK file>

The final command should look something like this, with your VMDK file instead:

/Applications/VMware\ Fusion.app/Contents/Library/vmware-vdiskmanager -d /Users/jake/Documents/Virtual\ Machines/Debian9.vmwarevm/Virtual\ Disk.vmdk

If you’ve done this correctly, you’ll see it defragmenting the file, and then return “Defragmentation completed successfully.” If it returns a different error, such as “This disk is read-only in the snapshot chain,” it should tell you which disk you should actually shrink. Just run the command again with that VMDK file instead. 

After the defragmentation completes, we need to finally shrink the image. We do this by running the same command as you did above, but replacing the “-d” with “-k” as follows:

/Applications/VMware\ Fusion.app/Contents/Library/vmware-vdiskmanager -k <path to the same .VMDK file>

Step 4: Storage Profit!

Obviously, this is a really annoying way to perform a feature that only takes one click to execute on Windows virtual machines. I don’t recommend going through this entire process every time you delete a few random files. However, if you notice the free space on your host OS is mysteriously lower than it should be, the time this takes can be well worth it.

Let’s hope this will be integrated in VMware Tools in the near future – feel free to nudge VMware about it in the meantime!

Let’s hope this will be integrated in VMware Tools in the near future – feel free to nudge VMware about it in the meantime!


Update (Dec. 30, 2018):

The open-source version of VMware Tools for Linux, open-vm-tools, has added a simple command to automate the above steps in the latest version. Make sure you have the latest update through either apt or yum, and then run the following command in the guest terminal:

vmware-toolbox-cmd disk shrink /

Thank you to commenter Susanna for pointing this out!

Why This Millennial Is With Hillary Clinton Now  — and Why We All Need To Be In November

Hillary for New Hampshire Winter Fellows with Hillary Clinton in Derry, NH (February 3, 2016)

Keeping in mind the big picture…

I am a 24-year-old “millennial” and I passionately support Hillary Clinton for the 45th President of the United States. Yes, we exist.

My goal here isn’t to convince every Bernie believer to jump ship and support her as passionately as I do, although I feel obligated to try. I totally understand the passion for Bernie. I smile inside every time I see a young person (like my sister) become interested in politics for the first time and become directly involved in influencing the course of their own future, no matter which candidate triggered it for them. For me, it was admittedly Senator Obama. I would, however, like to put the Democratic Party primary process back into perspective, because it’s turned into a bloodsport that isn’t helpful for anybody in the long run — not for either candidate, not for our party, and certainly not for our country.

News Flash: We aren’t in the general election right now. Not even close. We’re in the middle of our own party’s primary, where the field of opponents we are choosing from are all our friends. They’re both on our side. They both agree on an overall vision for our country. Of course as individuals we choose one who we like better than the other, and root for her or him and ideally invest some time and money to help however we can. I chose Hillary a long time ago because I feel she is, if anything, overqualified for the position. Especially during this increasingly turbulent period of foreign affairs, we can’t afford to allow an entry-level applicant to experiment with our standing in the world and learn our relationships with other nations on-the-fly.

After working for months as a fellow on Hillary’s campaign in New Hampshire leading up to the first primary in the country, I could feed you all the standard campaign talking points in my sleep: After graduating from Yale Law she went to work at the Children’s Defense Fund, not a high-paying New York law firm. She went undercover in Alabama to investigate discrimination in public schools. She got juveniles out of adult prisons. She gave 8 million children healthcare. But there’s just one thing that, for some reason, is hard for people to believe: at her core she is a good, caring, and loving person who has had only selfless intentions her entire life. I promise you.

I had the incredible chance to meet Hillary the weekend before the New Hampshire primary. Her motorcade plowed through a quiet suburb in Manchester around noon and she hopped out to go knock on the doors of some lucky families. As neighbors started coming out of their houses to shake her hand, I couldn’t restrain myself from at least trying to get close and wave hello. (By the way, it’s amazing how casual the people in New Hampshire are about meeting presidential candidates.)

I walked up nervously and told her that it was my birthday (it was) and all I wanted was for her to win, which got her attention, and I thanked her for the spotlight she had been shining on the rampant addiction epidemic in the state. Instead of nodding her head and thanking me for my support and moving along like I assumed she would — she knew she would have my vote no matter what — she locked eyes with me and asked me how I’d been affected by the issue. It felt as though she dropped everything in her life and literally put her jam-packed schedule on pause to make sure I was okay and to learn more about some dude she just met ten seconds ago. I told her that I had fallen into the trap myself when I was younger, and that the part of her detailed plan that addresses the overprescription of narcotics by doctors could have prevented me from doing so. As my conversation with her grew longer and longer, and as she respectfully asked me more and more questions about my story, I totally forgot I was casually chatting on the sidewalk with a freaking former First Lady, Senator, and Secretary of State. I promise you again: She. Is. A. Real. Person.

“I know I have some work to do, particularly with young people, but I will repeat again what I have said this week. Even if they are not supporting me now, I support them.” »

But at the end of the day, all I ask is for you to keep in mind the stakes in this overall election. They have never been higher. Last year, the spectacle of Donald “The Donald” Trump running to be the leader of the free world was purely comical and impossible not to laugh at, from the moment he entered the race via gold-plated escalator whilst blasting Neil Young. But as this racist xenophobic pumpkin is rapidly racking up actual real-life delegates thanks to votes from the poorly educated and/or the white supremacists, the thought of him being within striking distance of the desk in the Oval Office is slowly twisting a knife into the pit of my stomach. This is real. This is the big picture. This is why we need to team up and work together in any way possible as soon as possible.

I’m aware of the street cred young Democrats collect by claiming they hated Hillary before hating Hillary was cool. Hating on HRC has gone more viral than Damn Daniel. But when you ask these young voters to explain why they think she’s a liar or untrustworthy or a criminal, they can rarely put their distaste for her into actual words — or if they do, they just vomit hashtag-ready soundbites from Fox News or The Young Turks. #Benghazi. #Emails. #ReleaseTheTranscripts. Joining in on the Republican-led attacks and stooping to their level is no way to advocate for the candidate you support. If you support Bernie for the nomination, you do that by going out and talking to others about why his policies rock, what his life story is, how your story relates to his story and his policies, etc. — not by spending your day mercilessly assassinating the character of a woman you’ve never met and a woman you might very well be voting for in eight short months, unless you’re able to stomach the idea of President Trump. During primary season, you win by focusing on the merits of your own candidate, not the flaws you see in another.

As Bill Maher (an avid Bernie supporter) said this weekend, some in our party need to “learn the difference between an imperfect friend and a deadly enemy.” I don’t agree with everything Hillary has said or done. I don’t unconditionally defend every single chapter in her public record over the past 30 years (and neither does she, by the way). I don’t think that’s possible for any voter to find in a politician. But if you identify as a Democrat, she is the farthest thing from your enemy. Plain and simple. Like you and Bernie, she wants to prevent a Republican from winning in November and reversing so much of the progress we’ve made over the past seven years on their first day in office. That is our number one goal right now. And whether it gets accomplished by a President Clinton or a President Sanders, I am 100% on board either way. Let’s stop fighting each other and start fighting together.