LearnCommand Line Basics


writes on February 14, 2012

After part one of the git for designers post, Kim Røen suggested in the comments doing a post with some instructions on using the command line. This is that post. We’re going to be focusing on the Mac side of things, but a lot of the concepts in here will apply to all command line programs as well.

What is the Command Line?

If you really want to learn about the history of the command line, you can check out the Wikipedia page on the topic:

A command-line interface (CLI) is a mechanism for interacting with a computer operating system or software by typing commands to perform specific tasks… This method of instructing a computer to perform a given task is referred to as “entering” a command: the system waits for the user to conclude the submitting of the text command by pressing the “Enter” key (a descendant of the “carriage return” key of a typewriter keyboard). A command-line interpreter then receives, parses, and executes the requested user command.

TL;DR: wall of text.

You don’t have to be a developer in order to get the full benefit using the command line. Being familiar with how the command line works can vastly simplify your daily life. Let’s say you’d like to move all of the files with a certain extension from one folder to another. Let’s also say that there are far too many to want to select them in Finder and move them to a folder called html_files. You could take a lot of time and do it in Finder or open a terminal and type in the following:

  mv *.html html_files

It’s pretty easy once you get the hang of it. We’ll walk through that example now, learning the commands we would need to build up to that.

What Does It Look Like?

If you’re on a Mac, launch Terminal. You can find it in the "Utilities" folder inside of your main "Applications" Folder. This is what you’ll see:


Isn’t that exciting? Well no, not very. In fact, the command line is pretty boring and generally not very pretty. It does, however, provide great power once you learn to use it.

Let’s Learn Some Commands

The main way that you’ll interact with the command line is by typing commands in. For the most part, it works like this (yes, there are a lot of exceptions):

[command name] [options] [arguments]

We’ll start our command line journey with the ls command. This is a command that lists information about files. Essentially, it shows you a list of files in the directory or folder you tell it to. Let’s try it. Back in Terminal, type in ls and press enter. You should see something like the following:

  $ ls
  Public                  Desktop                 Library                 Sites               
  Documents               Movies                  Source                  Downloads           
  Music                   Dropbox                 Pictures

That’s what my home directory looks like. Cool, right!

Let’s say you wanted slightly different output that had file sizes and other information. Most command line programs offer more options to do what you want to do through the use of arguments. These can also be called options or flags. They are usually prefixed with a - character (hyphen or dash) to tell the program that you’re submitting an argument.

Here’s what the ls command looks like with the flags for a long list all files (by default, files that start with a . character are hidden) with human file sizes:

[~]$ ls -alhF
drwxr-xr-x+ 57 jason  staff   1.9K Feb  9 16:27 ./
drwxr-xr-x   5 root   admin   170B Nov  2 14:21 ../
-rw-r--r--@  1 jason  staff    15K Feb  8 09:51 .DS_Store
drwx------   2 jason  staff    68B Feb  8 09:40 .Trash/
lrwxr-xr-x   1 jason  staff    42B Nov  2 15:34 .bash@ -> /Users/jason/Source/private/dotfiles/.bash
-rw-------   1 jason  staff   112K Feb  7 16:25 .bash_history
lrwxr-xr-x   1 jason  staff    37B Nov  2 15:36 .bash_profile@ -> Source/private/dotfiles/.bash_profile
drwx------  13 jason  staff   442B Feb 13 10:00 .dropbox/
drwxr-xr-x   3 jason  staff   102B Nov  2 16:35 .gem/
lrwxr-xr-x   1 jason  staff    47B Nov  2 15:34 .gitconfig@ -> /Users/jason/Source/private/dotfiles/.gitconfig
-rw-r--r--   1 jason  staff    13B Nov  2 14:24 .gitignore_global
lrwxr-xr-x   1 jason  staff    20B Nov  2 15:48 .gvimrc@ -> Source/dotvim/gvimrc
drwx------   3 jason  staff   102B Dec  8 18:28 .heroku/
-rw-r--r--   1 jason  staff    27B Nov  2 14:24 .hgignore_global
-rw-r--r--@  1 jason  staff   622B Jan  9 14:10 .hgrc
drwxr-xr-x   6 jason  staff   204B Nov  7 09:36 .ievms/
lrwxr-xr-x   1 jason  staff    45B Nov  2 15:34 .inputrc@ -> /Users/jason/Source/private/dotfiles/.inputrc
-rw-r--r--   1 jason  staff   349B Feb  7 11:18 .irb-history
-rw-r--r--   1 jason  staff   525B Jan 30 11:42 .irb_history
-rw-------   1 jason  staff   649B Jan 23 10:18 .mysql_history
drwxr-xr-x  69 jason  staff   2.3K Jan 12 00:40 .npm/
lrwxr-xr-x   1 jason  staff    50B Nov  2 17:20 .pow@ -> /Users/jason/Library/Application Support/Pow/Hosts
drwxr-xr-x   4 jason  staff   136B Nov 21 17:06 .rbenv/
-rw-r--r--   1 jason  staff   8.1K Dec  7 18:02 .rdebug_hist
drwxr-xr-x  27 jason  staff   918B Nov 21 17:07 .rvm/
drwx------   9 jason  staff   306B Nov  7 11:57 .ssh/
lrwxr-xr-x   1 jason  staff    13B Nov  2 15:48 .vim@ -> Source/dotvim
-rw-------   1 jason  staff    45K Feb  8 15:32 .viminfo
lrwxr-xr-x   1 jason  staff    19B Nov  2 15:48 .vimrc@ -> Source/dotvim/vimrc
-rw-r--r--   1 jason  staff   661B Jan 11 11:37 .vimrc.local
drwxr-xr-x@  5 jason  staff   170B Feb  8 09:40 AeroFS/
drwx------+  4 jason  staff   136B Feb  7 16:26 Desktop/
drwx------+ 21 jason  staff   714B Feb  7 16:26 Documents/
drwx------+  6 jason  staff   204B Feb  8 10:09 Downloads/
drwx------@ 51 jason  staff   1.7K Feb 13 09:48 Dropbox/
drwx------@  8 jason  staff   272B Jan 19 13:51 Insync/
drwx------@ 49 jason  staff   1.6K Feb  7 15:23 Library/
drwx------+  5 jason  staff   170B Feb  2 16:19 Movies/
drwx------+  6 jason  staff   204B Feb  2 16:19 Music/
drwx------+  7 jason  staff   238B Feb  2 16:19 Pictures/
drwxr-xr-x+  5 jason  staff   170B Feb  2 16:19 Public/
drwxr-xr-x   5 jason  staff   170B Feb  2 16:19 Sites/
drwxr-xr-x   9 jason  staff   306B Nov 29 15:32 Source/
lrwxr-xr-x   1 jason  staff    40B Nov  2 15:34 bin@ -> /Users/jason/Source/private/dotfiles/bin

You can look up the arguments to ls here. The path when using the ls command is optional. If it’s not submitted, it defaults to the current directory. You can add another path after the arguments if you choose to get a file listing of a directory other than the current one.

Some More Commands

If you want to change the current working directory, you can do so with the cd command. If you’re in your home directory and want to change to. If you wanted to enter your Sites directory, you would type the following:

  $ cd Sites
  [~/Sites] $ 

That would put you in the Sites directory. The output you see above [~/Sites] $ is because I’ve modified the built in prompt. If you don’t have that in your prompt (you probably don’t) and forget where you are, you can always use the pwd command to remind you:

[~/Sites]$ pwd

Accomplishing Something Useful

Let’s say I wanted to duplicate one file as another file without using the Finder. For that, you can use the cp command. It takes two arguments: the path of the file you want to copy from and the path of the file you want to copy to. Let’s say I was in the think_vitamin directory from the Git for Designers article and I wanted to copy the index.html file to homepage.html. I could do the following:

[~/Sites]$ cd think_vitamin/
[~/Sites/think_vitamin master]$ cp index.html homepage.html

That would duplicate the index.html file to homepage.html file. If I have a change of heart and decide to rename the homepage.html file, I can use the mv command:

[~/Sites/think_vitamin master]$ mv homepage.html home.html
[~/Sites/think_vitamin master]$ ls -alhF
total 16
drwxr-xr-x   5 jason  staff   170B Feb 14 12:50 ./
drwxr-xr-x+ 36 jason  staff   1.2K Feb 12 12:01 ../
drwxr-xr-x  10 jason  staff   340B Feb  1 01:57 .git/
-rw-r--r--   1 jason  staff     3B Feb 14 12:46 home.html
-rw-r--r--   1 jason  staff     3B Feb  1 01:57 index.html

Let’s say I was trying to organize files by their type. I’ve decided (for some reason) to move all html files to the html directory. I could move every file individually in Finder. Or, I could use something called the wildcard character and do it on the command line. The wildcard character is represented by an asterisk. Here’s what it looks like:

[~/Sites/think_vitamin master]$ mv *.html html/
[~/Sites/think_vitamin master]$ ls -alhF
total 0
drwxr-xr-x   4 jason  staff   136B Feb 14 12:51 ./
drwxr-xr-x+ 36 jason  staff   1.2K Feb 12 12:01 ../
drwxr-xr-x  10 jason  staff   340B Feb  1 01:57 .git/
drwxr-xr-x   4 jason  staff   136B Feb 14 12:51 html/

Now let’s check out the html directory:

[~/Sites/think_vitamin master]$ ls -alhF html/
total 16
drwxr-xr-x  4 jason  staff   136B Feb 14 12:51 ./
drwxr-xr-x  4 jason  staff   136B Feb 14 12:51 ../
-rw-r--r--  1 jason  staff     3B Feb 14 12:46 home.html
-rw-r--r--  1 jason  staff     3B Feb  1 01:57 index.html

Getting Help

Invevitably there will be times when you get stuck or need a little refresher. The authors of most commands and programs knew that would happen. Accordingly, most programs allow you to specify either -h or --help as an argument. This will usually bring up a small help screen that displays the list of commands and arguments a program takes and give you a short description of each.

If you’re on a Mac or linux system, the man command will give you a much more in depth description of the options available. Not every program ships with a man page but most do. Reading through the man page will inevitably be helpful for understanding the command itself and all of the options. Often, man pages will include examples of correct usage, which is extremely helpful when you’re stuck.

If you’re still stuck, there’s always the option of doing a web search. Just type in the name of the command and the problem you’re having in to Google. Stack Overflow is another great resource for both finding help and looking up answers to your problems. Of course, Treehouse walks you through using the commands you need to accomplish tasks while earning badges.


Learning with Treehouse for only 30 minutes a day can teach you the skills needed to land the job that you've been dreaming about.

Get Started

3 Responses to “Command Line Basics”

  1. @Hope:

    This is an issue with your Mac.

    Here’s the solution:


  2. I tried to do the commands listed in this article in my terminal but my mac said those commands don’t exist. I just purchased my mac this past summer so it may have updated commands but when trying to follow along with the directions in your article, the “1s” command doesn’t exist, and when I typed “man”, it asked me what manual did I want to see and didn’t give me a list like this article said it would. Also, when I tried to type “-h” or “–help”, my mac again said that command doesn’t exist. Experimentally, I simply typed in “help” with no dashes and it gave me the list of commands. So I just wanted to let you all know that not every mac’s terminal has the same set of commands. Or maybe it’s just time to update the article. Who knows? I’m just starting with all this so I don’t know what the problem is. Just thought I’d let you know. Thanks!

    • Is it possible you typed in 1s instead of ls? the file list command is the letter l, not the number 1. And then from there you could type “ls man” or “ls -h”

      Hope that helps.

Leave a Reply

You must be logged in to post a comment.

man working on his laptop

Are you ready to start learning?

Learning with Treehouse for only 30 minutes a day can teach you the skills needed to land the job that you've been dreaming about.

Start a Free Trial
woman working on her laptop