{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Introduction to Command Line\n", "\n", "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. \n", "\n", "Important things that you can do from the command line:\n", "\n", "* Run Python scripts and scale them to run on multiple machines\n", "* Install software and Python packages\n", "* Do simple tasks, such as renaming many files or resizing many images, faster and more efficiently\n", "* Gain more power and flexibility over your computing experience\n", "\n", "## Command Line Cheatsheet\n", "\n", "| Mac / Chrome / Linux | Explanation | Windows PowerShell |\n", "|--------------------------------------------|-----------------------------------------------------------------------------------------------------------|--------------------------------------------------------------------------------------|\n", "| `cd` *filepath* | **c**hange **d**irectory, aka move into a different folder | `cd` *filepath* |\n", "| `ls` | **l**i**s**t the files and folders in your current **dir**ectory | `ls` / `dir` / `Get-ChildItem` |\n", "| `pwd` | show **p**ath of **w**orking **d**irectory, aka the folder that you're in right now | `pwd` / `cd` |\n", "| `touch` *filename* | make a new file | `ni` *filename* |\n", "| `mkdir` *directory-name* | **m**a**k**e a new **dir**ectory, aka a folder | `mkdir` *directory-name* |\n", "| `rm` *filename* | **r**e**m**ove, aka delete, a file or directory | `rm` *filename* / `del` *filename* |\n", "| `cp` *original-filename* *copied-filename* | **c**o**p**y a file or directory | `cp` / `copy` |\n", "| `mv` *original-filename* *new-filename* | **m**o**v**e or rename a file or directory | `move` *original-filename* *new-filename* / `ren` *original-filename* *new-filename* |\n", "| `cat` *filename* | show all the contents of a file | `cat` *filename* / `type` *filename* |\n", "| `less` *filename* | show snippet of a file that allows you to scroll through the entire thing | `more` *filename* |\n", "| `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` |\n", "| `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` |\n", "| `wc -w -l` *filename* | show how many **w**ords or lines in a file | `gc` *filename* \\| `Measure-Object -Word –Line` |\n", "| `man` *command* | show the **man**ual, aka the documentation that tells you what a particular command does | `help` *command* |\n", "| `echo` | print text to the command line | `echo` |\n", "| `grep` \"search term\" *filename* or *directory-name* | search for lines that include search term in file | `findstr` \"search term\" *filename* |\n", "\n", "\n", "### Display Path of Working Directory\n", "\n", "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 **p**ath of your **w**orking **d**irectory. \n", "\n", "| Mac / Chrome / Linux | Explanation | Windows PowerShell |\n", "|--------------------------------------------|-----------------------------------------------------------------------------------------------------------|--------------------------------------------------------------------------------------|\n", "| `pwd` | show **p**ath of **w**orking **d**irectory, aka the folder that you're in right now | `pwd` / `cd` |" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'/home/pm5113/Github/GEO203_Fall2022_PSETS/PS_0_Setup/01_Command_Line'" ] }, "execution_count": 1, "metadata": {}, "output_type": "execute_result" } ], "source": [ "%pwd" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The `%` and `!` symbols at the beginning of the lines below allow us to access the command line from a Jupyter notebook.\n", "\n", "Forward slash `/` before and after each name indicates that the name is a directory. On Windows computers, directories are indicated by backslashes `\\`.\n", "\n", "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.\n", "\n", "### List Files and Folders\n", "\n", "If you want to see what's inside the directory that you're currently in, you can run `ls`, which **l**i**s**t all the files and folders.\n", "\n", "| Mac / Chrome / Linux | Explanation | Windows PowerShell |\n", "|--------------------------------------------|-----------------------------------------------------------------------------------------------------------|--------------------------------------------------------------------------------------|\n", "| `ls` | **l**i**s**t the files and folders in your current **dir**ectory | `ls` / `dir` / `Get-ChildItem` |\n" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "command_line_tutorial.ipynb\n" ] } ], "source": [ "%ls" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The output above shows that this directory contains \"command_line_tutorial.ipynb\" (this Jupyter notebook!).\n", "\n", "### Change Directory\n", "\n", "\n", "If we want to find out what's inside that directory, we can move into it by using the **c**hange **d**irectory `cd` command and plugging in the name of the directory\n", "\n", "| Mac / Chrome / Linux | Explanation | Windows PowerShell |\n", "|--------------------------------------------|-----------------------------------------------------------------------------------------------------------|--------------------------------------------------------------------------------------|\n", "| `cd` *filepath* | **c**hange **d**irectory, aka move into a different folder | `cd` *filepath* |" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "/home/pm5113/Github/GEO203_Fall2022_PSETS/PS_0_Setup\n" ] } ], "source": [ "%cd .." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Rename a file\n", "\n", "| Mac / Chrome / Linux | Explanation | Windows PowerShell |\n", "|--------------------------------------------|-----------------------------------------------------------------------------------------------------------|--------------------------------------------------------------------------------------|\n", "| `mv` *original-filename* *new-filename* | **m**o**v**e or rename a file or directory | `move` *original-filename* *new-filename* / `ren` *original-filename* *new-filename* |" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Other Resources\n", "A good starting point for getting logging into a cluster is the \n", "[removing tedium workshop](https://github.com/PrincetonUniversity/removing_tedium).\n", "Specifically, look into:\n", " - [Suppressing Duo](https://github.com/PrincetonUniversity/removing_tedium/tree/master/01_suppressing_duo)\n", " - [Passwordless Login](https://github.com/PrincetonUniversity/removing_tedium/tree/master/02_passwordless_logins)\n", " - [Navigating Command Lines](https://github.com/PrincetonUniversity/removing_tedium/tree/master/04_navigating_command_line)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### To Do\n", "\n", "1. Navigate to your home directory through a command and list all contents\n", "2. Print out the lines 20 - 200 in any file using the commands provided above" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python 3 (ipykernel)", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.9.12" } }, "nbformat": 4, "nbformat_minor": 4 }