Favorite Terminal Commands

This is a list of my favorite terminal commands, aliases, and customizations for MacOS and Linux. 

MacOS Terminal

Open

In MacOS, the open command will open files and directories just as if you had double-clicked the file's icon. The default application for that file type/extension will be used if you do not specify an app by using the -a flag. 

Here are some examples 

open . Opens the current directory in Finder
open -a Firefox https://theverge.com Opens the specified website with the specified browser

And here are some useful aliases for the open command

alias ff='open -a Firefox'
alias ffd='open -a FirefoxDeveloperEdition'

One of my favorite uses of the open command is combining it with a list and/or search function. For example, if I wanted to open the last file that I downloaded, I can use the following command: 

$ open $(ls -t | head -n1)

The one caveat with using this type of command is that it will not work if the file's name has space in it. 

Find more information and options for the open command here.

pbcopy, pbpaste

Provides copying and pasting to the pasteboard (the Clipboard) from the command line

 

Grep

Full usage of grep, and Regular Expressions in general, is way out of scope for this reference page, but creating an alias that will automatically turn on color output is very useful. 

alias grep='grep --color=auto'

Rename

Changes the name of a file or files. Both the old and new must be of the same type, that is both must be either directories or files and must reside on the same file system. 

Here is an example: 

$ ls
this_has_text_to_find_1.txt
this_has_text_to_find_2.txt
this_has_text_to_find_3.txt
this_has_text_to_find_4.txt

$ rename 's/text_to_find/been_renamed' *.txt
$ ls
this_has_been_renamed_1.txt
this_has_been_renamed_2.txt
this_has_been_renamed_3.txt
this_has_been_renamed_4.txt