Linux cheatsheet
Less
Less without line wrapping
less -S
Colorful less
less -r
Less: leave uncleared output on exit
export LESS="-XR"
LESS="-XR" hg status
Shell
Write file without text editor: cat + EOF
Save exact content:
cat << 'EOF' > task.xml
Exact content: $100
EOF
cat << EOF > task.xml
Evaluated content: $VARIABLE
EOF
Turn on strict mode in bash
set -euxo pipefail
Screen
screen -S new_screen_name # create named screen
# detach with Ctrl+a, d
screen -ls # list screens
screen -r 5050 # reattach
screen -d -r byname # reattach not-detached session
xargs
Run command on each line:
ls -1 *.sh | xargs -I %s echo "mv '%s' '%s.bak'"
Disk usage of subfolders
du -sch .[!.]* * 2>/dev/null | sort -h
Linux Rescue Kit
Magic Key
Enable Magic Key:
# Temporary
sudo sysctl -w kernel.sysrq=1
# or
sudo echo 1 > /proc/sys/kernel/sysrq
# Permanent
sudo vim /etc/sysctl.conf
# Add:
kernel.sysrq=1
To use the magic SysRq key, press the key combo ALT-SysRq-<command key>:
r- Turns off keyboard raw mode and sets it to XLATEe- Send a SIGTERM to all processes, except for init.i- Send a SIGKILL to all processes, except for init.s- Will attempt to sync all mounted filesystems.u- Will attempt to remount all mounted filesystems read-only.b- Will immediately reboot the system without syncing or unmounting your disks.o- Will shut your system off (if configured and supported).
Note
Some keyboards may not have a key labeled ‘SysRq’. The ‘SysRq’ key is also known as the ‘Print Screen’ key.
Add GPG key to trusted keys (fix NO_PUBKEY)
sudo apt-key adv --keyserver keyserver.ubuntu.com --recv-keys <PUBKEY>
Restart touchpad driver
sudo modprobe -r psmouse
sudo modprobe psmouse
Disable jbd2 gvfsd
(Reduce disk usage in background)
pkill gvfsd-metadata
rm -rf ~/.local/share/gvfs-metadata
Diagnostics
Diagnostic messages of the kernel
sudo dmesg --reltime --ctime
Journal logs
Filter all errors:
sudo journalctl --priority 2..3 -e
sudo journalctl -b 0 -u NetworkManager -e
sudo journalctl -b 0 -e
Crashing GNOME
Errors in journal:
sudo journalctl -b 0 -e /usr/bin/gnome-shell
# Following output:
sudo journalctl -f -o cat /usr/bin/gnome-shell
Reset Gnome configuration:
dconf reset -f /org/gnome/
dconf reset /org/gnome/desktop/interface/cursor-theme
Deb Packages
List installed packages with version
dpkg-query -f '${binary:Package}=${Version}\n' -W