Blogpost

Published: 2020-06-17 - Updated: 2020-06-24

Your favourite linux cheat sheet

Contains common commands you'll probably need when working with a linux terminal.

I update this file when I am looking to solve a new task and find a command that helps me out.

Terminal

Table of contents

General

  • Keep your finger on "alt" in order to jump over words (Depending on your shell this may work using ctrl instead). You can use this in combination with backspace to delete a whole word
  • Search through past commands you have used: ctrl+r than type some search term, hit ctrl+r again in order to jump to next search result
  • Piping - you can pipe the stdout (standard output) of a command into the next by using "|", if you want to pipe the stderr to use "|&"
    • example 'cat /some/file.txt | grep hi'
  • Abort the current command "ctrl+c"

Files and folders

  • print current working directory pwd

Full text search

search file contents grep -r --include=redis_* "Exec" ./system

  • "-r" recursive, search in folders and subfolders, if not specified grep expects the path to point to a file
  • "--include=redis_*" searches only for files whose basename matches the GLOB, in the examples it searches in files starting with "redis_"
  • "Exec" is the search term
  • ./system is the path you want to search

You have to escape "-" with "-" in your search term.

More flags:

  • case insensitive "-i"
  • regular expressions "-P" - parses perl compatible regular expressions in the search term. Example: sudo grep -r -i -P "^port" /etc/redis/, looks for "port" at the beginning of a line
  • "-o" only show the matching part of the line

Find a file

  • find a file with a specific name sudo find / -type f -name 'pg_hba.conf'
    • first argument is the folder path you want to search
    • "-type f" will list only files
    • "-name" is the name of the file you are looking for, you can use GLOB patterns "*.config"
  • find a folder sudo find /where/path/ -name 'nginx-*'

Copy, Create, Delete, Move

  • copy file cp ./fileName ./otherFolder/fileName
  • copy folder recursively cp -r ./fileName ./otherFolder/fileName
  • move/rename file mv ./fileName ./otherFolder/fileName
  • remove file rm ./fileName
  • remove folder recursively rm -r ./fileName
  • create all folders in path sudo mkdir -p /path/to/final/folder
  • list all files within a folder ls -la you can also use it with a path ls -la /path/somewhere
  • copy from one server to another scp -r foo your_username@remotehost.edu:/some/remote/directory/bar

Access rights

  • change owner sudo chown username fileOrFolderPath
    • "-R" use this flag to change it for all files in a folder and its subfolders
  • change access rights sudo chmod -v 751 fileOrFolderPath
    • "-R" use this flag to change it for all files in a folder and its subfolders
    • "-v" stands for verbose, it will tell you what it did

Text editing

Within nano you have to use ctrl to jump over words.

I like nano, here some shortkeys:

  • copy - mark with mouse, then right click on marked text
  • paste - move cursor to position, then right click
  • cut line - ctrl+k, paste that line with ctrl+u
  • exit - ctrl+x - type "y" when you want to save, otherwise "n", choose different file name or just press enter
  • save - ctrl+s
  • search - ctrl+w - type search term, press enter, you can search the same term again if you press ctrl+w again and hit enter
  • undo - alt+m

Compression

  • gzipping files of certain type within current folder find . \( -name '*.css' -o -name '*.html' -o -name '*.js' \) -exec gzip -kNvf {} \;
    • "-N" .gz-files keep name and timestamp of original
    • "-f" overwrites existing .gz-files
    • "-v" verboes
    • "-f" force, add it to overwrite existing zipped files
  • split a file into smaller sized fragments tar cvzf - dir/ | split -b 200m - sda1.backup.tar.gz.
    • extract with cat sda1.backup.tar.gz.* | tar xzvf -
  • decompress tar.gz tar -xzf filename.tar.gz

HTTP

  • download a file wget yourURL

Sources

Tags

#linux #cheatsheet #terminal