To jump right in and start using it, go that way. →
↓ Or go this way to get some context.
Using →
, ↓
, & space
on your keyboard works the best.
The year Unix was invented. It's still one of the most-used operating systems in the world (just think: all of Google's servers use it). Mac OSX and Linux are based on it. When you use the command line, you're using your computer the way people used computers decades before colorful screens and the mouse were in use.
The Bourne Shell (abbreviated as simply sh) was created as a way to interact with Unix in 1977. Lots of other shells were invented later that added more features. You may have noticed that your Terminal says something about "bash"— bash is the Bourne-again shell and was first created in 1989. A lot of cool kids these days use zsh (z shell), if you care about what the cool kids do.
So why do people still use it?
Because it's easy.
That is, it's hard to write programs that use graphics. Writing programs that run in a shell, though, is easy peasy. So, there are all kinds of things that you can only do using shell.
Getting started in it is a bit strange, admittedly, but it opens up whole new worlds (it is a whole new world). Don't get overwhelmed, just take it slow. Any familiarity will be great, so if it starts feeling overwhelming, just stop for the day.
Open your My Documents folder (or whatever it's called these days).
The command line is a very simple way of interacting with a computer (simple
for the computer, anyhow!). You just type a command and then hit
enter
/return
on your keyboard.
This obviously requires a bit of memorization. But the internet is there when we forget.
One thing people need to do all the time is list files. ↓ This way to try it now! ↓
Type what comes after the dollar sign into your command line, and then
hit enter
:
$ ls
Compare it to what you see in Finder/Explorer. It's an old-school way of listing files!
↓ That way to learn more about listing.
Press ↑
on your keyboard. Now that you got your ls
back, turn it
into this & hit enter
:
$ ls -l
The -l
is called a "flag". You added a -l
flag to the ls
command,
which tells ls
to "long list" your files. This gives you all sorts of
info about your files, like who owns them, what you can do with them,
and the file size. Here's what it all means, if
you're curious (and brave).
One more thing about listing, right this way. ↓
All this time using your computer, and you never even knew it was hiding all sorts of things from you! Enter this:
$ ls -al
Now you've added two flags for ls
. The l
tells it to list "long",
the a
tells it to list "all". Not just the usual stuff, but all the
stuff that's usually hidden from you, too!
Hidden files start with a dot. They're often called "dotfiles". Notice the two "files" listed at the top, the single dot and the double dot. These aren't actual files. You'll learn more about them soon. →
In the days when the command line was created, folders were called directories. So to change directories, try this:
$ cd Desktop
$ ls
Now you're looking at your desktop on the command line. You changed into the directory called Desktop.
↓ This way to keep from getting lost.
In case you forget what directory you're currently working in, you can ask to be told:
$ pwd
This means Print Working Directory; it prints the directory you're working in to the screen. (When this command was invented, screens didn't exist, so it would have actually printed it!)
To get back to where you just came from, you could write cd
followed
by everything pwd
showed you, minus the /Desktop
. But there's
an easier way. ↓
To get into the parent directory, do this
$ cd ..
So that ..
that you see when you ls -a
is a link to a directory. A
link to the parent directory of the one you're in. If this all feels
really weird, try just cd
ing into different folders and cd ..
ing back
out for a while, hitting pwd
to see where you are in between.
..
isn't the only shortcut, though. ↓
Good coders are lazy. They do work up front to prevent long-term work. Here are some tricks to avoid typing:
.
means "the directory you're in now". ls .
= ls
; cd .
does nothing.
But .
is useful sometimes.~
(tilde) means "your home directory". You can cd
into really obscure
folders and easily ls ~
or cd ~
.cd
without anything after it (without any arguments) does the same
thing as cd ~
. It takes you home.*
(often called splat because "asterisk" is too long) is a
wildcard; try ls ~/D*
& ls ~/*s
.cd -
takes you back to the previous directory, so you can switch back &
forth between two easily.Renaming a file is the same thing as moving it. You "move" it from one place/name to another. Let's create a file:
$ touch tmp.txt
(touch
will create a file if it doesn't exist, or re-save a file that's
already there.)
move it:
$ mv tmp.txt ~/Desktop
and then rename it:
$ cd ~/Desktop
$ mv tmp.txt woohoo.txt
To learn to make copies, this way. ↓
Let's make two of something.
$ touch woohoo.txt
$ cp woohoo.txt amazing.txt
cp
makes copies. You just tell it the file to copy, and then the new place
and name to put it/name it.
&&
Run two commands at once with &&
. To make a copy and then list all
files (to verify it actually copied), you can do this:
cp amazing.txt fabulous.txt && ls
Make sure you use two "&"s, though; using only one does something veeery different! You'll be very confused if you only use one!
Using only one &
starts something in the background. Search online
to find out more about it.
Listing files is cool & all, but what if you want to see what's in them?
There are lots of ways to do this. Some of the most common are cat
(which concatenates files), more
, and less
.
You'll need a file to read! Download this file into your current working directory. Right-click the link (or control-click, or two-finger click) and select "save link as". If you can't remember where you should save it to, use pwd to remind yourself.
Now let's read it! ↓
Try three ways of reading it. Try these one at a time:
$ cat this-is-water
$ more this-is-water
$ less this-is-water
Oh no! Now your whole terminal window has been taken over by less
! And
the scrolling's all weird! What to do? ↓
So cat
and more
just plopped the contents of the file into standard
output, but less
took things over & messed up your scrolling. To get
out of it, hit q
. Phew!
Ok, but let's try it again, with a bigger file. Download this to your current working directory and then type
$ less my-father
At this point I must pause and tell you to hit Tab on your keyboard. Ta-da! Stop typing so much.
Now open it up with less
again, and let's learn how to scroll. ↓
j
and k
d
and u
space
and b
space
and b
scroll a page at a time. d
and u
scroll half a page.
e
and y
, line by line.
Now try reading the file with more
. You can use all of these same
scrolling commands. Lots of programs use them.
The command line has no safety nets! When you delete things, they're gone for good. No steenkin' trash, no steenkin' recycle bin. Just gone. So be careful!
But to get rid of the files you just downloaded, you can
$ rm my-father-moved-through-dooms-of-love this-is-water
Note that you just passed rm
two files to delete. You can list
however many you want.
But to rm
whole folders, you'll need a different trick. ↓
If you try to rm
a directory, rm
will get all whiny and say it can't.
But you can make it do it anyway by passing it the -r
flag. (The -r
stands for recursive, because if you're peculiar you might say that
you're deleting the folder and then recursively deleting everything in
it. Maybe?? Anyhow.) Try it out! Make a directory then delete it:
$ mkdir worthless-folder
$ rm worthless-folder
$ rm -r worthless-folder
You're now significantly more hireable! That's really all it takes. You're on your way to tapping into the unbridled power of the shell.
These were the basics. Play with what you learned! Have fun! Come back here when you forget things.
When you're ready, you can learn how the pros do it with the fledgling nerd's guide to the command line.