How I Use the Shell

I am by no means a master at programming the shell. I'm not even sure my skills would be considered novice. However, even with just a few commands, the Terminal/Unix Shell of the Mac can be an incredibly powerful tool. Things get more exciting when you couple it with Keyboard Maestro.

A few of my most common shell commands include grep, split,find, mv,cp cat, wc, head, tail and rsync. Even with just these few you can perform some very clever tricks and save yourself a ton of time.

Grep is used to find text within a text file. That may not sound like much until you try it with a .CSV file that's 1.5GB in size. Go ahead and look for a customer, SKU, or phone number in that mess. With grep it's light work and results can be piped off into a much smaller file you can open.

In fact, grep was my re-introduction to the Shell when using the Mac. The other member of the QA team was trying to open a gigantic file in Excel, and failing miserably. It had millions of lines, far more than Excel could parse. The better choice would be a database, but all he needed was to confirm if an entry existed. His goal was to open it and use AutoFilter.

After cringing at his plan, I took the file and the items he wanted to find and exported each result to a separate file. I sent those 2-5k files back to him a few minutes later and the problem was solved.

Split does what the name implies. It splits a file into smaller parts. If you actually need to open that 1.5GB CSV file, you can use split to make a dozen small files that can be opened. That has come in handy multiple times. If grep can't save you, split might get you out of a jam.

Find searches for files that match a certain criteria. I use this to find files of a certain size or certain type. There are multiple options, so you'll need to do some experimenting, but it's incredibly useful.

mv and cp are for moving and copying files. The power comes from using wildcards. I use this to move tons of files around quickly without having to select each and every one of them. Sure, there are tools to handle that, but there's no need to buy additional software when it's built it. mv *.doc is far faster than trying to click them all by hand.

rsync is a fantastic tool for backing up files. I use this to back up my DevonThink Office Pro files and documents to an external drive each week. The reason for doing it this way is that I can then copy those files to my home machine and not overwrite anything. I want everything from the work machine on the home machine, but not the other way around. This keep my home Mac up to date without losing any differences between the two. It also removes the backup once the process is complete so I don't run out of space.

Cat is used to view and concatenate files together. Easily put together a list of results.

Wc is "word count" and is great for checking the number of lines in a file.

Head and tail show the first X number or last X number of lines in a file. Excellent way to see headers, or make sure you have a complete file without having to open it.

While these commands are extremely beneficial, you can make them even more powerful and easier to use by combining them with Keyboard Maestro.

For example, I have the above sync scheduled to occur on Friday. The timing and action are handled by Keyboard Maestro. I get a prompt to connect the drive, the sync occurs, and everything is handled effortlessly. It took a bit of searching to get the right command and switches, but now it's set up and I don't have to worry about it. Keyboard Maestro adds the functionality of timing, prompts, and execution.

The same is true for mv and cp. I created a small UI using Keyboard Maestro to select common folders, set up wildcards, and pass the command over for execution. I don't have to remember the syntax and get everything in the right order. Again, I set it up once, then use it dozens of times to organize files and folders.

The Shell is it's own programming language as well. So commands can be chained together, variables set and passed, output from one used as the input for another. That is definitely a skill, but absolutely possible and incredibly powerful.

There are an amazing number of applets built into Unix. It may take some digging to find the right one, and a bit of experimentation to get the right options, but they are very well documented, along with hundreds of examples on various sites and forums. There are commands in the Shell that can easily save you having to buy an application.

This space for rent.

Author Signature for Posts

0