I recently spent some time becoming more familiar with the command line. Here are my notes:

General Commands

Ctrl+A : Go to the beginning of the line

Ctrl+E : Go to the end of the line

Ctrl+U : Deletes everything on a line before the cursor

Ctrl+K : Deletes everything on a line after the cursor

Ctrl+R : Recursive search

!! : Execute previous command (Often used: sudo !!)

![command] : Returns the last command which begins with the placeholder

> : Write to file

> > : Append to file

head/tail : Show beginning or end of file

wc : lines words bytes

ps aux : List processes

kill -15 [pid] : Kill the process with id 'pid'

pwd : Print working directory (Current path)

cd - : Changes to previous directory

&& : Runs next command only if the previous one succeeded

; : Colon runs next command regardless of whether the previous one succeeded

grep -ri : grep (search) recursively, case-insensitive

ln : Link files (same file, different location)


Less

Ctrl+F or [spacebar] : move forward a page

Ctrl+B : Move back a page

[Arrow keys] : Move by line

/[search term] : Find in file. Use ’n’ to go to cycle through found instances

G / 1G : Go to end/beginning of a file


Ag

-Q : Gets rid regex default

-l : Only shows filename (Not line and line number)

-i : Case insensitive search

-G : Define filename/path name to search within

--ignore-dir=railties/lib or --ignore-dir=“*.rb” : Exclude directories/file patterns from a search

--skip-vcs-ignores : Returns results for files in .gitignore


cURL

-i : Return headers

-o : Download file

-X : Specify HTTP request type(GET, POST, PUT), passed as an argument

-d : Adds params in quotations to request body. Can be specified as json, even from a file (using @[json_file_name].json)

-F : Mimic form data

-H : Define headers e.g “Accept: application/json”

-u : HTTP authentication (pass “username:password”)

-D / -b : Save returned cookies to file / Pass saved cookies file to server.

-c : Can be used to store just the cookies (unlike -D where headers are also stored)


find

find [PATH_TO_SEARCH] [OPTIONS_TO_USE] [PATTERN_TO_SEARCH_FOR]

-name : File names

-path : Path name

-type : f for file, d for directory

-or : replaces implicit ‘AND’ searching with ‘OR’ searching. Use parenthesis (backslashed) to control

-not or \! : Not matching query

-mtime and -mmin : Modified in the last n days or minutes (make sure numbers are negative)

-size : Size greater than value in bytes. k, M and G can be substituted for kilo, mega and giga. Include + before number.

-print -delete : Prints and deletes matching files. Usually appended to end of query


grep

grep [flags] [term] [filename and extension] - regex by default, accepts multiple filenames

-c : Counts instances

-n : Shows line numbers

-i : case insensitive

-R : Recursive search through directories

—include = “[pattern]” : Only searches files that match this pattern

-v : Invert search


ps

PID : Process ID

TIY : Controlling terminal

TIME : Cumulative CPU time used by process since starting

CMD : Name of running command

u : Extra info columns

-e : All processes

-U : Get processes being run by a user e.g root

-O : provide specific columns to show

-m : Sort by memory

-r : sort by CPU usage


sed

sed “[pattern] / s / [regex search] / [replacement] / [manipulation] / [flags] ” [source file]

sed “ / [regex search] / d ” : delete matching line (does not alter file, only STDOUT)

-i : Edit file in place (rather than sending output to STDOUT) adding .tmp saves second file with files original content -n : Suppress output


sed (Archiving)

tar -c [files/directories] (Can be multiple files/dirs, separated by a space)

-c : create a new archive (Written to STDOUT)

-f : Save to file. File must be passed as first parameter e.g foo.tar

-v : Outputs archived files after creation

-t : Print archived files

-r : append files to an archive (not compatible with compressed archives)

-u : Update file already in tar file (not compatible with compressed archives)

-x : Extract files from archive

-C [folder name] : State where the archive is to be extracted to

-z : Compress archive. Convention: Add “.gz” to tar file name e.g foo.tar.gz

-z : Uncompress archive

-T : Read files/dirs from file

- : Means the -T file is to be the piped in input


Other Commands

cal (defaults to showing one month)


cat


kill [process ID] : Peacefully shutdown when ready (despite command name)


tail [file name] : Prints out last 10 lines


tree : Recursively lists directories and files. Can optionally pass directory (defaults to current directory)


wc