This post is about how to correctly flail your arms at a terminal and get some results! It’s for people who are wanting to travel into the matrix for the first time and experience computers from the mysterious puzzle world of the command line interface (CLI). If you are familiar with the basics then this article is not for you, but feel free to share it with a friend (or stranger) who is new to computers 🙂
PLEASE NOTE this post is for people using Mac OSX/Unix-based machines. If you are using windows, there is this to show you how to get ubuntu-mode going on your ‘puter (these instructions may be hard to follow, I’m not sure… if you know of a better resource for setting it up, let me know!)
Here’s the tl;dr of this post:
Where am I? pwd
What’s around me? ls
How do I get around cd place/ (forwards), cd .. (backwards)
What does this command do? man
How do I create a folder? mkdir folder_name
I want to copy something cp thing new_thing
I wanna move a thing mv thing new/location/thing
I wanna to delete a thing rm file, or rmdir folder, or rm -r folder
Show me the contents of a file cat file, less file
I want to search for keywords cat
How can I do that command again? up-arrow, !!, or history
Permission denied? sudo (🚨danger mode🚨)
How do I clean up this mess? clear
How do I edit things? nano or vim (i for insert mode, esc for execute mode, 😡 for save and quit)
I tried running a program and it won’t stop ctrl+c
Alright — Welcome newbies!
Let me be your guide into the great Labyrinth of Bourne Again Shell (bash). I will help you bash your way to a electro-fusion hacker paradise. I am going to teach you some stuff (commands) so you too can be a swag flower queen hacker.
Pay attention to the stuff in the brackets — those acronyms are really important. They are what people usually refer to things as. One thing you’ll notice is that there is A LOT of jargon in the form of acronyms/initialisms. The reason for this is because computer-related concepts are quite abstract and it can be pretty hard to describe things. Biology went for the latin naming approach, but in the cyber-sphere people went for long descriptive names that get compressed into initialisms. I apologise on behalf of the whole industry for the overuse of acronyms, initialisms, and pseudo-blends.
Get into my warp-drive flux-traveller-bits-mobile, we’re going to the silicon transistor jungle. Now get in quick – here we goooo
Where am I?
When you wake up and find yourself in a hazy g̷litc̴h́-fog. You’re disorientated and you can’t remember how you got to where you are. Don’t worry, I’m here, let me take you under my wing and I will show you the way…
Firstly, there are a couple of things you need to know. This is what you’re probably seeing:
What this means:
“oofeekee” is the computer name, “~” is the directory, and “gracenolan” is the username.
Directory? Huh? It’s useful to know that in the matrix, folders are called directories and files are called files. I use a mix of “folder” and “directory” because I’m OFF THE CHAIN CRAZY.
Okay, time for your first command!
Print Working Directory
(“print” is the term used to describe displaying something on the screen)
This is a Path! This is like Hansel and Gretel’s breadcrumb trail but luckily in this techno-realm the wizards have figured out a way to stop the birds from eating all the crumbs. There’s also no evil witch that’s going to eat you. Well. Probably not. There might be some times where you might feel like you’re being eaten by a witch. Computers can inspire the kind of rage where you might be tempted to eat your own face off (if you’ve ever used a computer before you will know this feeling already).
What’s around me?
Now you know where you are, what is around you?
This is the list of the things in your current folder/directory
Things with an extension (.txt, .py, .whatever) are files, things without are usually a folder/directory.
Sometimes there are are rare hidden objects: ls -a You will notice these things start with a . This is the notation for hidden items. It’s how your computer knows what to show the user and what to hide. Hidden things are hidden because it’s probably safer for your great grandpa not to see them. But not YOU. YOU’RE more than that. You’re an average ordinary superhuman hacker-in-training.
Explore the area
So now you might be feeling a bit more comfortable and a little bit bored. Let’s explore a little further — It’s time to leave the nest and explore more of the map!
Let’s take a look through the mysterious door
cd directory_name/ (go to the place)
cd .. (go back up a level)
cd ../../directory_name (go back two levels then down one level)
cd (go back to the home directory)
If you wanna move into a different part of the map/tree/area, this is how you do it!
And another thing that’s cool is that if you start typing a name, you can slam the tab key on your keyboard and it will autocomplete the name of where you’re trying to go (as long as it is in the area). If you hit tab TWICE then it will show you a list of things around and then you can finish writing the name of the place you’re trying to explore.
Hey, Look! A pocket manual
This is the Pokédex equivalent for bash commands! It’s an ancient tome of forbidden knowledge and you can use it to gain eldritch powers.
Whenever you want to know more about a thing and about some of the possible command line options, you can go:
‘man’ is short for user manual1
This will tell you what the program does and some of the options. Now listen, entries are written in an ancient dialect of anglo-latin created “for the warlocks, by the warlocks”. It’s okay to have no idea what these pages are saying. Over time you’ll figure it out and it will make your terminal-navigation powers even more powerful!
You can even type man man and you’ll learn more about how it works
Hit q to exit the man page
How do I build a crazy maze-world of folders-in-folders-in-folders?
This command is how cities are built. This is how an empty expanse is turned into a labyrinth. It’s for creating folders.
Mkdir [folder_name] is how it goes. If you want to put a space in your folder name, use a \ before the space. This tells the terminal to include the space as part of the name instead of a separator. I like to use one word names or underscores instead of spaces when I’m in terminal, it keeps things kinda simple.
Cut, Copy & Paste
This is our good ol’ buddy, ctrl+c ctrl+v. It’s the two keyboard shortcuts that even your dog knows. It’s one of the many reasons why people 💖 computers. It’s SO EASY just to move stuff around and duplicate stuff. If only it was easy to do this IRL. Let’s have a moment to appreciate how cool and useful copy&paste is.
You can rename the thing you’re copying as well if you like! Just specify the name on the destination
This is ctrl+x ctrl+v. It’s our cut and paste option. It is ALSO the way you rename things! This works by copying the file/directory and then deleting the old one. When it creates the thing in the new place you can give it whatever name you like
Deleting and Flipping Tables
This is the command for when you are ready to spring clean, flip tables, exercise demons, or launch nukes.
rm -r folder
The -r means to run the command recursively. This essentially means to go down into all the subdirectories and delete all the files in those and then work upwards until everything is gone. -f is another argument that means to suppress confirmations before deleting files - so it will delete regardless of file permissions, but be careful using -f.
If anyone tries to be funny and tells you to run rm -rf / DO NOT LISTEN. They are lucifer in the form of a snake in the form of an internet troll! This command wipes everything you know and love: your excel spreadsheets, your unfinished masters thesis, your family, your friends, your game saves. Yep. It’s that serious. The / is short for “go to the very root of the tree - the top directory”. So it will recursively (-r) go through ALL of your files, and deleting them without asking for confirmation (-f).
Show stuff in terminal
Wanna know how to show what’s in a file? These are the three commands I use the most for this
This prints the file contents to the terminal. It’s called cat after “concatenation”. Concatenation means to join things together. In computerland it is a word that comes up a fair bit. The cat command can be used to merge two files together, but I usually just use it as a way of displaying the contents of a file, or from grepping things (I will get to what grep is shortly!)
less is more
Opens a viewer and makes it easy to scroll through larger files. It got it’s name from another command called more, but has more functionality (for example, in more, you could only scroll downwards through files but you can scroll both up and down with less).
How do I find specific words?
There are two components that are useful to know about when it comes to searching for things: grep and | (pipe)
g/re/p (globally search a regular expression and print)
grep "search term or regular expression"
Grep is a way of searching for keywords in stuff. The “globally search” part in the name means that it looks for all of the words matching the regular expression. And “print” means to output the result of the search.
What’s a regular expression, you ask?
WELL. It’s a pattern, and it will successfully match text that fits that pattern. The name “regular expression”, usually shortened to “regex” or “regexp”, comes from some pretty serious computer science theory. The mathemagicians that came up with it had EXTREMELY long beards and deeply furrowed brows. Usually you will just use it with words in the same way that you expect keyword search to work (looking for exact matches), but with regex, you can do SO MUCH MORE.
Say you have a text file and you are looking for all occurrences of “someone”, “somebody”, and “something”. You can get results for all of these words by searching with “some”. The * is a special character that means “match with anything and everything”
input | output
Now string these all together
The pipe is used to funnel output/results from one program to the input of another. It’s pretty useful! When you use this with grep, it takes whatever text you pipe into it, and will do its pattern matching magic, printing out the results.
Say it again!
Instead of typing out the whole spiel again, you can use up arrow to load in the last commands you’ve typed into your terminal shotgun, ready to be triggered by that return key.
You can also use !! to repeat the last command you did.
history will give you a full list of commands you've recently used2. The commands have an index number.
If you type ! and then the index number (so like !54 for example) and hit enter, it will rerun the command at that index
You can search your history for keywords by using history | grep “keyword” (I will explain what this whole “| grep” thing is in a bit)
You can press ctrl+r and start typing part of the command. It will search your history for the latest command, once it’s found it, then you can press enter to run that command
Now say it again, but with more force!
So say you stumble upon a gate and the snotty sexist trolls guarding the gates won’t let you through. What do you do now? Well, there is a spell that you can cast that will make you look all glowy and important.
In computerland there are places and spells that are restricted. But you can go from a regular citizen to royalty with this powerful command.
It’s important to be careful with this kind of magic. It can land you in BIG TROUBLE. Use sudo only if you really know what you’re doing. Otherwise you might accidentally change/delete something that is really important. You might install or execute something that might do some things that you reeeallly didn’t want it to do.
super user do
(super user is like a regular user login, but with more super — it’s like going from regular woman to Wonder Woman)
If you just tried to do a thing and it didn’t let you. Then you can combine sudo with a trick we just learned: sudo !! will run the last command you just did, but this time you're saying it as Wonder Woman instead.
It’s the Simon Says principle. When you ask for something the first time, and it doesn’t work, you can say “Simon Says” and you might get a little further 😀
Now everything is messy and I don’t even know what’s going on anymore
At some point your terminal screen is going to be filled with garble and magic dust from all your spell casting you’ve been doing. This command will tidy up the mess.
clear the screen
You want to try your magic powers at creating and editing a thing
Okay. So. You can do this the hard way or the easy way. If you just want to get started, the use nano. If you're willing to learn on hard mode (which I recommend, because, you know, who DOESN'T want to be hardcore?) then you can try out vim.
Nano is a command line text editor with instructions
Now, this will open “file”. If “file” doesn’t exit, it will create and empty file called “file”.
Nano is pretty easy to use because when you open it, it comes with instructions. This is very helpful for beginners! This kinda seems like a pretty obvious useful thing — you know, to come with instructions. But it’s something that one really comes to appreciate when faced with our next text editor option, vim.
vim is a command line text editor that expects you to magically and spontaneously know how to use it.
Now, buckle your seatbelt kiddo, this will get a little wild. There are some magical text editors that you can use to create and edit files. One that I like using is vim. Vim is Vi IMproved — it is based on a text editor called ‘vi’. Now, young caterpillar, this next part is going to be difficult. Vim is very powerful advanced magic — and unfortunately it is something that takes time to become a master at.
NOW STOP! Don’t just start typing because you’re going to have a BAD TIME. It will be like trying to feed steamed broccoli to a screaming toddler. You’ve gotta do the airplane before they are even going to CONSIDER eating that green garbage. By default you’ll be in the spell casting mode. To start typing stuff, press i. i is for insert mode. This is when you can write spells or memoirs or letters for hackers to find. To exit press esc then :wq for save (write) and quit (or :xworks too)- :q! for quitting without saving.
Now, you’re probably going to forget how to quit Vim. In fact, the question “How to exit the Vim editor?” on Stack Overflow (a popular website where people can get help for their technical questions) just reached a milestone of 1 million visits. (it’s esc then :wq or 😡 or :q!)
You can do some really cool stuff when you’re not in insert mode. Like to delete a single character, you can press x, or dd to delete a whole line. And if you need to undo, the press u. When you delete a line you can hit p and whatever you just deleted will paste in the line below (P (shift+p) will paste in the line above). y is for ‘yank’, which is vim-wizard-lingo for ‘copy’. This will copy the whole line you’re on, then you can p paste it where ever.
Summary of commands:
i - insert mode
:q! - quit without saving
😡 - save and quit
x - delete character
dd - delete line
u - undo
p - paste line below
P - (shift+p) paste in line above
y - copy (yank)
Well, Kiddo, if you managed to catch all those vim commands just now then all I gotta say is WHO ARE YOU AND I AM NOT COMFORTABLE WITH YOU ON MY INTERNET LAWN. It’s a true fact that 100% of people can’t learn all of that on their first day. Some people will try and learn one new vim command a day and then they will do that for a year, and even then they will still know only a portion of vim’s power.
As for me, I am happy enough getting around with the few commands that I know, and often I find myself thinking “there must be a more efficient way of doing this?” I will consult the Government Of Organised Official Goodly Linkage Elders (Google) and will learn (then promptly forget) new vim commands.
Part of the reason that makes vim so tricky to learn is that is has “modes”. There is a “insert mode” where you can type freely, and “execute mode” where you can use the commands (this is the mode where most of the magic happens), and “visual mode” for selecting text. Our brains don’t usually recognise things like modes. It’s not intuitive. You can read all about this here.
I recommend downloading a cheatsheet and making it a desktop wallpaper. Here’s a pretty alright one! There’s also vimtutor command which comes default with OSX, it’s an interactive vim tutorial and will teach you way more things than what you get taught here.
Now my sweet butterfly-to-be, go forth! Explore! Practice these magic arts!
How does all of this work? Has anyone really been far even as decided to use even go want to do look more like?
A command is a program. Usually they are small programs that do one thing really well. They probably take arguments too. Now these aren’t your Dragon Ball Z fighting-style arguments, these are more like lawyer-style arguments. Like with ls, when ls is followed by -a, you are giving it an argument that tells the ls programme to reveal the hidden items. You could also go ls directory/ and that will tell ls to list the items in that directory instead of the one you're currently in.
The shell will just try and execute whatever you give it. If you run a program and nothing seems to be happening and you don’t know what’s going on, the ctrl+c will stop that process. Slamming ctrl+c will send a signal to interrupt the process and terminate whatever it is was that you were trying to do.
crash the program
If you run a command and regret it and it’s still running and you wish you knew how to STOP IT but you don’T knOw aND evERythINg is CHaOs haAalp — then ctrl+c is what you're after.
It’s like a panic button, and will crash whatever program you were running.
Well that was an adventure!
Hey, look! You made it! You did the things! You are now a certified computer badass. Using terminal is like wearing some slick shades. You look cool. It’s also critical to know for doing things with IoT (Internet of Things), or learning how to hack stuff, or tricking people into thinking you know lots about computers.
- man is also short for mansplaining ↩︎
- If you happen to be on a shared computer or someone else’s machine and you’re, uuuh, interested in The Dark Arts and want to cover your tracks or maybe you don’t want anyone else to see your flubs, then you can edit your bash history nano ~/.bash_history ↩︎
Written By Grace Nolan