Unix Command Cheatsheet

  • cd change directory
  • cd <directory> changes to directory
  • cd .. parent directory
  • cd ../.. two directories up
  • ~ alias to home directory
  • touch updates or changes file modification time. Creates file if it doesn’t exist.
  • touch -c <file> prevents creation of file only updates
  • ls lists contents of the current directory
  • ls -la shows hidden files too
  • ls -1 prints list of files in folder each on its own line
  • pwd show the current (working) directory’s path
  • cp copy
  • cp -r recursive copy
  • scp secure copy
  • mv <file1> <file2> copies file contents from one file to another (essentially changes the name of the file)
  • mv <dir1> <dir2> copies directory contents from one directory to another (essentially changes the name of the directory)
  • mkdir <dirname> creates a directory named <dirname>
  • mkdir -p creates any directory that doesn’t exist (good for creating nested directories as in mkdir foo/bar/baz)
  • cat output file contents (as many files as you like separated by space)
  • stat <file> statistics about file (permissions, time created, time modified)
  • wc word count -l lines -w words -c characters
  • head -n5 <file> first 5 (or n) lines of a file
  • tail -n5 <file> last 5 (or n) lines of a file
  • tail -f <file> updates the end of a file when more input is added. (Good for log files)
  • gzip a compression program
  • gunzip a decompression program
  • find search for files
  • mysql connect to a MySQL server
  • mysqldump “dump” from a MySQL server
  • | “pipe” output to another program (standard output of one programme to standard input of another one)
  • > output to a file
  • >> append to the end of a file
  • < input from a file
  • grep <regex> find using regular expression (-E to avoid having to escape metacharacters)
  • sed s/<regex>/<replace>/g search-replace using regular expression (-r or -E to avoid having to escape metacharacters)
  • perldoc perlreref handy regex reference probably already on system
  • rm remove / delete (be careful on this one)
  • <command> --help many programs have a built-in “help” to show you basic usage information
  • man <command> “manual” program to show information on how to use a command
  • chmod 755 <file> gives full permissions to a file (not always a good idea so make sure you know what you’re doing)
  • host myip.opendns.com resolver1.opendns.com - returns your current IP address
  • nano ~/.bash_profile to edit the .bash_profile (with nano)
  • source ~/.bash_profile to save changes
  • reset resets terminal (hit enter even if you can’t see the command)
  • echo prints to screen (good for testing output of previous commands especially when using wild-cards or brace expansion)
  • cal 2015 calendar of 2015
  • cal -3 previous current and next month’s calendar

This is a conglomeration of several online cheatsheets, the origins of which are noted in the resources section below are completely forgotten. It also includes my own notes. Please feel free to inform me of any wrong information or to any which are vital but may have been overlooked.

For more specific use cases see Unix Cheatsheet 2