Linux command line cheat sheet
created: 18 January 2021
revision: 1
shortcuts
ctrl + r
commands execution history searchctrl + a
move cursor at the beginningctrl + e
move cursor at the endctrl + u
delete the beginning of linectrl + l
clears the screenctrl + d
exitalt + <-
move cursor one word backwardctrl + C
kill current processctrl + Z
stop current process, can be resumed in the foregroundfg
or backgroundbg
ctrl + W
cut one word before the cursor and add it to the clipboardctrl + U
cut part of the line before the cursor and add it to the clipboardctrl + K
cut part of the line after the cursor and add it to the clipboardctrl + Y
paste from clipboardctrl + R
recall last command that provides charactersctrl + O
run the previously recalled programctrl + G
exit command history without running a commandexit
log out of current session
tips
cd -
changes the directory to the previous onecd
go to $HOMEsudo !!
runs previous command with sudo!command
runs previous command that starts withcommand
cmd1 ; cmd2
- executes both commands ignoring result ofcmd1
cmd1 && cmd2
executescmd2
ifcmd1
is executed without errorscmd1 || cmd2
executescmd2
ifcmd1
is executed with errors$(cmd1)
the command is executed in a subshell and the output is placed in the original commandcmd1 | cmd2
out of cmd1 to cmd2 - a pipecmd1 |& cmd2
err of cmd1 to cmd2 - a pipe
processes
ps
active processespstree
active processes as treepmap
memory usage of processesbg
list and resume stopped process in the backgroundfg
bring the most recent suspended job to the foregroundfg[job]
bring particular suspended job to the foregroundlsof
list of files opened by processes.cat /proc/<PID_ID>/environ
see prcocess environment
io redirection
cmd < file
input from filecmd1 <(cmd2)
output of command 2 as input to command 1cmd > file
output to filecmd > /dev/null
discard outputcmd >> file
append outputcmd 2> file
error to filecmd 2>&1
error to the same place as outputcmd 1>&2
output to the same place as errorcmd &> file
all 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]
-l
case insensitive-v
inverted search,-o
show 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 file
quick search in the indexawk '{print $1}'
prints the first column of the outputawk '{[program]}'
executes Perl-like scriptsort -bf
sorts output line by line.b
ignore leading blanksf
ignore caseh
human sortr
reverse
tail [file]
shows the last part of a file.f
monitors a file for updates-n [N]
,-[N]
prints lastN
lines
head
- opposite totail
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 messagecat /proc/cpuinfo
cpu informationfree -h
free and used memory,-m
shows in MBdmidecode
hardware info from BIOS
user info
id
show details of the active userlast
show the last logins onto the systemwho
show who is logged into the systemw
show who is logged in and their activitygroupadd [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 lettersu
,g
, ando
stand foruser
,group
, andother
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 ofprog
.export LD_DEBUG=bindings,symbols prog
symbol relocation process, locate any dependency ofprog
.*. 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