Skip to content

Instantly share code, notes, and snippets.

@iztsv
Forked from hofmannsven/README.md
Last active April 30, 2017 10:18
Show Gist options
  • Save iztsv/a4ec0bdeb45c37429daa50d2c60ac465 to your computer and use it in GitHub Desktop.
Save iztsv/a4ec0bdeb45c37429daa50d2c60ac465 to your computer and use it in GitHub Desktop.
Simple Command Line Cheatsheet

Command Line Crash Course

Related tutorials

Cheatsheets

Shortcuts

CTRL+A  # move to beginning of line
CTRL+B  # moves backward one character
CTRL+C  # halts the current command
CTRL+D  # deletes one character backward or logs out of current session, similar to exit
CTRL+E  # moves to end of line
CTRL+F  # moves forward one character
CTRL+G  # aborts the current editing command and ring the terminal bell
CTRL+J  # same as RETURN
CTRL+K  # deletes (kill) forward to end of line
CTRL+L  # clears screen and redisplay the line
CTRL+M  # same as RETURN
CTRL+N  # next line in command history
CTRL+O  # same as RETURN, then displays next line in history file
CTRL+P  # previous line in command history
CTRL+R  # searches backward
CTRL+S  # searches forward
CTRL+T  # transposes two characters
CTRL+U  # kills backward from point to the beginning of line
CTRL+V  # makes the next character typed verbatim
CTRL+W  # kills the word behind the cursor
CTRL+X  # lists the possible filename completefions of the current word
CTRL+Y  # retrieves (yank) last item killed
CTRL+Z  # stops the current command, resume with fg in the foreground or bg in the background
!!      # repeats the last command
exit    # logs out of current session

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

# Re-call last input with sudo
sudo !!

# Run a script as background process
node server.js &

# List all running process's
ps aux

# Kill a running process
sudo kill 12345

# Exit terminal
exit

Permissions

# Change permissions
chmod 755 index.php

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

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

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

# Go back to previous directory
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 (sample-dirname / popd / dirs)
pushd sample-dirname

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

# Move/Rename file and prompt before overwriting an existing file
mv -i 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) (quit with `ctrl+d`)
cat > sample-filename.txt

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

# Output only 5 lines from file
cat file.txt | head -5

# 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

# MD5 hash for files
md5 sample-filename.txt

# MD5 hash for folders
tar c folder | md5sum

# Encrypt file
openssl enc -aes-256-cbc -e -in sample-filename.txt -out sample-encrypted.txt

# Decrypt file
openssl enc -aes-256-cbc -d -in sample-encrypted.txt -out sample-filename.txt

Server

# Access via ssh
ssh [email protected]

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

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

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

# Mount remote folder
sshfs [email protected]:/remote_dir/ local_dir/

Apps

# Start appliction
open -a [name-of-programm]

# 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

Functions

# Calculate (returns only `int`)
echo $((123/2))

Web

# Check site feedback
ping google.com

# Show site IP
dig +short google.com

# Show A Record
# (Returns: `google.com.	43	IN	A	123.123.123.123` aka `public-name ttl internet record-type server-address`)
dig a google.com

Tools

  • Tree: brew install tree

Security

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