The Command Line Interface
In this lesson, we'll discuss the basics of perhaps the most important tool in your belt as a programmer: the command line
What is the terminal? #
The command-line interface is a tool into which you can type text commands to make the computer perform specific tasks in contrast to using a graphical user interface through pointing and clicking on menus and buttons with a mouse.
Since the command line interface allows you to control the computer directly by typing commands, many tasks can be performed more efficiently. For example, if you want to make one change in the names of around 2000 files, you can use a for loop on the command line and change the names of all of them in a few seconds. Therefore basic knowledge of the command line is an absolute essential.
The user interface that accepts typed commands and displays data on the screen is called a shell. There is a wide variety of shells that you can choose from, but the most common is the Bash shell, which is the default on Linux and Mac systems in the Terminal application. Windows systems only include the anemic Command Prompt application by default, which is nowhere near as powerful as Bash. If you’re going to follow along on your windows machine, however, consider installing cygwin.
Open up a shell; you will see a blank (usually black) screen with a cursor to the right of something like:
accountname@machinname:~$
Let’s finally look at a few commands now!
Listing files and directories #
Type ls
into a shell and hit enter (or try it below!) and watch the magic happen.
ls
Voila! All the files and directories in your current directory get printed.
Checking what directory you are in #
Also, to check what directory you are currently in, type pwd
, and hit enter.
Here is an example:
pwd
Changing directories #
You can change to a directory that exists within your current directory with the command,
cd nameOfDirectory
you can move back to a parent directory with the command,
cd ..
lscd sampleDirectorylscd ..ls
cat SampleFile.txt
touch nameOfFile.txtls
mkdir newFolderls
Moving files #
You can move files from one directory to another like so,
mv path/to/file/filename new/path
mv path/to/file/new/path/filename .
mv filename ..
Note that .
indicates the current directory, so the second command above moves the file from path/to/file/new/path/filename
to the current directory. As mentioned previously, ..
indicates the parent directory, so the last command moves the file filename
to the parent directory.
mkdir newfoldermv fileName.txt newfolderlsmv newfolder/fileName.txt .ls
touch fileName # Creating a filelsrm fileName # Removing itls
Quick quiz on the command line!
What does the ls
command do?
It lists all the directories
It lists all the files, directories, and files within child directories
It lists all the files and directories within a folder
It lists all the files, hidden files and directories within a folder
Now that we know some command line basics, let’s move on to learning version control with Git in the next lesson!