Getting De-railed

Anna's blog

Mastering Your Terminal -- Part I

Developers tend to spend a lot of time in their Terminals, and while most know all the basic commands to run, there are some that are less commonly used, but quite nifty to know.

In an earlier post, I mentioned that one can use command line to run a software update command: softwareupdate -l to see what updates are available:

Terminal, or more accurately, Terminal.app, is a terminal emulator program that provides a connection to your shell (most likely bash; run echo $0 to see what yours is running).

Technically speaking, running commands in Terminal is really running commands in a command-line interface that runs in a shell that runs in Terminal.

Now that we got that out of the way –

Some basic tricks:

  • Up or down key to navigate between commands. I use up all the time when navigating to a previously used command that is too long to type again.

  • !! to repeat last command (this will be specific to your Terminal window or tab in which you run it)

Ok, it is actually easier to press the up arrow once, but hey, saying bang bang out loud is a lot more fun.

  • Drop file name from Finder into terminal to get its path – it can be then copy-pasted into a cd command etc.

More ways to navigate Terminal using familiar shortcuts:

  • Ctrl + T to open a new tab within the same Terminal session.

  • Ctrl + F to search your Terminal’s tab or session (actual text output thereof).

  • Command + + or Command + - to zoom in or out.

  • Command + G to go to a next search match, Command + Shift + G to go to a previous match.

  • Control + C or Command + . to cancel a command. Rails developers use that a lot to stop Rails server from running, for instance.

  • Use TAB to autocomplete a directory name. Use TAB TAB to show a list of options when there are multiple matches.

Who am I, where am I, and what time is it anyway?

  • pwd to see user’s current directory:

  • whoami to see username of the owner of the current login/connection session in the shell (or in manual’s words, “display effective user id”)

  • While whoami is a very commonly listed command in all of the top-20-Terminal-commands-you-must-learn-now list, its cousins WhoAmI and who am i are less known:

(There is no manual entry on WhoAmI, and that seems like a pretty rare command in general.)

  • Another cousin who displays a more expanded version of who am i, effectivly showing all users that are currently logged in into a terminal session:

  • And yet another cousin w “displays who is logged in and what they are doing:

  • date to see current date and time:

How do I get around?

  • cd directory_name or cd path/to/directory to move to a different directory

  • TAB can be used with cd to autocomplete a directory name, but using an asterix (*) as a wildcard also works:

  • ?characters to search for a specific name:

  • cd .. to move up one directory, cd ../.. to move up two directories, and so forth.

  • cd ~ or just cd to go to your home directory.

  • cd / to go to the level of your file hierarchy (this is not the same as home):

  • cd - to go to the last visited directory:

  • Note on case sensitivity: generally, names of files and directories (so that cd octopress is the same as cd Octopress) are not case sensitive unless you have specially set up your system to be that way.

So what am I seeing in here?

  • ls to see a list of files in your directory (or ls directory/path for another directory)

  • ls -l to see a long list of files, including creation dates and access permissions, number of links in the item, etc:

  • ls -lh or ls -l -h (flags can be combined) to see a human readable output as long list. In this case, the only thing that changes is size in bytes is converted into units that make more sense such as kilobytes. Note that -h flag can be use with some other commands as well.

(Try running ls first, then !! -l, then !! -h to stack these commands. Or just press an up key and add those flags before hitting enter).

  • ls / (or any other ls command with a flag and /) to see contents of home directory. Note that normally, you have to be in the directory which contents you are trying to view.

  • du -sh directory_name to see size of a directory: