Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

I'm sure you have seen TV shows and movies in which a super awesome programmer saves the world by widely typing commands into the computer (or a super villain tries to destroy the world by typing deadly commands). It might look like magic but it is actually not that hard; you don't have to be a genius to learn it. This page will explain the basics of using a terminal and what you can do with it. Don't Panic! It'll be aright.

...

On a Windows computer, you can click the Start button, click "All Programs", click "Accessories", and then click "Command Prompt". Or you can click the Start button and then in the Search box, type "cmd". From the list of results, double-click "Command Prompt".

Info
titleSetup

If you want to try out the examples given in this article, using the Finder, create a folder called "test" on your desktop. Create another folder called "subfolder" inside the folder "test". Then, using a text editor, create a file called "file.txt" inside the folder "test". Your folder structure starting at your desktop would look like this:

Image Added desktop

Image Added test

Image Added subfolder

Image Added file.txt

Navigating the File System

...

On a Unix-based OS, your root directory is "/". On Windows, you would select what hard-drive you want to look at by using its name (e.g. "C:\"). To tell your computer about a specific folder or file you specify its path from the root directory (there are other ways, but for now let's go with this). For example, on a Mac, you home folder would have the path "/Users/your-user-name"; on a Windows computer, it would be "C:\Users\your-user-name".

Note
titleNote

Unix-based systems use forward slashes "/", while Windows uses backward slashes "\".

So let's walk around in our file system a little bit. You know how you can move from one folder to another in your Finder or Explorer? You can do the same thing in a terminal, but instead of clicking you type where you want to go. And instead of automatically seeing what a folder contains, you have to type a command to tell the terminal to show you the content of a directory. Let's go!

ls

The first command you need is ls, which stands for list (files/folders). Simply type ls into your terminal window. If you're on a Windows computer, type dir instead. You should see an output similar to this one:

Image Modified

By default, when you open a new terminal window, you will be in your home directory. Hence, you should see a list of all the files and folders in your home directory. You should see something similar on a Windows machine, just with a different layout with some more information such as the creation date and time. You can get a similar output on Mac by typing ls -l. You should something like this:

 

The output might look intimidating and like gibberish at first, but it's actually not. Let's take a closer look. The first column tells you several things:

...

will move you into the folder "test", which is inside the folder "Desktop" of the user "jdamerow". If you want to try this out yourself, create a folder test on your desktop, and then adjust the path in the example by replace "jdamerow" with your username.

...

Note
titleNote

If you executed the first command that uses the absolute path to the test folder, you first need to go back to your home directory. You can always go back to your home directory from anywhere by typing:

Code Block
languagebash
cd ~

or simply cd (in Unix-base systems).

...

Tip
titleTip

You can use the "tab" key on your keyboard to autocomplete file names. Simply start typing what you are looking for (for example "De") and hit the tab key. The terminal will autocomplete the folder name to "Desktop". If there are several folder/files that start with "De" hitting tab once might not say anything. In that case hit "tab" twice and the terminal will show you all files/folders that start with "De". Type another character or two (so that the command says cd Desk) and hit "tab". If the nothing happens even if you hit "tab" 2 or 3 times, it means that there are no files starting with "De".

mv

Now, that you know how to walk around in your file system, let interact with it. Anything you can do in a finder, you can do in the terminal as well (and more). Let's start with moving files around. The command for that is mv followed by the file you want to move and where you want to move it to:

Code Block
languagebash
mv file new_location

For example, if you want to move the file "file.txt" in the test folder into the folder "subfolder", you can write:

Code Block
languagebash
cd ~/Desktop/test # making sure you are in folder "test"
mv file.txt subfolder # move file.txt into subfolder

and to move it back again:

Code Block
languagebash
mv subfolder/file.txt . # moves the file file.txt from subfolder to the current directory (.)

You can also use mv to rename files. For example, to rename file.txt to file1.txt, type the following:

Code Block
languagebash
mv file.txt file1.txt 

You can combine both actions moving and renaming by specifying a new filename while moving:

Code Block
languagebash
mv file1.txt subfolder/file.txt # moves file1.txt to subfolder and also renaming it to file.txt

cp

Besides moving and renaming files, you can also copy them. This is done by the command cp. Simply provide the file that you want to copy and the name (and location) of the new file. For example, let's copy file.txt (which, if you followed the previous examples, is now in subfolder) into our test folder:

Code Block
languagebash
cp subfolder/file.txt file_cp.txt # copies file.txt to a new file called file_cp.txt
cp file_cp.txt ../file_cp.txt # copies the file created in the last line onto the desktop

If you want to copy a folder, you need to provide the -r option (for recursive) that copies a folder and all its contents recursively:

Code Block
languagebash
cp -r subfolder subfolder2 # copies subfolder with all its contents to a new folder folder2
Info
titleExercise

Try the following: copy a file randomly selected from your computer (that is not on the desktop but preferably in a different subfolder of you home directory) into your test folder.

 

 

...

rm

Of course, you can also remove files through the terminal. The command for that is rm. To remove a file, simply type rm filename:

Code Block
languagebash
rm file_cp.txt # removes the file file_cp.txt in the current folder
cp subfolder/file.txt file_cp2.txt # copies file.txt to a new file called file_cp2.txt

If you want to remove a directory, you can use the same command but have to specify the option -r (for recursive) similar to the copy command. This tells the terminal to recursively delete the contents of a folder and then delete it.

Code Block
languagebash
rm -r subfolder2

mkdir

Last but not least, let's take a look at the command mkdir (make directory). You can use that to create new folders. For example, if you want to create a new folder called "mysub" into your test folder run:

Code Block
languagebash
# make sure you're in test: cd ~/Desktop/test
mkdir mysub

Where to go from here...

This is just a glimpse of what you can do with a terminal. There are several more commands and options for each command not covered here. You can simply google whatever you want to do on the terminal and there will be pages explaining that. A few places to go from here (most cover the basics we talked about but might go into more detail):