←back to thread

2039 points Gadiguibou | 1 comments | | HN request time: 0.218s | source
1. ernst_mulder ◴[] No.36503661[source]
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 <PID>

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

fs_usage -w <PID>

This one is mentioned by others here as well, but followed by a <PID> 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.

<some command> | open -ft

Opens the <some command>'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.