Mac Terminal commands and packages / addons - Do you use any?

Hello!
I’m new into using Terminal apps because a lot of GUI apps I was looking at didn’t work for me. I installed Python and brew and it feels like a new world is opening to me. So, can you recommend CLI apps / commands / packages you are using regularly and are better than GUI counterparts?

2 Likes

It might help if you gave some indication of what it is you do with you Mac, and perhaps which GUI apps did not work for you.

This will help target responses to your use cases and needs.

A search of the forums finds this discussion which may be of interest:

2 Likes

I’m a teacher and our schools have bad WIFI, if they have WIFI at all. So I started downloading some Youtube videos with various apps but they are so bad written and clunky and laggy.
I tried yt-dlp and it works like a charm.

I archived a lot of old docs with Winrar back in my Windows days some 20 years ago. I was looking for an app but didn’t like the implementation and so I tried unrar you can get through homebrew and it also works so nice.

I installed a python crossword maker, I’m making my own crosswords and word search games for my students, without having to think about some copyright.

I wrote some Python scripts with the help of ChatGPT to scramble words or sentences for my worksheets.

I found out today that there is a speedtest CLI to test my internet connection.

So I was wondering: are there more useful tools I don’t even know? Password managers? Email apps?

I stumbled upon this list a week or so ago. I do not use CLI password manager or email app, so I cannot speak to the quality of the recommendations (and a quick perusal did not reveal an email app):

Note that some applications have associated CLI tools. For example, BBEdit has a command line tool to invoke the app.

Have fun poking around and trying things out.

1 Like

FWIW, if you’re talking about archiving on the command line “tar” and “gzip” are your friends. “Tar” literally creates an uncompressed file of everything you ask it to add. Gzip compresses it. And you can gzip while you tar with a command line switch:

tar cvfz /Volumes/ExternalDrive/MySuperSecretDocuments.tgz /Users/yourname/Documents

gives you a gzipped tarball of your Documents folder.

And then, for the bonus, you can start playing with the crontab to schedule things. Type “crontab -e”, and you’ll get a file where you can type fun things like:

1 4 * * * tar cvfz /Volumes/ExternalDrive/MySuperSecretDocuments.tgz /Users/yourname/Documents

So on the 1st minute of the 4th hour, assuming your Mac is awake, that will archive your Documents folder to your external hard drive.

And if you just want to sync your Documents folder to that external drive, noncompressed, try rsync:

rsync -r /Users/yourname/Documents /Volumes/ExternalDrive/

That literally recursively copies everything from your Docs folder to your external drive.

And if you want to get a bit more exotic, create a shell script that does

today=$(date "+%F")
tar cvfz /Volumes/ExternalDrive/mydocs-$today.tgz /Users/yourname/Documents

That will give you a dated archive file on your external drive, containing all your documents.

Lots of fun stuff you can do. Have fun playing around. :slight_smile:

6 Likes

Thank you a lot. This will be fun. An hour ago I was still thinking: What should I do this weekend. Now I’m safe. :smiley:

1 Like

A couple of days ago I had ChatGPT help me write a perl script to combine a series of CSV files as an Excel workbook with each file becoming worksheet.

Also had it helped me with hints for a shortcut to dump out data from iOS’s Health app, which I’m looking forward to using as soon as I upgrade from my iPhone 5S. Although it wasn’t so helpful with inputing external data (such as my several years of blood pressure readings) into the health app.

Been using ChatGPT for other things too including some SQL statements to fiddle with names. Helpful but not a full on solution unlike that perl script.

1 Like

Love to see people with non-programming jobs getting so much use out of the command-line :slight_smile:

As a dev, I always have a terminal open and ready to type in, so some things might be easier for me to do with CLI, but not so much to someone less interested in learning intricacies like pipes, redirections, package installers etc.

Here are some workflows that for me are much easier to do with the command line, in the hope that you might find something useful:

curl --upload-file hello.txt https://transfer.sh/hello.txt

Helpful when you need to send someone a download link in an email, or even in chats where images might get too compressed by the platform, or if the attachment is too large.

  • Optimizing image size without losing quality with ImageOptim
# Install with brew
brew install imageoptim

# Optimize individual images
imageoptim image.png

# Optimize a directory of images
imageoptim -d /path/to/folder/with/images

For this purpose, I also built Clop to do automatic optimization on images that you copy. I noticed I used ImageOptim too often on the screenshots I was copy-pasting, so I automated the task.

  • Upgrading/installing Mac App Store apps with mas
# Install with brew
brew install mas

  • Updating the system without having to go through System Preferences
sudo softwareupdate --all --install --force --restart
  • Navigating and editing Excel/CSV files like a wizard with visidata

You can do a lot of actions quickly using one-key actions like d for delete row, / for searching rows in a column, shift-f for finding the frequency of values in a column etc.

asciicast

  • Optimizing and converting videos using ffmpeg

Most people don’t know, but their screen recordings can usually be made 20x smaller without losing much quality.

# Install with brew
brew install ffmpeg

# Optimize a MOV video into a more compatible MP4
ffmpeg -i my_video.mov my_video.mp4

# Optimize a too large MP4 video
ffmpeg -i my_video.mp4 my_video_optimized.mp4
  • Create a good looking GIF from a video using gifski
# Install gifski
brew install gifski

# Deconstruct video into frames
ffmpeg -i my_video.mp4 /tmp/frame%04d.png

# Reconstruct GIF from frames
gifski -o my_video.gif /tmp/frame*.png
  • Looking at system/app logs without going through the slow and clunky Console.app
# See all logs
log stream

# Filter logs by text (example below shows logs when the ambient light changes)
log stream --predicate 'eventMessage contains "lux"'

# Filter logs by process/level/text (example below shows logs when TrueTone adapts to color changes)
log stream --level info --process corebrightnessd --predicate 'eventMessage contains "Tristimulus"'

# Look at errors only (example below looks for error logs containing text "network")
log stream --predicate 'eventMessage contains "network" && (messageType == "error" || messageType == "fault")'

There’s nothing the command line can’t do, but you’ll probably discover more as you find what your needs are. Some things are a lot faster at the CLI, some have good GUIs that make the job easier.

9 Likes

I’m an old school programmer/software engineer/computing scientist who started out using punched cards, migrate to using teletypes with a side turn to a Tektronix graphics disply and then a VDU (as a glass teletype mostly but a screen editor) who still prefers to use the command line more than a GUI.

At the moment using SQLite to mangle some data acquired from a public body; their data is a real mess causing me to create some wacky SQL statements to clean it all up before I do anything meaningful with it. Including create a bash script to grab related data from elsewhere to plug holes in the public body’s data. Have also "brew"ed a copy of csvkit to further help me sanitise the data.

I just wish I could find a way to have both bash and SQLite do word deletion using the usual macOS key-combinations.

2 Likes

Freshman Fortran on a IBM 3033 with punch cards. Good times. :slight_smile:

1 Like

You might benefit from using a more user friendly shell like fish and a configurable terminal like kitty

I know it’s hard to move from tools you know well, but it can bring benefits if you can get a better user experience.

For example in this video I show how I:

  • move between words with ⌥ Option + /
  • move between space-delimited tokens with ⌘ Command + /
  • delete words with ⌥ Option + backspace
  • delete space-delimited tokens with ⌘ Command + backspace
  • undo/redo modifications with ⌘ Command + Z and ⌘ Command + Shift + Z
  • go to start/end with fn + /

All this is made easy with what fish and kitty provide in user configuration. It’s probably doable in bash and other terminals as well, but maybe a bit harder.

For fish I have the following bindings:

bind \e\[1\;3D backward-word
bind \e\[1\;3C forward-word
bind \e\[1\;5D backward-bigword
bind \e\[1\;5C forward-bigword
bind \cw backward-kill-bigword
bind \e\[3\;3~ kill-word
bind \e_ undo
bind \e+ redo

And because shells don’t have any notion of a Command key, I have the following kitty bindings:

map super+z         send_text normal,application \x1b_
map super+shift+z   send_text normal,application \x1b+
map super+backspace send_text normal,application \x17
map super+left      send_text normal,application \x1b[1;5D
map super+right     send_text normal,application \x1b[1;5C
2 Likes

You can find a free ebook about the Terminal here:

1 Like

Interesting idea but as appealing as a friendler shell is it does not solve the parallel problem with SQLite3. However, if I solve the SQLite3 setup it means I’ll also solve it for bash as both programs utilise the GNU readline library functions. GNU readline has a configuration file (~/.inputrc) or /etc/inputrc in which remapping keystrokes can be specified. Also solves it for any other command-driven program that uses readline.

A minor hinderance to do that is that Apple, for some reason known only to themselves, do not ship the man page for readline; had to go look that one up on a Linux site.

“Est. reading time: 2 minute”

Looks like a pretty short book … :slight_smile:

(I just couldn’t resist! It is actually worth reading, and listening to the podcast it is based on.)

I tend to take terminal commands into AppleScript applications, either to allow them to run in a broader context (e.g. toggling between normal mode and video presentation mode) or to allow them to accept files or folders via drag+drop or to allow them to run as quick commands via a context menu option at the Finder level.

→ pdf-crop-margins: three scripts – one to crop margins to 1in, one to crop margins to 0in, and one to crop margins defined input value

→ jupyter: toggle on/off JupyterLabs notebooks

→ blueutil: toggle blue tooth on/off

→ scselect: select a specific network location

→ defaults write … : set/reset certain default states for various apps

→ ffmpeg … : optimize a video or set of video files

→ xattr -d …: clean up stray Finder tags


JJW

This is really about BASH. The current Mac Terminal uses ZSH.

1 Like

I think they’re mostly syntax compatible though, and the shell can changed to bash if needed (but that’s probably not a great idea). If you’re writing a script you can choose the whichever shell works best for the script.

The website for the book and podcast notes that, and even warns that there may be incompatibilities. But as ACaurtionaryTale noted, the syntax is mostly compatible.

While macOS since Catalina has set the default shell as Zsh, Ventura ships with seven shells, including Bash, albeit an old version (it is easy enough to install the current version should one so desire, I have, although most of the time I use Zsh). Terminal programs will startup with the default shell, but simply typing the name of the desired shell will switch to that shell. One can even run different shells in different shell windows or tabs at the same time (or to get real crazy in multiple panes in the same window!).

And it is easy enough to change the default shell as well.

If one would rather a book that focuses on Zsh, Armin Bruegel’s mocOS Terminal and Shell is a good choice.

1 Like

This post on StackExchange gives a summary of the main deltas of zsh versus bash. From my own experience, they are just different enough to be annoying.

A popular theme among online discussions of zsh vs bash is that “Bash isn’t going anywhere.” This is undoubtedly strictly true but why, then, did Apple decide to switch? When I explored this at the time I came to the conclusion that it was probably a licensing issue which, if true, means that bash is likely to be removed from MacOS at some point. It will probably continue to be available via Homebrew and MacPorts, though.

For the purposes of this discussion, there is little enough difference between bash and zsh to matter because most users aren’t learning about the intricacies of the shell so much as they are learning about various CLI commands, that is, the programs that one executes using the shell.

3 Likes

Agreed :slight_smile:

One thing that I’ll mention is that I’ve decided that once a shell script becomes anything beyond a serial sequence of commands (that is, once it involves conditionals or itteration), I don’t even bother with shell scripts anymore. That’s when I pick up python, and I’ve never, ever regretted doing so. I find shell scripts great for one-offs and quick tasks, but if it’s going to be something beyond that I go with python.

2 Likes