Linux Cheatsheet

Here are a few everyday commands I find useful.

Get help

man ls
info ls

Users

See which groups a user is in

groups username

Add a user to a group

E.g., to add a user to group postgres.

sudo usermod -a -G postgres username

Then log out and log in for change to take effect.

Filesystem

(un)mount devies in fstab

sudo umount /dev/sdc1
sudo mount DEVLABEL

btrfs snapshots

btrfs subvolume list -t /
btrfs subvolume list -ts / # snapshots only

btrfs disk usage

btrfs fi usage /

Change permissions on directory and contents

A 3 digit argument is given to chmod, the digits are the octal representation of binary Read Write Execute permissions for the users in the following order:

user group others

with permissions as the sum of:

4 read
2 write
1 execute
# e.g. change your ssh directory contents to readable only you
chmod 600 ~/.ssh -R
# set a file executable by its owner, readable and executable by group, and readable by others
chmod 754 somefile

Bash

read file lines into array

# -t removes newline from each line
readarray arr -t < ~/dotfiles/git/.gitconfig
echo "${arr[@]:0:3}"
# "${arrayname[@|*(for all)]:start:number}"

loop over array

arr=(a b c)
for a in "${arr[@]}"
do
echo $a
done

for b in $(ls ~/dotfiles)
do
echo $b
done

SSH

copy file over ssh

scp local/file user@host:/destination/folder

File manipulation

zip a file/directory

zip -r  dir.zip dir
unzip dir.zip dir/

replace string in file

# in place preserves original, appending _old to the filename
sed --in-place=_old "s/replace_this/with_this/g" somefile.txt

Devices

usb devices

lsusb

kernel debug messages

dmesg | tail

Systemd

misc

systemctl daemon-reload # reload unit files
systemctl list-units
systemctl import environment # before running a service that requires environment variables

timers

systemctl list-timers --all

unit status

systemctl status postgres.service
systemctl status emacs@slice

journal

# watch the tail of the journal
journalctl -xf
# show entries from today for specific unit
journalctl  --since today -u status-email@.service

Misc

make <CAPSLOCK> <CTRL>

setxkbmap -option 'ctrl:nocaps'

copy output of command to clipboard

ls | xclip -selection clip

Networking