Skip to content

Instantly share code, notes, and snippets.

@gregdeane
Created May 8, 2017 05:35
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save gregdeane/f4b7a073f9845a90d6bfb48543f0a3a3 to your computer and use it in GitHub Desktop.
Save gregdeane/f4b7a073f9845a90d6bfb48543f0a3a3 to your computer and use it in GitHub Desktop.
Helpful Terminal Commands

Compression

  • tar cf file.tar files - create a tar named file.tar containing files
  • tar xf file.tar - extract the files from file.tar
  • tar czf file.tar.gz files - create a tar with Gzip compression
  • tar xzf file.tar.gz - extract a tar using Gzip
  • gzip file - compresses file and renames it to file.gz
  • gzip -d file.gz - decompresses file.gz back to file

File Content

  • cat file - output the contents of file
  • less file - view file with page navigation
  • head file - output the first 10 lines of file
  • tail file - output the last 10 lines of file
  • tail -f file - output the contents of file as it grows, starting with the last 10 lines

File System

  • ls - list items in current directory
  • ls -l - list items in current directory and show in long format to see perimissions, size, and modification date
  • ls -a - list all items in current directory, including hidden files
  • ls -F - list all items in current directory and show directories with a slash and executables with a star
  • ls dir - list all items in directory dir
  • cd dir - change directory to dir
  • cd .. - go up one directory
  • cd / - go to the root directory
  • cd ~ - go to to your home directory
  • cd - - go to the last directory you were just in
  • pwd - show present working directory
  • mkdir dir - make directory dir
  • rm file - remove file
  • rm -r dir - remove directory dir recursively
  • cp file1 file2 - copy file1 to file2
  • cp -r dir1 dir2 - copy directory dir1 to dir2 recursively
  • mv file1 file2 - move (rename) file1 to file2
  • ln -s file link - create symbolic link to file
  • touch file - create or update file
  • find file - find all instances of file in real system
  • locate file - find all instances of file using indexed database built from the updatedb command. Much faster than find

Screen / Terminal Multiplexer

  • screen -ls - show your current screen sessions
  • CTRL-A - activate commands for screen
  • CTRAL-A d - detach from screen session
  • screen -x - attach to screen session
  • screen -r name - attach to screen session by name
  • exit - (inside screen) exit screen session

Search/Replace

  • sed -i 's/day/night/g' file - find all occurrences of day in a file and replace them with night - s means substitude and g means global - sed also supports regular expressions

Searching

  • grep pattern files - search for pattern in files
  • grep -r pattern dir - search recursively for pattern in dir
  • grep -rn pattern dir - search recursively for pattern in dir and show the line number found
  • grep -r pattern dir --include='*.ext - search recursively for pattern in dir and only search in files with .ext extension
  • command | grep pattern - search for pattern in the output of command
  • command | grep -A 10 -B 10 pattern - search for pattern in the output of command show 10 lines [A]fter and 10 lines [B]efore
  • command | grep -C 10 pattern - search for pattern in the output of command show 10 lines [A]fter and 10 lines [B]efore

Shortcuts - Bash

  • ctrl+a - move cursor to beginning of line
  • ctrl+f - move cursor to end of line
  • alt+f - move cursor forward 1 word
  • alt+b - move cursor backward 1 word
  • ctrl+r - search .bash_history for previously used command
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment