Command Line
The Terminal
Key Points
- The Terminal is an application that provides a text-based command line interface (CLI) to the operating system allowing a user to issue commands and control the computer.
- The Windows Command Prompt is a similar piece of software for Windows based computers but do not use it, instead install a console emulator such as https://cmder.net/
Learn The Command Line by Playing Terminus!
Terminus is a text-based adventure game, aimed at teaching the player to use the command line with basic commands. It was made by two students from MIT. They found that as freshman coming to MIT, many students did not know what a terminal was or how it could be used to interact with the computer.
The main computing environment available to students in Athena clusters is Ubuntu Linux (the same environment we use in the course). While using the terminal is not required, the ability to navigate the command line interface is a powerful tool in computing.
Why is the Command Line So Powerful?
So, why should you care about using the command line, after all, you’re perfectly capable of navigating your computer’s graphical user interface, creating directories, deleting files, installing software, browsing the web, etc…
It’s very important to become familiar with your computer’s command line interface. Besides the ability to rapidly navigate and interact with your computer’s file system, proficiency with the command line also enables you to take huge shortcuts when managing tasks on your computer. A few hours of investment in learning basic CLI commands can add up to hundreds of hours saved clicking your mouse.
Okay, so now that you’re (somewhat) convinced that the CLI is worth your time (get it…time), let’s get into the basics.
Accessing the Command Line
The application or user interface that accepts your typed responses and displays the data on the screen is called a shell, and there are many different varieties that you can choose from, but the most common these days is the Bash shell, which is the default on Linux and Mac systems in the Terminal application.
For windows users, you’ll need to install a command line emulator like https://cmder.net/ before you can use these commands
Basic Command Line Usage
To get started with the command line, you’ll need to open up a terminal window and get ready to start typing commands.
When you run your terminal application (cmdr, Ubuntu on Windows, Terminal on Mac and Linux etc…), your command prompt will start up pointing to a specific folder on your hard drive. You can navigate between folders, act on files inside those folders, or perform other actions.
Finding Your Place
You can type the pwd
command to print your working directory.
Change Directories
You can navigate to either full or relative paths. For example, the command above navigates to a relative path — one above the current folder. If you’re in /path/to/
and you want to navigate to the folder stuff
inside that folder, you can simply type:
cd stuff
You can also navigate to absolute paths. Again, if I were in /path/to/
and I want to navigate to /another/path/
, I would simply type:
cd /another/path
You can move between directories using the cd
command. You can use the following command to switch to the directory directly above the current one:
cd ..
You can also go back to your home directory by typing
cd ~
Creating or Removing Folders
To create a new folder, you can simply use the mkdir <foldername>
command. You can then remove any folder with the rmdir <foldername>
command—as long as the folder is empty. If there are files in the folder, you'll have to delete those files before you can remove the folder.
Creating and Removing Files
The touch <filename>
command can be used to create a new blank file. You can use the rm <filename>
command to delete files:
You can quickly remove all files in a directory by using the ‘*’ (asterisk) wildcard — another simple tool that will come in very handy during your time in the command line. For example, if you’re in a folder and want to delete every file inside that folder, just type:
rm *
If you want to delete a list of files and folders, including all files from subdirectories, without prompting you for every single entry, you can use the -r option for recursive, and the -f option for force. This command will wipe out every instance of a matching filename pattern (note the slightly different use of the wildcard) from the current directory and below:
rm –rf filename.*
List Files
First, let’s display a list of files inside the active folder. For this task, you’ll need to use the ls command. You can pass a number of parameters to the command to display extra details or change the sorting. For instance, if you add-l
to the end a ls
command, you’ll see a detailed listing; -t
will sort the results by file time; -S
will sort by file size; and -r
will reverse the sorting.
You can combine these commends together, like this command, which will show all files sorted by file size with the largest files at the bottom:
ls -lSr
If you use the –a option, you can see hidden files, and you’ll also notice something else in the listing: there are two entries for “.” and “..” at the beginning of the list. These represent the current folder — the “.” folder — and the parent folder — the “..” folder.
How to Read the Terminal
Below are a few video tutorials introducing the terminal application, it’s purpose and how to issue commands on a Mac or PC.
For Mac users, here is a short video on how to launch and use the terminal to perform simple file and folder manipulation.
Conclusion
We’ve just touched (*no pun intended*) upon a small fraction of the commands available to you in the CLI. Here’s a cheat sheet that I highly recommend: https://www.git-tower.com/blog/command-line-cheat-sheet/