Linux command line cheat sheet
created: 18 January 2021
revision: 1
shortcuts
ctrl + rcommands execution history searchctrl + amove cursor at the beginningctrl + emove cursor at the endctrl + udelete the beginning of linectrl + lclears the screenctrl + dexitalt + <-move cursor one word backwardctrl + Ckill current processctrl + Zstop current process, can be resumed in the foregroundfgor backgroundbgctrl + Wcut one word before the cursor and add it to the clipboardctrl + Ucut part of the line before the cursor and add it to the clipboardctrl + Kcut part of the line after the cursor and add it to the clipboardctrl + Ypaste from clipboardctrl + Rrecall last command that provides charactersctrl + Orun the previously recalled programctrl + Gexit command history without running a commandexitlog out of current session
tips
cd -changes the directory to the previous onecdgo to $HOMEsudo !!runs previous command with sudo!commandruns previous command that starts withcommandcmd1 ; cmd2- executes both commands ignoring result ofcmd1cmd1 && cmd2executescmd2ifcmd1is executed without errorscmd1 || cmd2executescmd2ifcmd1is executed with errors$(cmd1)the command is executed in a subshell and the output is placed in the original commandcmd1 | cmd2out of cmd1 to cmd2 - a pipecmd1 |& cmd2err of cmd1 to cmd2 - a pipe
processes
psactive processespstreeactive processes as treepmapmemory usage of processesbglist and resume stopped process in the backgroundfgbring the most recent suspended job to the foregroundfg[job]bring particular suspended job to the foregroundlsoflist of files opened by processes.cat /proc/<PID_ID>/environsee prcocess environment
io redirection
cmd < fileinput from filecmd1 <(cmd2)output of command 2 as input to command 1cmd > fileoutput to filecmd > /dev/nulldiscard outputcmd >> fileappend outputcmd 2> fileerror to filecmd 2>&1error to the same place as outputcmd 1>&2output to the same place as errorcmd &> fileall output to file
manipulations
grep [keyword]leaves only lines contain keyword,grep -E "[regex]"works with regular expressionsgrep [pattern][file_name]grep -r [pattern][folder_name]-lcase insensitive-vinverted search,-oshow matched part of file onlygrep -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 namefind [/folder/location] -name [start_letters]list files starting with name inside folderfind [/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 commandlocate filequick search in the indexawk '{print $1}'prints the first column of the outputawk '{[program]}'executes Perl-like scriptsort -bfsorts output line by line.bignore leading blanksfignore casehhuman sortrreverse
tail [file]shows the last part of a file.fmonitors a file for updates-n [N],-[N]prints lastNlines
head- opposite totaillsshow all-ainclude hidden,-Rrecursive,-rreverse order,-tsort by last modified,-Ssort by size,-llong listing format,-1one file per line,-mcomma separated,-Qquoted output
hardware info
dmesgbootup messagecat /proc/cpuinfocpu informationfree -hfree and used memory,-mshows in MBdmidecodehardware info from BIOS
user info
idshow details of the active userlastshow the last logins onto the systemwhoshow who is logged into the systemwshow who is logged in and their activitygroupadd [group_name]adduser [user_name]usermod -aG [group_name] [user_name]userdel [user_name]usermoduse for changing / modifying user information
file permissions
chmod u=rwx,g=rx,o=r myfileThe lettersu,g, andostand foruser,group, andotherchmod 754 myfileequivalent 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 proglocate any dependency ofprog.export LD_DEBUG=bindings,symbols progsymbol relocation process, locate any dependency ofprog.*. program.shrun a program and let the set environment stays in the terminal*./program.shrun a program without letting the set environment to stay in the terminal. May require executable permissions
git
git add <folder>/*git reset --hard HEAD~1git reset --soft HEAD~1git push --force
git conflicts
git checkout <branch>git pull origin mastergit commitgit 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