An Introduction to the Command Line: Linux Basics (and OS X)

In this ultra-basic tutorial, we’re going to introduce you to the Linux, Mac (OS X) and *Nix systems and show you how to move around your File System with the command line. This is going to be tailored to beginners, so if you know your way around the command line then skip this tutorial or check out the end of the post for a summary.

Okay, let’s get started!

All commands covered will be listed at the bottom for your convenience. run “$ man name_of_command” for more information on that command!

The Structure of Your Computer

Think of your computer as a box full of boxes…. or a folder with folders inside folders inside folders etc….

As a beginner, the most important thing to understand is what your filesystem looks like. Here’s an image to display visually what it might look like on Linux and Mac machines.

The image above shows you EVERYTHING that a normal user would interact with on a regular basis. All of that information above is in a single “partition”.

The most important thing to understand is it’s all just “files” or “Folders/directories” inside directories. Looking at the image above, we have the “Root” Directory or “/”. Inside the Root directory we have more directories like “/Applications”, “/bin”, and “/Users”. There’s more but that’s all that would fit on the paper.

The “/Applications” folder is where most of your Applications are going to be stored. Then you’ve got “/bin” where all your binaries are stored.

There’s also the “/Users” directory. This allows you to separate your stuff from your kids stuff, or from anyone else who may use your computer. So each user gets their own Desktop, and their own little world. (And each user shares some stuff too).

By using the terminal you can move in and out of all of these folders which for the most part make up your entire computer.

Technically, the image above is a picture of what a single “partition” looks like with a file system on it. Imagine it being like one large slice of pizza, and your entire hard drive is the whole pizza. When you log onto the computer, you get booted into a single slice of pizza. If you want to access the other slices you can “mount” them, but that’s another conversation.

Finding Your Terminal

Unfortunately there’s a lot of ways to get to your terminal depending on your OS and setup.

In Ubuntu (Linux), there should be a “Search” option at the top left. If you type in “Terminal” or “Command Line” there should be an option you can click on. Do a google search for your system if you can’t find it.

On an Apple computer (Mac/OS X) you can get to it by

1. opening “Finder”. Double click on Finder, then

2. click Applications, then

3. scroll down to the Utilities Folder and open it.

4. Inside you should see an application called Terminal towards the bottom. Double click on Terminal and your Command Line will open up, and you’re ready to start hacking!

Basic Commands

Note: Type everything AFTER the $

Note 2: To find more information on a command, type $ man name_of_command and q to escape the manual. So $ man ls or $ man rm etc…

You’re probably familiar with the “GUI” or “Graphical User Interface”, which is a visual way to do the same thing in a terminal.

For example: If you’ve ever made a new folder, or deleted a file by clicking or dragging and dropping, then you’ve used a GUI. You can do the same exact thing in a terminal.

The ls command

Now that you’re in a terminal, try typing $ ls which will print out the contents of the current folder. Think of “ls” as short for LIST. like… List everything in the current folder.

You’re probably going to see a little ~ sign somewhere before the dollar sign. The ~ is your “home” directory for that user, so when we run “ls” you’ll get all the contents in the users folder. My user is called “johncurry”.

Learn to Code for Free

Learning to Code is simple. You just need to learn the right stuff at the right time. We'll show you how to learn to code quickly and easily with our step-by-step guide.

envelope

If you somehow ended up in a different folder, don’t worry. Come back to this part if you have to at the end.

Now we’ll use “ls” again but with a “flag”. Type $ ls -a to see ALL the files including “hidden” files that have a period in front of them.

the -a flag shows you hidden files that you won’t always want to see.

Then there’s $ ls -l which shows you more information about each file/folder. It’s probably not important yet, but it will be soon. Try it out to see what you get.

You can chain flags together by just listing them one after the other. For example: to use ls with both the -l and the -a flag, run $ ls -la

the cd command

The “cd” command helps you move to anywhere in the filesystem quickly. cd stands for “Change Directory”. To move into a specific directory, you run $ cd name_of_directory.

There is a commonly used parent-child reference to describe a relationship between two things. If you look at the image at the top of this post, You’ll notice that “/” or “root” is at the very top. That means ROOT is the parent of everything.

Go one level down, and you’ll see “/Applications”, “/bin”, and “/Users” on the same level. That means they are CHILDREN of ROOT, and ROOT is their “parent”.

It also means that /Applications, /bin, and /Users are Siblings of each other because they live in the same folder.

If you want to move into the parent directory, then you run the command $ cd ..

Note: .. is a fancy way of saying “parent directory”

You can chain this command together to move just about anywhere you want very quickly. So let’s say you want to move up 4 levels in one command. All you have to do is run something like this: $ cd ../../../../

Now if you want to move into a CHILD directory, you’ll have to run something like this as an example: $ cd Users

You can get all sorts of crazy with this command. You can move up and down as much as you want in a single command. I can get from my ROOT directory to my Desktop in one command like this:

$ cd Users/johncurry/Desktop/

Practice Challenges:

1. try to find your way down to the ROOT directory, then back to the Desktop directory.

2. If you have multiple users try to see whats on their desktop without being logged in to their account.

3. try to LIST (with ls) the contents of a directory you’re not in. ex: $ ls /etc/apache2

mkdir, mv, rm, rmdir, and touch

Before we go any further, let’s move into our Desktop directory so we don’t delete anything important. I’ll briefly explain all the commands one after the other, then we’ll play around with them. I’m going to take you through this part quickly, and your job is to play around with it until you understand all the commands.

mkdir – creates a new directory

mv – Moves a file or folder to a new location

rm – Removes a file. Removes folders if you use a flag

rmdir – removes an empty directory

touch – creates a new file

Okay. Let’s play with these

Note: I highly recommend having a second Terminal window open so you can look at the changes as they happen. OR look at the folders with a GUI so you can see things happening.

1. Run $ mkdir testDir to create a folder called “testDir” (notice a folder pops up on the Desktop)

2. Run $ cd testDir to move into testDir

3. Run $ mkdir animals to make the “animals” folder inside of testDir.

4. Run $ touch cats.txt to create a file called “cats.txt”

5. Run $ touch dogs.txt to create a file called “dogs.txt”

6. Run $ mkdir enemies to create a folder called “enemies”

7. Run $ mv enemies/ friends to rename the folder “enemies” to “friends”

8. Run $ mv dogs.txt puppies.txt to rename dogs.txt to puppies.txt

9. Run $ mkdir friends/best to create a folder “best” inside “friends”

10. Run $ mv cats.txt friends/ to move cats.txt into the friends/ folder.

11. Run $ mv puppies.txt friends/best/ to move puppies.txt into the /best folder inside friends/

12. Run $ mv friends/best/ friends/bff/ to rename the “best” folder to bff

13. Run $ mv friends/cats.txt friends/kitties.txt to rename cats.txt to kitties.txt

14. Run $ cd friends to move into the friends folder.

15. Run $ rmdir ../animals to remove the empty “animals” folder.

16. Run $ mv kitties.txt bff/ to move kitties.txt into the bff folder.

17. Run $ rm bff/ to see that you can’t remove a folder with “rm”

18. Run $ rm bff/puppies.txt to remove puppies.txt from the bff folder.

19. Run $ rm -rf bff/ to remove the bff folder and EVERYTHING inside it. BE CAREFUL WITH THIS COMMAND.

20. Run $ touch 1file 2file 3file 4file.txt 5file.txt to create 5 files at once

21. Run $ rm 1file 2file 3file 4file.txt 5file.txt to remove 5 files at once

cat, nano

Now we’ll want to work on files themselves. let’s create a file, but instead of using “touch”, we’ll use a Text Editor: Nano or vi .

Run $ nano test.txt to open up a new file called “test.txt”. Then type in whatever you want. When you’re finished typing, look at the bottom to see all your options.

To save your edits, press CTRL+o (the letter o) and it’ll tell you at the bottom “The filename to write: test.txt” and you can press “Enter” or “return” to save.

to EXIT nano, press CTRL+X.

to VIEW the contents of a file from the command line, run $ cat test.txt

A more commonly used editor is VI, but we’ll cover that in its very own post. It’s not hard, but there’s a bit more to it.

Chaining Commands together

Last thing for this post! You can “Chain” commands together by using TWO “And” signs.

Examples:

$ mkdir catvideos && touch catvideos/catvideo1.txt && cd catvideos

WildCards

Wildcards aren’t commands, but they’re useful anyway. What if you’re in a folder with thousands of files and you’re looking for all the .jpg files? You can use the wildcard to get all the files ending in .jpg. The command $ ls *.jpg translates to something like “LIST all the contents of the current directory where the last characters are “.jpg”. It doesn’t care what comes before .jpg, or how long the file name is.

If you wanted all the files that start with “cat”, then you can use a wildcard at the end:

$ ls cat*

If you wanted all of the files that had the word “vid” SOMEWHERE in the filename, whether it’s at the beginning, middle, or anywhere… Then you can use two wildcards:

$ ls *vid*

You can use WildCards for a lot of things like removing files, moving them, etc… There’s quite a bit more you can do, but these are the simplest and most common uses. Here’s an image of me demonstrating all of these one after the other: I’ve already added some dummy files.

Conclusion

Hopefully that went well! If you made it to the end, then you’ve got a great start to learning how your command line works. Here’s a list of all the commands we covered:

$ man name_of_command – Shows the manpage for that command with all flags and description

ls – LISTS all the contents of the “current working directory” (the one you’re in on the terminal)

cd – Changes the current working directory

mkdir – Creates a new directory (folder)

rmdir – Removes an empty directory

mv – MOVES a file or folder to a new location. Also renames a file/folder.

rm – REMOVES/DELETES a file. Will delete a folder and everything inside it if you add the -rf flags. (-r and -f are two separate flags)

touch – Creates a new file

cat – Views the contents of a file

nano – a simple command line text editor. edits a file.

WildCards – Regular Expression character to get specific results from a command.

You must have an account and be logged in to solve challenges and lessons!

Submit a Comment

Your email address will not be published. Required fields are marked *