Skip to content

Instantly share code, notes, and snippets.

@chayn1k
Forked from hofmannsven/README.md
Last active September 18, 2015 10:46
Show Gist options
  • Save chayn1k/f2ffea81c219b1660ae2 to your computer and use it in GitHub Desktop.
Save chayn1k/f2ffea81c219b1660ae2 to your computer and use it in GitHub Desktop.
Simple Command Line Cheatsheet

Command Line Crash Course

Related tutorial: http://cli.learncodethehardway.org/book/

CodeCademy: https://www.codecademy.com/courses/learn-the-command-line

Official cheatsheet: http://cd64.de/linux

Note: Hold option (alt) and click a position in the current line to move your cursor to that position.

Clear history: ctrl + l

Clear everything left from current cursor position: ctrl + u

Clear everything right from current cursor position: ctrl + k

Re-call last input with sudo: sudo !!

Stop current process: ctrl + c

Jump to left: ctrl + a

Jump to right: ctrl + e

Help: help cd / help dir (...)

Finding Help: apropos directory / apropos search (...)

Define custom startup screen: sudo nano /etc/motd

System

Get the current path: pwd

Get the current hostname: hostname

Get the current users: users

Get all info about the environment: env

Show calendar: cal

Show today's date: date

Exit terminal: exit

Permissions

Use -R option to change permissions recursively.

List: ps -ef | grep apache | grep -v grep

Change permissions: chmod 755 index.php

Change owner: chown root index.php (root is the username)

Change group: chgrp www-data index.php (www-data is the groupname)

Directories

List directory contents: ls

List all directory contents sorted by time edited: ls -alt

List directory (wildcard matching): ls *.txt

List all files of type: find . -name "*.txt" -print

Switch/toggle between dirs: cd -

Make (empty) directory: mkdir sample-dirname

Remove (empty) directory: rmdir sample-dirname

Remove directory with all contents: rm -rf sample-dirname/

Remove directory contents and keep directory: rm -rf *

Checkout directory: cd sample-dirname

Browsing directories: pushd sample-dirname / popd / dirs (see http://unix.stackexchange.com/a/77081)

Files

Make (empty) file: touch sample-filename.txt

Change creation date: touch –t 201401011337 sample-filename.txt

Change modified date: touch –mt 201401011337 sample-filename.txt

Duplicate file: cp sample-filename.txt sample-filename-copy.txt

Copy/Page folder with content: cp -a folder/ new_folder

Move/Rename file: mv current-filename.txt new-filename.txt

Remove file: rm sample-filename.txt

View file: less sample-filename.txt / more sample-filename.txt

Write to file (will overwrite existing content): cat > sample-filename.txt (quit with ctrl+d)

Search for a filename (not content!) in the current directory: find sample-filename.txt

Search for a string (not filename!) inside all files in the current directory: ack "string"

Search for a string inside all files in the current directory and subdrectories: grep -r "string" *

Search and replace within file: sed -i '' 's/original-text/new-text/g' sample-filename.txt

Server

Access via ssh: ssh [email protected]

Copy file from server to local: scp [email protected]:/path/to/file.png ~/Desktop/ (use -r to recursively get complete folder)

Copy file from local to server: scp ~/Desktop/file.png [email protected]:/path/to/folder (use -r to recursively get complete folder)

Escape files with spaces in name like this: /path/to/file\\\ name.png

Apps

Start appliction: open -a [name-of-programm] e.g. open -a firefox

Open finder with current folder: open .

Variables

Register variable: export TESTING="Sample Text"

Echo variable: echo $TESTING

Unset variable: unset TESTING

Output & Redirects

Write to file: echo "Hello" > hello.txt

Append content from a file to another file: cat file1.txt >> file2.txt

Add the amount of lines, words, and characters to file2.txt: cat file1.txt | wc | cat > file2.txt

Sort the content of a file (like cat): sort hello.txt

Save to sorted content to a new file: cat file1.txt | sort > sorted-file1.txt

Sort and remove duplicates and save to a new file: sort file1.txt | uniq > uniq-file1.txt

Tools

Tree: brew install tree

Nano CLI Basics

Jump to end of file: ctrl + w + v

Vim CLI Basics

Startup vim: vim filename.txt

Insert mode: i

Command mode: ESC

Navigation: h, j, k, l

Move to next word: w (combine with number to skip words)

Move to beginning of word: b (combine with number to skip words)

Move to end of word: e (combine with number to skip words)

Insert three dashes: 3i-

Jump to next dash: f-

Jump to third dash: 3f-

Jump to next bracket: %

Jump to beginning of line: 0

Jump to end of line: $

Jump to next occurence of a word: *

Jump to previous occurence of a word: #

Jump to beginning of the file: gg

Jump to end of the file: G

Search word: / (Use n and N to navigate)

Insert as new line: o and O

Delete chars: x and X

Replace char: r

Cut text e.g. next word: dw (use p to paste in again)

Undo: u

Redo: ctrl + r

Repeat last command: .

Switch to visual editor: v

Save changes: :w

Quit Vim: :q

Quit without saving: :q!

Save and exit: ESC + :x

Force quit (without saving): ESC + :q!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment