Introduction to Command Line
Contents
Introduction to Command Line#
The command line on a computer is how the user can interact directly with the underlying software. It allows greater control on installations of files than a standard Graphical User Interface (GUI) can. The command line — also referred to as the shell, bash, or terminal — is the gateway to computational analysis. Most operating systems (Mac, Linux, Windows) have ways to access the command line.
Important things that you can do from the command line:
Run Python scripts and scale them to run on multiple machines
Install software and Python packages
Do simple tasks, such as renaming many files or resizing many images, faster and more efficiently
Gain more power and flexibility over your computing experience
Command Line Cheatsheet#
Mac / Chrome / Linux |
Explanation |
Windows PowerShell |
---|---|---|
|
change directory, aka move into a different folder |
|
|
list the files and folders in your current directory |
|
|
show path of working directory, aka the folder that you’re in right now |
|
|
make a new file |
|
|
make a new directory, aka a folder |
|
|
remove, aka delete, a file or directory |
|
|
copy a file or directory |
|
|
move or rename a file or directory |
|
|
show all the contents of a file |
|
|
show snippet of a file that allows you to scroll through the entire thing |
|
|
show the first 10 lines of a file (change number of lines by adding |
|
|
show the last 10 lines of a file (change number of lines by adding |
|
|
show how many words or lines in a file |
|
|
show the manual, aka the documentation that tells you what a particular command does |
|
|
print text to the command line |
|
|
search for lines that include search term in file |
|
Display Path of Working Directory#
One of the most basic and useful commands is the one that tells you where you are on your computer. Type pwd
and it will show you the path of your working directory.
Mac / Chrome / Linux |
Explanation |
Windows PowerShell |
---|---|---|
|
show path of working directory, aka the folder that you’re in right now |
|
%pwd
'/home/globalseismology/Github/solid-earth-fundamentals/bin/PS_Setup/01_Command_Line'
The %
and !
symbols at the beginning of the lines below allow us to access the command line from a Jupyter notebook.
Forward slash /
before and after each name indicates that the name is a directory. On Windows computers, directories are indicated by backslashes \
.
This filepath is unique to my personal computer, which you could probably guess because it includes my name. If you open up a command line on your own personal machine and type pwd
, it should return your personal “home” directory.
List Files and Folders#
If you want to see what’s inside the directory that you’re currently in, you can run ls
, which list all the files and folders.
Mac / Chrome / Linux |
Explanation |
Windows PowerShell |
---|---|---|
|
list the files and folders in your current directory |
|
%ls
command_line_tutorial.ipynb
The output above shows that this directory contains “command_line_tutorial.ipynb” (this Jupyter notebook!).
Change Directory#
If we want to find out what’s inside that directory, we can move into it by using the change directory cd
command and plugging in the name of the directory
Mac / Chrome / Linux |
Explanation |
Windows PowerShell |
---|---|---|
|
change directory, aka move into a different folder |
|
%cd ..
/home/globalseismology/Github/solid-earth-fundamentals/bin/PS_Setup
Rename a file#
Mac / Chrome / Linux |
Explanation |
Windows PowerShell |
---|---|---|
|
move or rename a file or directory |
|
Other Resources#
A good starting point for getting logging into a cluster is the removing tedium workshop. Specifically, look into:
To Do#
Navigate to your home directory through a command and list all contents
Print out the lines 20 - 200 in any file using the commands provided above