Intro to Command Line Interface - RobotGirls/FTC-Team-25 GitHub Wiki

Whether you're using a Mac or PC, we usually use the graphical user interface to click around, open files, move stuff around, etc. There's another way to navigate a computer called the command line, which is a text-based interface. Programmers frequently use command line because it's faster and easier to navigate (sometimes) so here are some basics to get you started:

For a list of common commands and other links, see Unix Quick Reference

How to open a terminal

  • On a Mac computer:
    • Click the Launchpad icon in the Dock. Type "Terminal" in the search field, then click "Terminal"
  • On a PC computer:
    • Download Git Bash
      • Git Bash is a Windows port of the Bash shell, which is the default shell on Linux and macOS. Git Bash can be used to run Linux commands but it is not a Linux shell itself
    • Open a Git Bash Terminal

Navigating with command line

To see the path of the directory (folder) you're currently in:

$ pwd
  • pwd is short for 'print working directory'. You should get a line of output that tells you the path. If you've just opened a new terminal, this path is usually your home directory.

To see the contents of the directory you're in:

$ ls
  • ls is short for 'list' and list the files and directories in your current directory. Variations of this command include ls -a or ls -al. Try them out and note the differences in output.

Let's create a new directory:

$ mkdir ./test_dir
  • The . here refers to the current directory you're in. It's the same command as if you typed mkdir test_dir, but adding a . just makes it more explicit that we want the new directory made in the current directory
  • It's conventional for command line interfaces to use lowercase letters with no spaces when naming directories or files. This makes it easier to search in command line because
    1. you don't have to keep pressing the Shift key
    2. command line will interpret a space as a new entity, and the latter parts of your directory name will get lost. There are ways around this, such as using quotes around a directory name with a space in the middle, but overall it's just easier to make the whole thing lowercase

List the contents of the directory you're in:

$ ls
  • You should see your new directory listed.

Let's navigate into this new directory you just created:

$ cd ./test_dir

'cd' is short for 'change directory'. Print out the path of the directory you're in:

$ pwd
  • You should see the path now looks like your home directory with a /test_dir added at the end.

Create and remove files

List out the contents of the directory:

$ ls
  • It should be empty, since you just created this directory.

Create a new file in this directory by invoking the text editor Vi:

$ vi ./test_file
  • This will open the file in the terminal window

To edit the file:

  • Press i. This switches you to "insert" mode, which allows you to make edits
  • Type hello world
  • Press ESC. This switches you to "command" mode
  • Type :wq. This "writes" (saves) your file and "quits" You should have returned to your terminal window

List out the contents of the directory:

$ ls
  • You should now see your new file test_file listed

To print out the contents of your file into the terminal:

$ more ./test_file
  • Other ways to print out contents of files are cat or less. If you're interested, Google these terms to learn the differences

Move around files

Copy your file:

$ cp ./test_file ./test_file_2
  • If you list out the contents of the directory, you should see both test_file and test_file_2
  • If you print out the contents of test_file_2, you'll see it's the same as that in test_file

Move one file to another directory:

$ mv ./test_file ~/
  • The ~ here refers to the home directory
  • If you list out the contents of the current directory, you'll see that test_file seems to have disappeared

List out the contents of the home directory:

$ ls ~
  • You'll see that test_file has been moved there instead

Remove the copied file:

$ rm ./test_file_2
  • If you list out the contents of the directory, you'll see test_file_2 has been removed

Go back to your home directory

$ cd ..
  • The .. here refers to the previous directory, or "up" one directory. Since you created test_dir in your home directory and changed into the test_dir directory, going up one directory would return you back to your home directory
  • Other ways to navigate to your home directory are cd ~ or simply cd

List the contents of another directory:

$ ls ./test_dir
  • You can use ls on directories other than the current one you're in, you just have to know the path of that directory

Clean up your test files

Remove the test file you created:

$ rm ./test_file
  • If you list the contents of your home directory, you'll see that test_file has been removed

Remove the test directory you created:

$ rmdir ./test_dir
  • If you list the contents of your current directory, you'll see that test_dir has been removed