Skip to content

Instantly share code, notes, and snippets.

@andrewboring
Last active December 19, 2018 01:04
Show Gist options
  • Select an option

  • Save andrewboring/4c50720aaf231a495961558e61c4c046 to your computer and use it in GitHub Desktop.

Select an option

Save andrewboring/4c50720aaf231a495961558e61c4c046 to your computer and use it in GitHub Desktop.
Some quick ref commands for command line usage

Shell Quick Reference

There are eleventy-dozen thousand command-line/shell references and tutorials online. I whipped this up for those who were not familiar with directory navigation, since Python os.path.join requires accurate path construction.

Format:

  • Comments start with a #. This describes the command to follow.
  • Commands with "$" to indicate a prompt. Do not type the "$" in these commands.
# clear = clear screen
$ clear
# pwd = Print working directory (folder)
$ pwd
/Users/andrew
# cd = change directory
# cd with a directory (folder) name which change to the specified directory
$ cd homework
$ pwd
/Users/andrew/homework

# cd by itself will change to your home directory...
$ cd 
$ pwd
/Users/andrew

# ...no matter where you are
$ pwd
/Users/andrew/long/complicated/path/that/we/will/never/want/to/use/because/it/makes/no/sense
$ cd
$ pwd
/Users/andrew
# Absolute paths begin with / to indicate "root" directory. On windows, the root is C:\
# You can navigate to a directory using its absolute path
$ pwd
/Users/andrew
$ cd /Users/andrew
$ pwd
/Users/andrew
# Relative paths start from current [working] directory
# Use .. to indicate "go up to the immediate parent directory"
# Use a directory to indicate "descend into the child directory
$ pwd
/Users/andrew
$ cd homework
$ pwd
/Users/andrew/homework
$ cd sucks
$ pwd
/Users/andrew/homework/sucks
$ cd ..
$ pwd 
/Users/andrew/homework
$ cd ..
$ pwd
/Users/andrew
# You can construct long paths using this format:
$ pwd
/Users/andrew
$ cd homework/sucks/and/i/hate/it
$ pwd
/Users/andrew/homework/sucks/and/i/hate/it
$ cd ../../../../../is/lovely/and/i/love/it
$ pwd
/Users/andrew/homework/is/lovely/and/i/love/it
# The ~ (tilde) character refers to home directory for your user.
$ pwd
/Users/andrew/homework/is/lovely/and/i/love/it
$ cd ~/homework
$ pwd
/Users/andrew/homework
# ls = list directory contents
$ pwd 
/Users/andrew/homework
$ ls
GTATL201811DATA3	GTDataBootCamp
# Use -l for "long format" listing. 
# Use -a for "all files", which includes "hidden" files.
# This includes a single . (refers to current directory() and double dots (the "up a directory" convention).
# You can combine the "-l" and "-a" into a single "-la".

$ ls -l
total 0
drwxr-xr-x  10 andrew  staff  320 Dec 18 18:39 GTATL201811DATA3
drwxr-xr-x   7 andrew  staff  224 Dec 16 14:34 GTDataBootCamp

$ ls -a
.			..			.DS_Store		GTATL201811DATA3	GTDataBootCamp

$ ls -la
total 16
drwxr-xr-x   5 andrew  staff   160 Dec  6 12:31 .
drwxr-xr-x+ 80 andrew  staff  2560 Dec 18 19:10 ..
-rw-r--r--@  1 andrew  staff  6148 Dec  6 12:31 .DS_Store
drwxr-xr-x  10 andrew  staff   320 Dec 18 18:39 GTATL201811DATA3
drwxr-xr-x   7 andrew  staff   224 Dec 16 14:34 GTDataBootCamp
# mkdir = make [new] directory
# rm = remove [directory OR a file!]

$ pwd
/Users/andrew/homework
$ mkdir newdir
$ ls
GTATL201811DATA3	GTDataBootCamp		newdir
$ rm newdir
rm: newdir: is a directory

# whoops! The rm command will only remove a file, unless we use -r (for "recursive")
$ rm -r newdir
$ ls 
GTATL201811DATA3	GTDataBootCamp	
# rm works on files, rm -r works on directories
$ touch file
$ mkdir folder
$ ls 
file folder
$ rm file
$ rm -f folder
# The -h command often gives you quick ref on what options do what.
# Sometime, you can use --help, too
# Sometimes, neither will.
$ touch -h
usage:
touch [-A [-][[hh]mm]SS] [-acfhm] [-r file] [-t [[CC]YY]MMDDhhmm[.SS]] file ...

# There is also a command called "man", which is short for "manual", available on Mac/Unix/Linux
# For example:
$ man touch
$ man rm
$ man mkdir
# cat will "catenate" (dump the contents of) a file
$ cat file.txt
This is a file.
$ cat file2.txt
This is another file.

# cat will concatenate two files, if you want.
$ cat file.txt file2.txt
This is a file.This is another file.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment