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

cd filepath

change directory, aka move into a different folder

cd filepath

ls

list the files and folders in your current directory

ls / dir / Get-ChildItem

pwd

show path of working directory, aka the folder that you’re in right now

pwd / cd

touch filename

make a new file

ni filename

mkdir directory-name

make a new directory, aka a folder

mkdir directory-name

rm filename

remove, aka delete, a file or directory

rm filename / del filename

cp original-filename copied-filename

copy a file or directory

cp / copy

mv original-filename new-filename

move or rename a file or directory

move original-filename new-filename / ren original-filename new-filename

cat filename

show all the contents of a file

cat filename / type filename

less filename

show snippet of a file that allows you to scroll through the entire thing

more filename

head filename

show the first 10 lines of a file (change number of lines by adding -*a number* flag, e.g. head -100)

gc filename -head 10

tail filename

show the last 10 lines of a file (change number of lines by adding -*a number* flag, e.g. tail -100)

gc filename -tail 10

wc -w -l filename

show how many words or lines in a file

gc filename | Measure-Object -Word –Line

man command

show the manual, aka the documentation that tells you what a particular command does

help command

echo

print text to the command line

echo

grep “search term” filename or directory-name

search for lines that include search term in file

findstr “search term” filename

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

pwd

show path of working directory, aka the folder that you’re in right now

pwd / cd

%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

ls

list the files and folders in your current directory

ls / dir / Get-ChildItem

%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

cd filepath

change directory, aka move into a different folder

cd filepath

%cd ..
/home/globalseismology/Github/solid-earth-fundamentals/bin/PS_Setup

Rename a file#

Mac / Chrome / Linux

Explanation

Windows PowerShell

mv original-filename new-filename

move or rename a file or directory

move original-filename new-filename / ren original-filename new-filename

Other Resources#

A good starting point for getting logging into a cluster is the removing tedium workshop. Specifically, look into:

To Do#

  1. Navigate to your home directory through a command and list all contents

  2. Print out the lines 20 - 200 in any file using the commands provided above