If you'd like a more guided of the command line, please refer to the General Assembly Command Line Prework Tutorial: http://generalassembly.github.io/prework/command-line/#/
This document outlines basic usage of the command line. For Linux and Mac users, these commands should work in Terminal. For Windows users, these should work in Git Bash.
The Command Line Interface (CLI) is a way of interacting with your computer using text-based commands. This is different from the way most people interact with their computers, using their mouse and a Graphical User Interface (GUI).
Once you become comfortable with the basics, it can be a more powerful way to use your computer. You're able to do many things more quickly and programatically.
<command> -<options> <arguments>
<command>
is the action we want the computer to take<options>
(or "flags") modify the behavior of the command<arguments>
are the things we want the command to act onFor Linux and Mac users, you can get view the manual for a command by typing man <command>
. For Windows users, you can view the help page by typing <command> --help
.
A relative file path specifies the path to a file, taking into account your current working directory. For example, if you were to give someone "relative" directions to your house, you would give them directions from their current location (the relative path from where they are to where you are).
An absolute file path specifies the complete path to a file, ignoring your current working directory. For example, if you were to give someone "absolute" directions to your house, you would start by telling them to be on earth, then go to your continent, then go to your country, then go to your region, etc.
pwd
ls
ls -a
lists all files, including hidden filesls -l
lists the files in a long format with extra information (permissions, size, last modified date, etc.)ls *
also lists the contents of subdirectories (one level deep) in your working directoryls <path>
lists files in a specific directory (without changing your working directory)clear
cd
cd <path>
changes directory to the path you specify, which can be a relative path or an absolute pathcd ..
moves you "up" one directory (to the parent directory)cd
moves you to your "home" directorymkdir
mkdir <dirname>
makes a new directory called <dirname>
touch
touch <filename>
creates an empty file called <filename>
rm -i
rm <filename>
removes (deletes) a file permanentlyrm -i <filename>
removes files in interactive mode, in which you are prompted to confirm that you really want to delete the file. It's best to always use rm -i
.rm -ir <dirname>
removes a directory and recursively deletes all of its contentsmv
mv <filename> <new path>
moves a file from its current location to <new path>
mv <filename> <new filename>
renames a file without changing its locationcp
cp <filename> <new path>
copies a file from its current location to <new path>
, leaving the original file unchangedcp <filename> <new filename>
copies a file without changing its locationDesktop
).project
, and then add the following contents:project
directory: draft_paper.md
, plot1.png
, plot2.png
.project
called code
, and then create the following files in it: processing.py
, exploration.py
, modeling.py
.project
called data
, and then create the following files in it: original.csv
, clean.csv
, extra.csv
.project
, and then confirm that you have created the correct structure by printing out (with a single command) all of its files, subdirectories, and the contents of those subdirectories.viz
, and then move plot1.png
and plot2.png
to viz
.plot1.png
as scatterplot.png
, and rename plot2.png
as histogram.png
.extra.csv
from data
.draft_paper.md
called final_paper.md
.project
to confirm you have the correct structure.head
head <filename>
prints the head (the first 10 lines) of the filehead -n20 <filename>
prints the first 20 lines of the filetail
tail <filename>
prints the tail (the last 10 lines) of the filecat
cat <filename>
prints the entire fileless
less <filename>
allows you to page or scroll through the fileq
to exit.wc
wc <filename>
returns the count of lines, words, and characters in a filewc -l <filename>
only counts lines, wc -w <filename>
only counts words, and wc -c <filename>
only counts charactersfind
find <path> -name <name>
will recursively search the specified path (and its subdirectories) and find files and directories with a given <name>
.
for the <path>
to refer to the working directory.<name>
, you can search for an exact match, or use wildcard characters to search for a partial match:*
specifies any number of any characters, such as find . -name *.py
or find . -name *data*.*
?
specifies one character, such as find . -name ??_*.*
grep
grep <pattern> <filename>
searches a file for a regular expression pattern and prints the matching lines-i
option to ignore case.grep -r <pattern> <path>
does a recursive search of the path (checks subdirectories) for matches within files.
for the <path>
to refer to the working directory.grep <pattern>
does a global search (of your entire computer) for matchesCtrl + c
if you want to cancel the search.|
<command 1> | <command 2>
pipes the results from <command 1>
into <command 2>
, and then the results of <command 2>
are printed to the console>
<command> > <filename>
takes the output of <command>
and saves it in <filename>
>>
<command> >> <filename>
takes the output of <command>
and appends it to <filename>
chipotle.tsv
in the data
subdirectory:cut
cut -f1,2 <filename>
cuts a tab-delimited file into columns and returns the first two fieldscut -f1,2 -d, <filename>
indicates that the file is delimited by commassort
sort <filename>
sorts a file by the first fielduniq
uniq <filename>
discards all but one of the successive identical lines (thus it only keeps unique lines)uniq -c <filename>
also records the count of the number of occurrencessort
before uniq
.