shortcuts

  • ctrl + r commands execution history search
  • ctrl + a move cursor at the beginning
  • ctrl + e move cursor at the end
  • ctrl + u delete the beginning of line
  • ctrl + l clears the screen
  • ctrl + d exit
  • alt + <- move cursor one word backward
  • ctrl + C kill current process
  • ctrl + Z stop current process, can be resumed in the foreground fg or background bg
  • ctrl + W cut one word before the cursor and add it to the clipboard
  • ctrl + U cut part of the line before the cursor and add it to the clipboard
  • ctrl + K cut part of the line after the cursor and add it to the clipboard
  • ctrl + Y paste from clipboard
  • ctrl + R recall last command that provides characters
  • ctrl + O run the previously recalled program
  • ctrl + G exit command history without running a command
  • exit log out of current session

tips

  • cd - changes the directory to the previous one
  • cd go to $HOME
  • sudo !! runs previous command with sudo
  • !command runs previous command that starts with command
  • cmd1 ; cmd2 - executes both commands ignoring result of cmd1
  • cmd1 && cmd2 executes cmd2 if cmd1 is executed without errors
  • cmd1 || cmd2 executes cmd2 if cmd1 is executed with errors
  • $(cmd1) the command is executed in a subshell and the output is placed in the original command
  • cmd1 | cmd2 out of cmd1 to cmd2 - a pipe
  • cmd1 |& cmd2 err of cmd1 to cmd2 - a pipe

processes

  • ps active processes
  • pstree active processes as tree
  • pmap memory usage of processes
  • bg list and resume stopped process in the background
  • fg bring the most recent suspended job to the foreground
  • fg[job] bring particular suspended job to the foreground
  • lsof list of files opened by processes.
  • cat /proc/<PID_ID>/environ see prcocess environment

io redirection

  • cmd < file input from file
  • cmd1 <(cmd2) output of command 2 as input to command 1
  • cmd > file output to file
  • cmd > /dev/null discard output
  • cmd >> file append output
  • cmd 2> file error to file
  • cmd 2>&1 error to the same place as output
  • cmd 1>&2 output to the same place as error
  • cmd &> file all output to file

manipulations

  • grep [keyword] leaves only lines contain keyword, grep -E "[regex]" works with regular expressions
    • grep [pattern][file_name]
    • grep -r [pattern][folder_name]
    • -l case insensitive -v inverted search, -o show matched part of file only
    • grep -oP '<string>\K.+(?=</string>)' to get “aaa” from “aaa”. grep -oP ‘stuff before your match\Kyour match(?=stuff after your match)’
    • sed 's/\s*<string>\(.*\)<\/string>/\1/'
  • locate [name] find all files/folder with name
  • find [/folder/location] -name [start_letters] list files starting with name inside folder
  • find [/folder/location] -size [+100M] list files larger than 100M inside folder
    • -user [username] owned by username
    • -mmin [num] modified less than num
  • whereis [command] find binary/source of a command
  • locate file quick search in the index
  • awk '{print $1}' prints the first column of the output
  • awk '{[program]}' executes Perl-like script
  • sort -bf sorts output line by line.
    • b ignore leading blanks
    • f ignore case
    • h human sort
    • r reverse
  • tail [file] shows the last part of a file.
    • f monitors a file for updates
    • -n [N], -[N] prints last N lines
  • head - opposite to tail
  • ls show all
    • -a include hidden, -R recursive, -r reverse order, -t sort by last modified, -S sort by size, -l long listing format, -1 one file per line, -m comma separated, -Q quoted output

hardware info

  • dmesg bootup message
  • cat /proc/cpuinfo cpu information
  • free -h free and used memory, -m shows in MB
  • dmidecode hardware info from BIOS

user info

  • id show details of the active user
  • last show the last logins onto the system
  • who show who is logged into the system
  • w show who is logged in and their activity
  • groupadd [group_name]
  • adduser [user_name]
  • usermod -aG [group_name] [user_name]
  • userdel [user_name]
  • usermod use for changing / modifying user information

file permissions

  • chmod u=rwx,g=rx,o=r myfile The letters u, g, and o stand for user, group, and other
    • chmod 754 myfile equivalent to the above, where each digit is a combination of the numbers 4, 2, 1, and 0.
    • 4 stands for “read”
    • 2 stands for “write”
    • 1 stands for “execute”
    • 0 stands for “no permission”

miscellaneous

  • export LD_DEBUG=libs prog locate any dependency of prog.
  • export LD_DEBUG=bindings,symbols prog symbol relocation process, locate any dependency of prog.
  • *. program.sh run a program and let the set environment stays in the terminal
  • *./program.sh run a program without letting the set environment to stay in the terminal. May require executable permissions

git

  • git add <folder>/*
  • git reset --hard HEAD~1
  • git reset --soft HEAD~1
  • git push --force

git conflicts

  • git checkout <branch>
  • git pull origin master
  • git commit
  • git push origin HEAD

Ubuntu on HyperV

  • Set-VM -VMName “Ubuntu 20.04.1 LTS” -EnhancedSessionTransportType HvSocket
  • https://stackoverflow.com/questions/33027204/how-can-i-get-hyper-v-to-detect-my-ubuntu-vms-ip-address
  • https://linuxize.com/post/how-to-enable-ssh-on-ubuntu-20-04/
  • additional fix here (/etc/xrdp/xrdp.ini from port=3389 to port=vsock://-1:3389 and setting use_vsock=false): https://askubuntu.com/questions/1246362/how-to-setup-xrdp-for-ubuntu-20-04-in-windows-hyper-v

WSL2