Live data from Hacker News

macOS command-line tools you might not know about

saurabhs.org

391–400 of 454 posts

Re: macOS command-line tools you might not know about

#391
post #309
post #8

`pbcopy` and `pbpaste` are one of my most-loved in the list. Dealing with some minified json, switching to iTerm, doing `pbpaste | json_pp | pbcopy` and having a clean output is _so_ nice.

I use the following to edit contents of my clipboard: pbpaste | vipe | pbcopy Where vipe is a util for inserting your editor (vim) in the middle of a pipe. From: https://joeyh.name/code/moreutils/

This is great utility I didn't know about! Thanks!

But do you know why it doesn't seem to work with the `pbfilter` function?

If I do directly `pbpaste | vipe | pbcopy`, then it opens vim and the clipboard text is pasted there. But if I run `pbfilter | vipe`, then vim opens with a blank buffer.

   function pbfilter() {
      if [ $# -gt 0 ]; then
          pbpaste | "$@" | pbcopy
      else
          pbpaste | pbcopy
      fi
   }      

It seems that the number of args is 0 for some reason

Re: macOS command-line tools you might not know about

#392

Earlier quoted context omitted.

So much of my Linux use is over ssh from a MacOS client that I've made a `pbcopy` executable that just pipes stdin over ssh to my MacBook to its pbcopy (with a dedicated ssh key that runs this as a forced command). Makes it super nice to be on an SSH session and `pbcopy` some content to my MacOS clipboard!

You might want to have a look at osc52

I have! Unfortunately not supported in MacOS Terminal.app, which I'm otherwise very satisfied with (have tried iTerm2, use Alacritty on Linux, just like Terminal.app).

Re: macOS command-line tools you might not know about

#393

Earlier quoted context omitted.

So much of my Linux use is over ssh from a MacOS client that I've made a `pbcopy` executable that just pipes stdin over ssh to my MacBook to its pbcopy (with a dedicated ssh key that runs this as a forced command). Makes it super nice to be on an SSH session and `pbcopy` some content to my MacOS clipboard!

That sounds amazing, I always wanted to do that! Do you have a guide or some script to help with it? Otherwise, I will try to do it on my own.

Happy to share, but I'm away from my MacBook for the next 2-3 weeks. I'll ping you when I have access to the code again.

Re: macOS command-line tools you might not know about

#394
post #309

Earlier quoted context omitted.

I use the following to edit contents of my clipboard: pbpaste | vipe | pbcopy Where vipe is a util for inserting your editor (vim) in the middle of a pipe. From: https://joeyh.name/code/moreutils/

This is great utility I didn't know about! Thanks! But do you know why it doesn't seem to work with the `pbfilter` function? If I do directly `pbpaste | vipe | pbcopy`, then it opens vim and the clipboard text is pasted there. But if I run `pbfilter | vipe`, then vim opens with a blank buffer. function pbfilter() { if [ $# -gt 0 ]; then pbpaste | "$@" | pbcopy else pbpaste | pbcopy fi } It seems that the number of ar…

I think you have to use `pbfilter vipe`, as the argument to pbfilter is inserted into the middle of the pipe.

Re: macOS command-line tools you might not know about

#395
post #388

At the risk of sounding like an idiot, can someone please explain why I cannot use `networkQuality` when zsh is my shell? Is there an alternative for zsh?

It’s not a shell utility, it should work with any shell. $ which networkQuality /usr/bin/networkQuality

Yes thanks- probably an issue with my $PATH

Re: macOS command-line tools you might not know about

#396

You can use sips together with iconutil to generate a complete .icns file for your app from a single 1024 by 1024 PNG without any third party software: mkdir MyIcon.iconset cp Icon1024.png MyIcon.iconset/icon_512x512@2x.png sips -z 16 16 Icon1024.png --out MyIcon.iconset/icon_16x16.png sips -z 32 32 Icon1024.png --out MyIcon.iconset/icon_16x16@2x.png sips -z 32 32 Icon1024.png --out MyIcon.iconset/icon_32x32.png sips…

If you really want nice looking smaller icons (16x16, 32x32) you'll have to hand edit them after scaling down to get something that looks crisp. That said I just checked some macOS system icons (get info on app, select icon, copy, create new document in Preview.app) and they don't seem to be hand adjusted any more.

I’d say hand-crafting pixel-perfect icons with different versions for extreme sizes is worthwhile (and I don’t believe in simple rasterization from vector for the same reason), but not every developer would have resources to spare for that.

Re: macOS command-line tools you might not know about

#397
Many thanks OP for the couple of commands I did not yet know. Especially `pbcopy` and `pbpaste`, those are going to be very useful.

Here are a couple of commands I use quite a lot.

lsof -p

Lists the open files of the process with process ID . Very very useful.

fs_usage -w

This one is mentioned by others here as well, but followed by a it shows all filesystem activity of the given process. Useful if you want to know where specific settings of an application are stored.

top -u

Obvious what this does, standard command. Sorted by CPU usage.

| open -ft

Opens the 's result in your default text editor.

system_profiler

Very useful for finding out stuff. E.g. system_profiler SPNVMeDataType SPSerialATADataType | grep 'BSD Name: disk[0-9]$' | sed 's/.\* //' Gives the device name of all your system's SATA and NVMe SSD's.

sysctl -a

Another way to find out stuff. E.g. sysctl -a | grep hw.memsize Shows the amount of physical memory in your system.

tmutil

Very powerful for managing Time Machine as mentioned by others here. Also useful for other stuff. There is a lot of File I/O on my system due to running at least five VM's all the time. This produces big snapshots. Every now and then my system hung up due to running out of space because of these snapshots. Now I'm running my own "snapshottaper" daemon running every ten minutes keeping only the last 4 snapshots and deleting the rest, using "tmutil listlocalsnapshotdates" and "tmutil deletelocalsnapshots" which eliminates that issue (which is a bug imho).

And some of my often used (tcsh) aliases:

proc, aliased to 'ps -axww -o pid,user,command | grep -v "grep -i" | grep -i \!\* | sed "s/^\ *//"'

Filter the list of running processes for a specific string, e.g. use "proc adobe" to find all running processes by Adobe. I use this a lot.

spf, aliased to 'dig \!* txt | grep "v=spf"'

Useful for finding SPF records for a given domain, e.g. 'spf apple.com'.

mx, aliased to 'dig \!\* mx | grep -v "^;..*" | grep "IN\WMX"'

Same, but for MX records (I maintain e-mail servers, hence these two).

est, aliased to 'netstat -n | grep -i proto ; netstat -n | grep ESTABLISHED | grep -v 127.0.0.1'

List all currently open network connections.

listen, aliased to 'sudo echo -n; sudo lsof -i4 -n -P | grep "\*:" | sed "s/IPv4.*://" | grep LISTEN | sort -n --key=5 | sed "s/ (LISTEN)//" | awk BEGIN\ \{print\ \"COMMAND\ \ \ \ \ \ \ \ \ \ \ PID\ \ \ \ \ \ \ USER\ \ \ \ \ \ \ PORT\"\}\ \{printf\ \"%-10s\ %10s\ %10s\ %10s\\n\"\,\ \$1\,\ \$2\,\ \$3\,\ \$5\}'

List all processes currently listening on network ports.

router, aliased to 'netstat -rn -f inet | grep default | grep -v link | awk \{print\ \$2\} | head -1'

List the currently used internet router.

(I hope all escape characters and such survive posting this, please excuse me if they don't, also my default command shell is tcsh for historical reasons, it was my default shell in the early 1990's. Yes my shell scripts are all #!/bin/sh)

Other than these, I really like using AppleScript and shell scripts together. Using AppleScript I now have my own GUI tools for making disk images using drag-and-drop, compacting sparse images, performing default settings for new installations, switching between virtual machines whilst hiding others, etc.

Re: macOS command-line tools you might not know about

#398

`open` is one I use all the time. I love that simple command. alias tab='open . -a iterm' alias phpstorm='open -a "PhpStorm"' alias smerge='open -a "Sublime Merge"' etc. I use those more than I do the Open/Recents dialogs in the respective apps.

`open -a ...` tab completion has been horribly slow for years because Apple hasn’t updated their completion definition. I replied with a work around on this SO post: https://stackoverflow.com/a/63097652

Re: macOS command-line tools you might not know about

#399
post #152

Another good one is 'hidutil' which can remap any keys without additional software. It's handy for things like remapping CapsLock to anything, etc. For actual full keyboard layouts though I'd use Ukelele[0]. [0] https://software.sil.org/ukelele

There is a helper tool for hidutil https://hidutil-generator.netlify.app/ > It's handy for things like remapping CapsLock to anything It's a built-in MacOS feature that you can find in the keyboard settings.

to anything” is the key difference. MacOS only lets you remap it to other control keys.

Re: macOS command-line tools you might not know about

#400
post #57
post #3

Neat, I was not aware of `networkQuality`. A good replacement for opening up Speedtest or whatnot when you just want to figure out if the network is slow or something else is up.

It seems to grossly underestimate my upload bandwidth!

It tests your bandwidth while fully saturating your upload AND your download. If maxing out your upload has a huge impact on your download (like, a 70% drop), then that’s probably a sign you could tweak your network for better flow control.

That’s sort of the idea of `networkQuality`. It’s a new idea for how to measure that’s different from the standard speed test.

Through if you run it with the `-s` flag it’ll test them sequentially.

Post reply on HN