The Command Line for Scientists
The gateway to coding, bioinformatics tools, hacking, and more.
Have you wondered how programmers and engineers do their work day to day? Supposedly, they run code and analyze data and search databases, but where? Is there some “Run” button to click once their code is finished?
Well, not really.
Programmers use the “command line,” the app where all the engineering and hacky computer stuff happens in the real world.
On the command line, you can:
- run scripts
- browse all your files and folders
- create, rename, and delete anything
- start an app, like Chrome or Photoshop
- start a database or server
- use Git and other programming tools
- and more
To use most popular bioinformatics tools and to run scripts in Python, C++, and the like, you will need the command line.
How do I use the command line?
The command line is really just an app on your computer (and an ugly one at that).
On Windows, it is called Command Prompt (or “the Command Prompt”).
On Mac and Linux, it is called Terminal.
Once you open it, every script and file on your computer is at your fingertips.
To do stuff on the command line, all you do is type a command and press Enter. We’ll discuss some common commands below.
When you first open the command line, you’re dropped into your Home folder, then you type short commands to move around to different folders and interact with files, and run or edit your Python/R/MatLab scripts.
This button-less, behind-the-scenes way of using your computer is the basis for automating your day-to-day work.
How Do We Know Which Commands to Type?
There are 100s of commands available to use, but we only use about 6 of them to do 80% of our work at Proto.
Here’s a preview of them. We’ll cover them in the next few sections.
cd
— changes the folder you’re inls
— list all the files in a foldermkdir
— make a new foldertouch
— create a filerm
— delete a filemv
— rename or move a file
We’ll also cover how to run code from the command line.
Before that, check out the next two sections on how to open your computer’s command line app (Terminal or Command Prompt).
How to Open the Command Prompt on Windows
To open Command Prompt, go to Search and type in “Command Prompt.”
Click the app in the search results, and a blank, black window will pop up on your screen.
This is where you’ll type the example commands that we discuss below.
How to Open Terminal on Mac
To open Terminal on a Mac, press the Command (⌘) key and Spacebar to open Spotlight Search. Type in “Terminal.”
Click the first option, Terminal, and a blank, black screen will pop up.
This is where you’ll type the example commands that we discuss below.
Command Line Example #0: Echo
Before we get to the 6 easy commands listed above, let’s do a “Hello World” using the echo
command.
Open the command line, and type the following:
echo "Hello, World"
Press Enter.
Your computer will “echo” whatever you wrote back to you! It’s kind of like the print
statement in Python or R. It just writes stuff to the screen.
You won’t use echo
much day-to-day, but it illustrates the simplicity of typing in commands.
Command Line Example #1: CD
The cd
command is used to switch folders.
CD stands for “change directory.” (A directory is another name for a folder.)
Since we can’t click any apps or buttons on the command line, we use cd
to move around instead.
On the command line, type cd Documents
. This will move you into your Documents folder.
Now, you’re in your Documents folder. We’ll explore what is in your Documents folder with the next command.
To go back to the previous folder, you can type cd ..
, where the ..
means “the parent folder.”
Command Line Example #2: LS
The ls
command lists all the files in your current folder. If you’re in your home folder, it will list all the contents of the home folder.
On Windows, the ls
command is actually dir
, but it does the same thing.
To try it out, type ls
on Mac or dir
on Windows:
You’ll see everything in the home folder (usually named after your username), including other folders and files.
Command Line Example #3: MKDIR
The mkdir
command makes a new folder (AKA a “directory”) in the current folder.
If we’re in the home folder and want to make a new folder called “Code,” we would type:
mkdir Code
Now, we have a folder in the home folder called Code
, which we can also cd
into with cd Code
.
Command Line Example #4: touch
The touch
command is used to create a file.
What we’re really doing is telling the computer to nudge a file to change its “last modified” date. But if that file doesn’t exist, it creates one.
On Windows, the equivalent is type nul
. (If you’re annoyed at the differences between coding on Windows and Mac/Linux, consider installing the Windows Subsystem for Linux.)
To create a file called hello.txt
on Mac OS, type:
touch hello.txt
To create a file called hello.txt
on Windows, type:
type nul > hello.txt
Now, we have an empty file called hello.txt
in the Code
folder. Type ls
to see it.
Command Line Example #5: RM
The rm
command will delete (“remove”) files. Again, on Windows, it is different. We type del
instead.
Let’s test this using the hello.txt
file we just made.
Note that the rm
and del
commands permanently delete things, so be careful.
To delete hello.txt
on Mac, type;
rm hello.txt
On Windows, type:
del hello.txt
And if we type ls
or dir
after that, we’ll see that hello.txt
is gone.
Command Line Example #6: MV
The mv
command moves or renames files.
On Windows, the command is move
.
To rename a file, we write:
mv OLD_filename.txt NEW_filename.txt
OR
move OLD_filename.txt NEW_filename.txt
To test this out, make another hello.txt
file (with either touch hello.txt
or type nul > hello.txt
).
Then, run the following:
mv hello.txt hello2.txt
Or on Windows:
move hello.txt hello2.txt
Now, hello.txt
has been renamed to hello2.txt
:
What about running code and other tools?
We can run code, apps, and other tools with similar, one-word commands.
For example, to run a Python script, we type:
python file.py
To run an R script, we type:
Rscript file.R
There’s also a command to download virtually anything from the Internet called curl
. We have a tutorial on getting the current location of the International Space Station, in which we run the following:
curl http://api.open-notify.org/iss-now.json
Running the above command will get us the latitude and longitude of the ISS right this second from the Open Notify API.
Generally, this is what it looks like to use the command line: moving around from folder to folder and running commands and programs with a few keywords.
For more detail on running Python scripts, check out our How to Create and Run a Standalone Python Script tutorial.
Bonus Tip: Getting Help
Typing command -h
on virtually any command gives you a help menu about that command.
What else can the command line do?
To learn intermediate skills for the command line, check out this free command line course from Dataquest.io. It will teach you how to use the command line to link commands line together, clean your data, and more.
Questions and Feedback
If you have any questions or feedback, email us at protobioengineering@gmail.com or message us on Instagram (@protobioengineering). Thanks for reading.
If you liked this article, consider supporting us by donating a coffee.