Skip to content

Terminal

Easy Unix date formatting

I use the date function quite often in scripts, mainly to append date/time stamps to filenames. For example, something like this…

newtime=`date +%Y-%m-%d_%H%M`
cp somefile $newtime-some_other_file

That particular format is the one I use most often, with the full date followed by the hours and minutes in 24 hour format: 2017-04-12_2315, for example. I use this one so that filenames wind up sorted by date order in Finder views.

Once I move beyond that format, though, the vagaries of date string formatting leave me dazed. Enter strftime.net, where you can build any date string you like using a point-and-click editor with real-time previews:

It doesn't get much easier than that.



Adjusting for the oddities of ctime

In the shell script I use to back up my web sites (I really should update that, they're much different now), I include a line that trims the backup folder of older compressed backups of the actual WordPress databases. That line used to look like this:

find path/to/sqlfiles/backups -ctime +5 -delete

I thought this should delete all backups in that folder that are at least five days old, via the ctime +5 bit.1Footnote: I know now I should have been using mtime, though it would have had the same issue I had with ctime. But it turns out I thought wrong. The above will delete all files that are at least six days old. Why? I don't know why it works this way, but it's mostly explained in the man page for find (my emphasis added):

-ctime n[smhdw] If no units are specified, this primary evaluates to true if the difference between the time of last change of file status information and the time find was started, rounded up to the next full 24-hour period, is n 24-hour periods. If units are specified, this primary evaluates to true if the difference between the time of last change of file status information and the time find was started is exactly n units. Please refer to the -atime primary description for information on supported time units.

To make find do what I wanted it to do, I just needed to change +5 to +5d. Simple enough…but while figuring this out, I stumbled across this page, which has an alternative solution with more flexibility:

find path/to/sqlfiles/backups -mmin +$((60*24*5)) -delete

The mmin parameter is much more precise than ctime:

-mmin n True if the difference between the file last modification time and the time find was started, rounded up to the next full minute, is n minutes.

By using mmin, I can be really precise. As shown, 60*24*5 gets me the same five-day interval as ctime +5d. (And yes, I could have used 7200 instead of 60*24*5, but I find it clearer to leave it in its expanded form.)

But I could instead delete backups that were older than 3.25 days (60*24*3.25 or 5040), or for any other arbitrary time period. I like the flexibility this offers over ctime, so I've switched my script over to this form.



Cancel shell script on remote connect failure

I use a shell script to back up this site (and a variant of the same script to back up the Many Tricks site). I've been using these scripts for over a decade (wow), and though they've evolved, they're still fundamentally the same. (I did switch from cron to launchd for launching them, however.)

While the script typically runs very nicely, I recently noticed that my last backup was from a few weeks ago—uh oh. It didn't take long to figure out what had gone wrong: My ISP changed the hostname of the machine my site runs on, and my script uses ssh, scp and rsync, which connect via the hostname. Unfortunately, the failure mode is silence, because the script runs via a scheduled task. The only way I knew it failed was when I went to check the backup folder. Obviously, something more automatic than that would be desirable.

After much web searching, I couldn't find anything that seeemed like it'd do what I want: An email (and onscreen alert) if my backup failed. I found lots of Unix solutions to send mail using sendmail, but I didn't really want to enable that on my Mac. So I futzed around and built a simple checker that will mail me when it can't reach my web host.

[continue reading…]



Color and ‘human readable’ file sizes in Terminal

These are two very old tips, but I'd forgotten about them until recently, when I sent someone a screenshot and they said "Hey, how'd you do that?"

Do what, exactly? This…

The most-obvious thing in that shot is the colored filenames. But notice, too, the file sizes are in a human-readable form. Both of these changes are pretty simple, though you could spend hours playing with colors.

Human-readable output

To get human readable output—not just from ls but also in du, which shows disk space usage—just include an h with the ls command: ls -alh. Instead of raw bytes, the values are converted and marked with trailing B, K, M, etc.

Because I never use ls in its short form, I actually added a line to my .profile (which loads whenever you open a Terminal session) to make this automatic:

alias ls='ls -alh'

You could do the same thing with du, but I rarely use that command, so I didn't bother.

[continue reading…]



Use macOS VMs in VMware Fusion in retina mode

I use VMware Fusion often—I have virtual machines that span Mac OS X 10.6 to macOS 10.12.4 beta. I use the more-recent of these for supporting our customers on older versions of the OS, and keep the really old versions just for nostalgia purposes. (I have a bunch of non-macOS virtual machines, too, but they're not relevant to this tidbit.)

In all the time I've been using Fusion on my retina Macs, though, I've never enabled this setting…

…well, I enabled it once, but turned it off, because the end result was too small to see: In Retina mode, every pixel is an actual pixel, not a doubled pixel. On my 27" iMac, that meant the macOS VM thought it was running at (for example) 2560x1600 instead of a retina resolution of 1280x800. VMware even warns you of this in their Knowledge Base:

Mac OS X running in a virtual machine is limited to an approximate resolution of 2560 x 1600, and treats the display as a standard DPI device. This makes the text and icons to appear small in the OS X interface.

However, today I stumbled across this solution from Patrick Bougie—and it's brilliant in its simplicity. Patrick's post has all the details; I'll reproduce them here in abbreviated form, just in case his page ever vanishes.

[continue reading…]



Watch a screen saver in the background

Another oldie but goodie, and it's best demonstrated by example:

Yes, that's a screen saver running in the background, behind whatever work you're doing. And if nothing else, it's a great example of the progress of our CPUs and GPUs since 2002. In the original hint, I noted:

On my G4/733 with the GeForce3, this is simply amazing. The new "flurry" screensaver is running right now on the destop at 1600x1200 in thousands, iTunes is playing, the ink recognition floater is open, and yet the CPU utilization is averaging at or below 50% of thereabouts

Today, I'm testing it on a 5K iMac (5120x2880) with a second connected 4K (3840x2160) display—a total of 23,040,000 pixels, or 12 times as many pixels as in 2002—with Flurry running on both screens, and the CPU usage is somewhere around 10% to 15%. (Flurry does send the iMac's fans into a tizzy, though.) Other screen savers are even less intensive, and don't send my iMac's fans into high gear.

I can't imagine actually working this way for very long, but it is kind of interesting. Here's how to start (and more importantly, perhaps, stop) a background screen saver.

[continue reading…]



A unique way to see the weather

Ever wanted a "light" weather check web site, free of ads and other visual clutter? One that you could maybe even use from Terminal? Then you want wttr.in.

Sure, you can use it from your browser, i.e. see the weather in Boston or Montreal, by just appending the zip/postal code of interest to the URL, i.e. http://wttr.in/95014. If you omit the location, wttr.in will get the location based on your IP address—for me, that's never anywhere near correct when I'm at home, though.

What's really neat is you can use it in Terminal, too, via curl:

$ curl wttr.in/80301

The output is graphical, but done so with text characters (click for zoomed version):

At a glance, you get a few days' worth of conditions, including temperature range, wind speed, visibility, and precipitation. There's even animation—check somewhere with thunderstorms, and you'll see flashing lightning bolts.

There's a help page that explains lots of other options, like forcing metric or US units, and looking at weather by airport code.



Install and use a non-GUI connection speed test tool

Yesterday on Twitter, Dave Hamilton tweeted…

What is this speedtest exactly? And what is brew, you may also be wondering? You may also be wondering why, if you have brew, Dave's command doesn't work…that's because it's actually brew install speedtest_cli…but I'm getting ahead of myself.

speedtest is a command line interface (i.e. Unix app run from Terminal) to the connection speed tests at Speedtest.net—you get the results without the fancy animated graphics. And Brew is "the missing package manager for macOS." In other words, it's an app to help you install (and uninstall) other apps.

Here's how speedtest looks in its default mode—note that I've sped things up greatly for the GIF…

Much nicer to me, though, is the simplified version:

$ speedtest_cli --simple
Ping: 9.482 ms
Download: 94.23 Mbit/s
Upload: 68.66 Mbit/s

No animated dots, just three lines with the results. As you might expect if you read here regularly, I also wrote a Keyboard Maestro macro (of course I did!) that makes it really easy to run the simple version of the test, and does some editing of the output to simplify the display:

If you'd like to install speedtest (and maybe add the macro)—even if you don't want to install Brew to do so—keep reading…

[continue reading…]



Control inline video—and more—in Safari

This is another oldie but goodie from Mac OS X Hints, explaining how to enable the Debug menu in Safari. To do that, quit Safari, open Terminal, paste the following line, and press Return:

defaults write com.apple.Safari IncludeInternalDebugMenu 1

When you relaunch Safari, you'll have a (really long) Debug menu on the far right of Safari's menus. And just why might you want a Debug menu in Safari? Kirk McElhearn offers up one good reason:

Auto-play videos suck. They use bandwidth, and their annoying sounds get in the way when you’re listening to music and open a web page. …

But you can stop auto-play videos from playing on a Mac. If you use Chrome or Firefox, it’s pretty simple, and the plugins below work both on macOS and Windows; if you use Safari, it’s a bit more complex, but it’s not that hard.

In Safari, they key is the Debug menu, as Kirk points out. Go to Media Flags and select (activate) Disallow Inline Video, and that should be the end of auto-playing video. See Kirk's blog post for ways to do the same in Firefox and Chrome.

Beyond auto-play video, though, there's lots to geek out about in the Debug menu…

[continue reading…]



Change the look of inactive Terminal windows

I tweeted this recently…

Things I did not know (or perhaps remember): Terminal lets you set the opacity and blur of inactive windows. (Profiles > Text > Background)

…but because I often forget about those things—the ephemeral nature of tweets being what it is—I figured I should post it here, too.

I have no idea when the feature appeared, but I only discovered it on February 27th, when I tweeted about it. You'll find the window at right in Terminal's preferences, on the Profiles tab—look in the Text section for a selected window, then click the color tile under Background. Check the box to set opacity and blur for inactive windows, and you're done.

As I use a dark background in Terminal, I like this feature a lot. I've now got it set up to go transparent and fuzzy when inactive—this makes it basically vanish, so the dark background no longer grabs my eye.

At some point, I'll dig through my virtual machines and figure out when this was added…who knows, maybe it's been there for forever. In any event, I'm glad I stumbled across it the other day.