Linux / Bash Cheatsheet

Find Kernel version

uname -r

More detailed information

uname -a

Find release name

lsb_release -a

Device / driver info

lsmod
lspci -k
dmesg

Crontab

how to run python scripts: https://www.geeksforgeeks.org/how-to-schedule-python-scripts-as-cron-jobs-with-crontab/

create a new python script

#!/usr/bin/env python3
import time
import os
with open(os.path.expanduser('~/crontest.txt'),'a') as f:
    f.write(time.ctime()+'\n')
chmod +x test.py
crontab -e
# m h  dom mon dow   command
* * * * * $HOME/test.py

Desktop applications

  • ends in .desktop
  • found in /usr/share/applications

Desktop icons

  • svg and png typically
  • found in /usr/share/icons

search path

.bashrc holds path variables.

GUI

ctrl-h hides and shows “dot” files in nautilus like .config or .ssh/

Password

sudo passwd <username>

Networking

ifconfig
ifconfig <network interface> down
ifconfig <network interface> up
ip link
ip addr

output current wifi name

sudo apt install -y wireless tools
iwgetid
iwgetid -r # just the name

list all wifis

iwlist wlan0 scan
sudo apt install -y network-manager
nmcli -f in-use,ssid,bssid,signal,bars  dev wifi

list wifis

nmcli d wifi

connect to a specific wifi

nmcli d wifi connect XX:XX:XX:XX:XX:XX

https://askubuntu.com/questions/833905/how-can-i-connect-to-a-specific-bssid

APT / dpkg

sudo apt list --installed <package-name-start*>
sudo apt --reinstall install <package>
dpkg --list | grep <package-name-fragment>

apt history

less /var/log/apt/history.log

These logs gets rotated (every month I guess), old files will be suffixed with a number and compressed. So to view the next history log, use:

zless /var/log/apt/history.log.1.gz

To view the logs available:

ls -la /var/log/apt/

from here

Devices

sudo libinput --list-devices

Find/Replace

To change the file in place:

sed -i "s/regex/replace/" file

or

sed -i "s|regex|replace|" file

To copy output to a new file

sed "s/regex/replace/" filein > fileout

List all kernel images installed:

dpkg --list | grep linux-image

Login, Logout, Restart

sudo reboot now
sudo shutdown now
gnome-session-quit

Ubuntu Specific

From https://fostips.com/lid-close-action-ubuntu-21-04-laptop/

edit /etc/systemd/logind.conf to configure power options such as lid closing opening,

HandleLidSwitch=ignore
HandleLidSwitchExternalPower=ignore
HandleLidSwitchDocked=ignore
  1. Restart Service
systemctl restart systemd-logind.service

Users, groups

From:

  1. Create a new group

    sudo addgroup groupname
    
  2. Create a new user

    sudo adduser username
    
  3. create a new group by gid

    groupadd -g <group-id> <groupname>
    
  4. create a new user and group by groupid

    MYUSER=ubuntu
    MYGID=1000
    MYUID=1000
    
    groupadd -g ${MYGID} ${MYUSER}
    useradd -u ${MYUID} -g ${MYGID} -p $(perl -e 'print crypt($ARGV[0], "password")' 'password') -G adm,sudo ${MYUSER} && mkdir /home/${MYUSER} && chown ${MYUSER}:${MYUSER} /home/${MYUSER}
    
  5. Find groups associated with current user:

    groups $USER
    
  6. Add new user to new groups

    sudo usermod -aG adm username
    sudo usermod -aG sudo username
    #...
    
  7. modify list of groups user belongs to

    unlike the last command(-aG), -G redefines rather than appends

    sudo usermod -G usergroup,othergroup username
    

Change password

passwd [username]

Remove User

  1. remove user from group

    sudo deluser username groupname
    
  2. remove user completely

    sudo deluser --remove-home username
    

    more tips here

delete group

groupdel <groupname>

Force new password

passwd --expire <username_here>

Expire / unexpire

from here

Expire Account

Let the account expire to disallowing a user from logging in from any source including ssh:

# disallow peter from logging in
sudo usermod --expiredate 1 peter

This is how you can reenable that account:

# set expiration date of peter to Never
sudo usermod --expiredate "" peter

List all users / groups

users:

cut -d: -f1 /etc/passwd
getent passwd

groups:

cut -d: -f1 /etc/group
getent group

find out who is logged on

users
who

Backup folders:

/bin /boot /etc /home /lib /lib64 /opt /root /sbin /usr /var

https://askubuntu.com/questions/222326/which-folders-to-include-in-backup

Update Distro

Based on this link

  1. Might as well be fully updated first

    sudo apt update
    sudo apt upgrade
    sudo shutdown -r now
    
  2. edit which upgrade you want to do (lts or normal)

    sudo nano /etc/update-manager/release-upgrades
    

    change prompt=lts to prompt=normal

  3. run updater

    do-release-upgrade
    

    you may need to indicate what to do with specific config files that get updated.

  4. Restart

    sudo shutdown -r now
    

get ip info

from here

ifconfig
host myip.opendns.com resolver1.opendns.com
dig @resolver4.opendns.com myip.opendns.com +short

get mac

https://www.lifewire.com/find-a-mac-address-using-an-ip-address-818132

arp -a <ip address>

Wifi Scanning

nmcli d wifi

sudo

to run something as root

sudo <command> [command options]

to run interactively as root

sudo -i

to run something as someone else

sudo -i -u <username>

run exit to leave that session

Show all manually-installed packages

apt-mark showmanual

close bash without saving history

unset HISTFILE && exit

or

history -c && history -w && exit

https://www.cyberciti.biz/faq/clear-the-shell-history-in-ubuntu-linux/

Package Management

List repositories

apt policy

Remove a repository

sudo add-apt-repository --remove ppa:PPA_Name/ppa

Misc

Count files in bash

source: https://devconnected.com/how-to-count-files-in-directory-on-linux/

ls | wc -l

How to find .desktop file location for a particular application

For example, if “Image Viewer” is in the name of the icon:

find / -name '*.desktop' -exec grep -H 'Image Viewer' {} \; 2>/dev/null

derived from here:

Some default locations listed here:

/usr/share/applications/gnome-terminal.desktop
~/.local/share/applications/gnome-terminal.desktop
~/.config/gnome-panel/launchers/gnome-terminal.desktop
~/.gnome/apps/gnome-terminal.desktop

Learn about your hardware

Get DMI/BIOS information

sudo dmidecode 
sudo dmidecode -t0 # BIOS
sudo dmidecode -t1 # System
sudo dmidecode -t2 # Board
sudo dmidecode -t3 # Enclosure or Chassis
sudo dmidecode -t4 # Processor
sudo dmidecode -t1 # System

Trash

Files can be stuck in ~/.local/share/Trash/expunged when you delete from Nautilus a folder that belongs to you, but contains files which are belong to another user, and it is tricky for Nautilus to handle this situation correctly. To delete them try to use:

sudo -i
rm -rv /home/<desired_user_name>/.local/share/Trash/expunged/*
exit

octal file permissions

stat /path/to/filename
stat -c '%A %a %n' /path/to/filename

Drives

Hard drive information

list disks with lsblk

lsblk -f #list filesystem details
lsblk -t #show topology
lsblk -i # use ascii
lsblk -o NAME,UUID # show specific columns
sudo lshw -class disk
sudo fdisk -l
sudo hwinfo --disk
ls -l /dev/disk/by-path
ls -l /dev/disk/by-id

get drive information:

sudo hdparm -I /dev/sda

if you have an NVMe device…

sudo apt install -y nvme-cli
nvme list
#sudo nvme smart-log <node_name> 
sudo nvme smart-log /dev/nvme0n1 
sudo nvme id-ctrl /dev/nvme0n1

Mounting

From here

##list disks
sudo fdisk -l
#if you know the disk you want info about:
sudo fdisk -l /dev/sdd 
sudo mount /dev/sdd1 /media/backup
# unmount
sudo umount /media/backup
# unmount all
sudo umount -a
# force unmount
sudo umount -f /media/backup

mounting with fstab

you can get most information from lsblk if you have temporarily mounted it…

#UUID=<yourUUID>                           <mount_location> <filesystem>  <options>  <dump(use 0)>  <order(use 2)>
UUID=24df9215-550f-4ca0-a9f1-8f0efd2  /media/backup    ext4          defaults   0       2

once you have edited, check by running

mount -a

Recursively find storage space of a directory

from here

du -sh /path/to/my/dir

list directories, one level only, from here

du -h  --max-depth 1 /path/to/my/dir

Find the free space of a drive

df -H

Recursively list files

from here

tree /path/to/dir

Recursively chown

from here

#chown -R <owner> <folder_1> <folder_2> ... <folder_n>
chown -R user /home/user
printenv | grep ROS

mount information

information on nautilus-aware mount locations

Read from Serial

from here

sudo apt install -y cu
cu -l /dev/ttyACM0 -s 9600

To exit enter tilde dot (~.)

sudo apt install -y screen
screen /dev/ttyACM0 9600

tail

From here

see the last few lines

#tail -100 <filename>
tail -100 history.txt

see real-time changes to files as they get appended:

tail -f history.txt

see line 196-200 of a file

head -200 history.txt | tail -5

More

see this page for more links

List login times including boots

list login dates / times, users, etc

last

Boot Information

more info here

journalctl --list-boots 

Will list all recent boots

journalctl --boot=<boot id>

To just get information about the most recent boot, use

journalctl -b

Accidentally deleted sudoers

use pkexec to do things you would normally do with sudo, like

pkexec cp /path/to/sudo/backup /etc/sudoers

great advice from here: https://askubuntu.com/questions/438123/accidentally-deleted-etc-sudoers-file

Networking

nm-connection-editor

Disk Cloning

check out dd

sudo fdisk -l /dev/sdb

sudo dd if=/dev/sda of=~/sda.dd bs=128k status=progress conv=noerror,sync
sudo dd if=/dev/sda bs=128k status=progress conv=noerror,sync | gzip -c > /sda.gz
sudo dd if=/dev/sda of=/media/danaukes/24df9215-550f-4ca0-a9f1-8f0d666befd2/sda.dd bs=128k status=progress conv=noerror,sync

Change Swap