Unix (BSD/Mac OS X)

There are a lot of Unix tutorials out there, many of them already well-developed by people much more experienced than myself, so I will dispense with any of the basics here. Instead, this is more of a cookbook-style page that shows some of the more clever tricks I have come across or created on my own. I have really only just started using the command line on a daily basis, so I expect this collection to grow.

Copying Files

cp is easy to use…

cp target_file destination_folder

…but rsync can be much better, I think:

rsync -ar --progress target_folder_file destination_folder

This lists the files and shows their copy progress, preserving perms along the way.

Finding Files

A better use of ls

ls on its own is too simple to be useful over the long term, for me at least. So I updated my profile file to give me more of that I see in the Finder: owner, domain, last modified date, a trailing “/” at the end of a name if a directory, and the like.

$: ls -lAph
drwxr-xr-x    4 username  userdomain   136B Aug 15  2012 Applications/
$:

Copying Files

Globally tailoring commands

I have learned the hard way that editing the ~/.profile file should be done carefully and judiciously but can allow the power that lies behind the prompt to be used at every turn. When updating a command, the entire path to the executable for that command must be used. For example, to have ls use the full string of arguments ls -lAph every time, then in .profile add the following, which creates an alias for the command ls having it use a robust set of arguments.

alias ls="/bin/ls -lAph"

Change the prompt to something more useful

There are countless ways to change the prompt, but I took this from an O’Reilly title. The following prompt format shows the current active directory and the number of commands entered. Editing the \$ can change the prompt token whatever (“By your command” is clever but long-winded; best to stick with dollar sign. I tried).

PS1="\w (\!) \$: "

Having the current working directory in the prompt is immensely useful and has helped save me from my own confusion. It gets a bit onerous when I go really deep into a folder structure, but that’s rare.