marcandrewb
2/14/2016 - 3:14 AM

Shell Commands

Shell Commands

Basic

  • Ctrl + C to cancel command
  • Ctrl + A to move the cursor to the beginning of the line
  • Ctrl + E to move the cursor to the end of the line
  • Ctrl + U to clear the line to the beginning
  • Opt + Click to move the cursor to the desired location
  • clear or Ctrl + L to scroll up to clear the screen
  • Cmd + K to clear the screen
  • exit or Ctrl + D to exit the current session
  • Ctrl + A and Ctrl + K to delete the current line in the terminal

Writing and reading files

  • echo "Hello, world" > sample.txt is used to set the contents of sample.txt to Hello, world
  • echo "Hello, world" >> sample.txt is used to append Hello, world to the contents of sample.txt
  • cat sample.txt is used to display the contents of sample.txt
  • head sample.txt is used to display the first 10 lines of sample.txt
  • tail sample.txt is used to display the last 10 lines of sample.txt
  • diff sample.txt sample2.txt is used to display the difference in contents between the specified files
  • less sample.txt is used to view sample.txt in a more interactive way
  • /search is used to search for a substring when viewing files using less
  • n or N is used to find the next or previous match respectively when searching using less
  • grep substring filename -i is used to search for substring in filename. -i represents case-insensitivity
  • grep -ri substring directory/ is used to serach for substring in all files recursively found in the directory directory

List files

  • ls *.txt is used to display all files ending in .txt in the current directory
  • ls -l is used to display all files with additional information such as size, permissions, etc
  • ls -r is used to reverse the order in which the files are displayed
  • ls -t is used to display most recently modified files first
  • ls -a is used to display all files (i.e. visible and hidden files)
  • ls -d is used to display all directories
  • pwd prints working directory
  • find . -name "*.txt" is used to display all files ending in .txt in the current directory

Rename, move, remove and copy files and folders

  • mv currentFileName newFileName is used to rename a file
  • cp file.txt file-copy.txt is used to copy a file
  • rm filename is used to remove a file
  • rm -f is used to force remove a file without prompting for confirmation
  • mv *.txt text_files/ is used to move all files ending in .txt to the directory text_files
  • cp filename . is used to copy filename to the current directory
  • cp -r ../text_files . is used to copy the directory text_files to the current directory. -r means recursively.
  • cp - r ../text_files/ . or cp -r ../text_files/* . is used to copy the contents of text_files to the current directory
  • rmdir directoryname is used to remove an empty directory
  • rm -rf directoryname/ is used to remove a directory

Misc

  • tab is used to auto complete file names
  • tab + tab is used to display all the possible auto complete options
  • !! is used to run the previous command
  • !curl is used to run the previous command starting with curl
  • Ctrl + R is used to interactively search for previously ran commands
  • wc filename is used to display word count information
  • | is used to connect two commands (e.g. head sample.txt | wc)
  • cd - is used to navigate back to the previous directory
  • ; is used to seperate multiple commands
  • && is used to seperate multiple commands but only executes a command if the previous command was successful
  • man command is used to display a manual for command

Source