Home

Terminal Commands

General

# Clear all the contents (stronger than Ctrl+l; tmux forgets history (I've overridden this with an alias))
clear

# Don’t show current directory
export PS1='> '
# Print current directory
pwd

# Find all files of a certain type, e.g. epub
find . -name "*.epub" -print
# Change permissions of all directories to drwxr-xr-x
find . -type d -exec chmod 755 {} \;
# Change permissions of all files to -rw-r--r--
find . -type f -exec chmod 644 {} \;
# To find and replace in files recursively
find . -type f | xargs perl -pi -e 's/find/replace/g'
# Alternatively,
find . -type f -exec sed -i -e 's/find/replace/g' {} \;
# To rename files by regex, install rename, and run
find . -name "*doc*" -exec rename 's/doc/RuleOfStAugustine/' {} ";"
# To replace all spaces in filenames with underscores in a directory
for f in *\ *; do mv "$f" "${f// /_}"; done
# fd --type d returns the same as find . -type d

# To see current permission and ownership, use 
ls -l
# or to get the octal code of permission,
stat -c '%A %a %n' *
# To change ownership of home directory,
sudo chown -R jeremy:jeremy /home/jeremy

# ctrl+r to search command history

# To find where a script is located (assuming it is in a $PATH directory i.e. can be called without supplying path)
which sb-battery

# Zip file
zip -r foo.zip foo
# Unzip file
unzip Spectral.zip -d /spectral
# Extract a .tar or .tar.gz file
tar -xf filename.tar.gz

# To list all files (including hidden ones)
ls -a
# To list files by time sort by time
ls -t
# To sort by size
ls -S
# To view the size of directories, use 
du -sh *

# Copy and paste file into directory from path
cp file path
# For directories
cp -r directory path
# To cut and paste (move) file/directory into directory from path
mv file path
# If path doesn’t exist, can be used for renaming
# To move multiple files to single directory
mv -t DESTINATION file1 file2 file3

# Delete a file/directory
rm -rf ./filename

# Install from .deb
sudo dpkg -i package_file.deb

# Open Linux Manual page
man ascii
# Open a section
man open.2
# Search for a string
man -K "WNOHANG"

# To view top processes
top
# Within top, k to kill, shift+f to change sorting, W to save configuration
# To kill a prgram, find the pid with
ps aux | grep -i "blender"
# Then kill it with
kill -9 17090
# Where 17090 is the pid

# To silence the output of a program, redirect output to /dev/null
scriptname >/dev/null
# To silence only the errors of a program, redirect output to /dev/null
scriptname 2>/dev/null
# To silence all output of a program, including errors
scriptname &>/dev/null

# Find a disk
sudo fdisk -l
# Mount it
pmount /dev/sda1
# To unmount
pumount /dev/sda1

# To delete all partition of a drive
sudo sfdisk --delete /dev/sda
# To format a drive to fat
sudo mkfs.vfat /dev/sda

# Reload .bashrc
source ~/.bashrc

# Shutdown
shutdown now
# Restart
reboot
# Logout (KDE)
qdbus org.kde.ksmserver /KSMServer logout 0 3 3

# To view output devices
pacmd list-sinks | grep -e name: -e index -e description -e muted
# To toggle mute
pactl set-sink-mute @DEFAULT_SINK@ toggle
# To change volume 
pactl set-sink-volume @DEFAULT_SINK@ +2%
pactl set-sink-volume @DEFAULT_SINK@ -2%

# To disable screen tearing, edit
sudoedit /etc/X11/xorg.conf.d/20-intel.conf
# by adding this line:
Option "TearFree" "true"
# Also, add 
Option      "Backlight"  "intel_backlight"
# to make xbacklight work

# To add an option as a default, find the options
update-alternatives --get-selections
# then
sudo update-alternatives --install /usr/bin/x-terminal-emulator x-terminal-emulator /usr/local/bin/st 100
# then
sudo update-alternatives --config x-terminal-emulator

# To print the nth line of a file, use 
sed 'NUMq;d' file

# To find the version of software, use
dpkg -l | grep -i "qt"

# To repeat a command 10 times, use 
for i in {1..10}; do commandNameHere; done

# To open a terminal, run a command, then wait for a user keypress,
st -e sh -c 'tput civis;curl -s v2.wttr.in;stty raw;dd bs=1 count=1 of=/dev/null'

# To clear command history
cat /dev/null > ~/.bash_history && history -c && exit
# alternatively
shred -u ~/.bash_history && touch ~/.bash_history && history -c

# To check all packages installed,
grep " install " /var/log/dpkg.log
# To get the names of all packages installed after a certain time or date,
grep "install " /var/log/dpkg.log | sed -ne '/2023-06-12 18:46:32/,$ p' | awk '{ print $4 }' | cut -d: -f1
# To remove such packages
grep "install " /var/log/dpkg.log | sed -ne '/2023-06-12 18:46:32/,$ p' | awk '{ print $4 }' | cut -d: -f1 | awk '!seen[$0]++' | xargs /usr/bin/dpkg-query --show --showformat='${db:Status-Status} ${binary:Package}\n' 2>/dev/null | sed -n '/^installed /p' | sed -e 's/installed //g' | xargs sudo apt-get --yes purge

# To increase the title level in markdown (i.e. ## Title -> # Title)
sed -i -e 's/#//' software.md
# To decrease the title level in markdown (i.e. # Title -> ## Title)
sed -i -e 's/#/##/' software.md
# The lack of g makes such replacements happen once per line.

# Turn off location
gsettings set org.gnome.system.location enabled false

# Look at the weather
curl wttr.in

# Compile using the c++11 std library with optimisation level 2 the helloworld.cpp file into a new file called hello.
g++ -std=c++11 -O2 helloworld.cpp -o hello

# For scheduling, use
crontab -e
# Then enter a line like this to make a command run every day at 2000
0 20 * * * export DISPLAY=:0 && /home/jeremy/Documents/Programs/StartupScripts/sct/xsct 1200

# To create a system link 
# (e.g. to a binary in a separate Python environment to /usr/bin/)
ln -s ~/Documents/Programs/Python3.11/bin/yt-dlp /usr/bin/yt-dlp

# Redirecting output of time (Bash)
( time ./foo ) |& bar
# or, to a file 
( time ./foo ) 2> bar

Aliases

# Create an alias
alias drb='./drb/drb -l && ./drb/drb'
# Make a permanent alias
echo alias drb='./drb/drb -l && ./drb/drb' >> ~/.bash_aliases
# Or, put
alias myalias="echo someCommandHere"
# into ~/.bashrc

# To view all current aliases (including default ones), type
alias

# Using _complete_alias, add all aliases to autocomplete
complete -F _complete_alias "${!BASH_ALIASES[@]}"

Git

# Link your local repo with the master repo, identified by the name *upstream*
git remote add upstream https://gitlab.cecs.anu.edu.au/comp2420/2020/comp2420-2020-labs.git

# Check that it has been done correctly.
# You should be able to see both your origin (fork) and upstream (master)
git remote -v

# Fetch the changes
git fetch upstream

# Merge locally
git merge upstream/master
#If GNU nano opens, hit ctrl+S (save), ctrl-X (exit)

# Either pull or merge locally
# Pull
git pull --no-ff --no-edit upstream main

# Push the changes to your fork
git push

#Discard local changes
git stash save --keep-index --include-untracked

#Then, to delete the stash
git stash drop

# Alternatively, discard unwanted changes with
git clean -df
git checkout -- .
# Note that this deletes ignored files

# Name current branch
git status
# Print all branches (local and remote)
git branch -a
# To create a branch
git branch b branch_name
# Checkout a branch
git checkout <branch>

# If you have a .diff file, apply it with
git apply /path/to/file.diff
# To unapply/revert
git apply -R /path/to/file.diff

# Look at picture
git log --oneline --all --graph --decorate --branches

Anaconda

# List all environments
conda env list

# Activate environment
source activate comp2420

# Open directory with jupyter
jupyter lab

GCC

# This assumes the makefile is made a certain way
# Make an executable
make hello_world

# Run an executable
./hello_world
# ./ is the location

# Remove executables
make clean

# Manually you can write
gcc -Wall -Wconversion -g -o program program.c
# where program is the name of the executable created. -g is for debugging.
# -On can define the optimisation level.

Upgrade Distro

# Update the current distribution
sudo apt-get update && sudo apt-get dist-upgrade
# Specific to KDE: Check that non-LTS upgrades are allowed.
sudo software-properties-kde
# Under the Updates tab, set 'Normal Releases' in the Release Upgrade section.
# Install the new one
sudo do-release-upgrade -m desktop

# If completely reinstalling, find user-installed packages with
aptitude search '~i!~M!~E!~prequired!~pimportant'
ls -l /snap/bin
ls -l /var/lib/snapd/snaps
# or
dpkg --get-selections '*'

Make space in root

sudo apt-get clean

# Remove old kernel versions
dpkg -l 'linux-*' | sed '/^ii/!d;/'"$(uname -r | sed "s/\(.*\)-\([^0-9]\+\)/\1/")"'/d;s/^[^ ]* [^ ]* \([^ ]*\).*/\1/;/[0-9]/!d' | xargs sudo apt-get -y purge

# Find biggest files
du -ahx / | sort -rh | head -20

Docker

# Run nginx docker and mount a directory to /usr/share/nginx/html so that you can have a different index.html
sudo docker run -p 80:80 -v /path/to/docker-share/html/:/usr/share/nginx/html --hostname nginx-container nginx

# To stop, hit ctrl+c

#To see which containers are running
sudo docker ps

# Alternatively, you can make a docker-compose.yml file and run
sudo docker compose up
# and to stop
sudo docker compose down

# Use -d to run detached. To stop run
sudo docker stop <CONTAINER_ID>

# To inspect the file system of the container, run
sudo docker exec -it <CONTAINER_ID> /bin/bash

# To see which volumes are mounted, run
sudo docker inspect <CONTAINER_ID> | jq '.[0].Mounts'

Bash

# Put this at the top of a .sh file you want to execute as bash
#!/bin/bash
# else, put to run as default shell (should be POSIX compliant)
#!/bin/sh

# Give the file executable permissions
chmod u+x script.sh

# Here is an example bash function to put in ~/.bashrc
function hello() {
   echo "Hello, $1!"
}

export -f hello

# To add confirmation, use this (works Bash>=3.2)
read -r -p "Are you sure? [y/N] " response
if [[ "$response" =~ ^([yY][eE][sS]|[yY])$ ]]
then
    do_something
else
    do_something_else
fi

# To make the operating directory of a script the location
# of the script, add this line after #!/bin/bash
cd "$(dirname "$0")"

# To create and use a variable
OUTPUT=$(ls -1)
echo "${OUTPUT}"

# To get the file extension (e.g. .md), use
fileext=${filename##*.}
# To get the file name without the path, use
basename $filename
# To get the path and file name without the extension, use 
${filename%.*}
# To get the file name with neither the extension nor path, use 
basename $filename | sed 's/\.[^.]*$//'

# Halt and print Usage:... if no arguments provided
[ $# -eq 0 ] && { echo "Usage: $0 file.yaml"; exit 1; }

# Show standard help
display_help() {
    echo "Usage: $0 [option...]" >&2
    echo
    echo "   -r, --resolution=           make image of resolution WxH (set to monitor size)"
    echo "   -i, --image=                path to image to be set as background"
    exit 1
}

# Get options
while [ "$#" -gt 0 ]; do
  case "$1" in
    -h|--help) display_help; shift ;;

    -r) resolution="$2"; shift 2;;
    -i) image="$2"; shift 2;;

    --resolution=*) resolution="${1#*=}"; shift 1;;
    --image=*) image="${1#*=}"; shift 1;;
    --resolution|--image|--directory|--output) echo "$1 requires an argument" >&2; exit 1;;
    
    -*) echo "unknown option: $1" >&2; exit 1;;
    *) handle_argument "$1"; shift 1;;
  esac
done

# Ensure two arguments (shell)
if [ "$#" -ne 2 ]; then
    echo "Usage: $0 arg1 arg2"
    exit 1
fi

# Assign a variable if unassigned already
if [ -z ${image+x} ]; then image=$(find "${imagesDir}" -type f |sort -R | tail -1); fi
echo "$image"

# To search within a string, use
if [[ $1 == *"needle"* ]]; then
    ...

# To split a string by delimiter ",", use
IFS="," read -r JUNK VID <<< "$1"
# If IFS is not specified, it splits on space " "

# To search output for a regex, use
if xrandr | grep -q 'HDMI.* connected'; then
    ...;
fi

Networking

# Fix DNS when only Google works
# In network settings, add
8.8.8.8
8.8.4.4
# into “Other DNS Servers” for IPV4
# Add these into “Other DNS Servers” for IPV6
2001:4860:4860::8888
2001:4860:4860::8844

# Restart networking in Debian/Ubuntu
sudo systemctl restart networking
sudo systemctl status networking

# To ping a server to see if it is working
ping -c 2 -i 5 example.com
# This is for 2 packets, with 5s intervals

Vim

# To set vim as the default text editor, choose one of the vim options from 
sudo update-alternatives --config editor

# To attempt to save a readonly file
:w !sudo tee % > /dev/null

# Read error messages
:messages

# Read from stdin, but set filetype, e.g. for asm got from objdump
vim -R -c 'set syntax=asm' -

XDG-OPEN

# Find the mime file-type of the file
mimetype file.mp3
# Find the .desktop file in /usr/share/applications
# Edit the default application
xdg-mime default mpv.desktop audio/mpeg
# Or edit the file
vi ~/.config/mimeapps.list
# Add the line
audio/mpeg=mpv.desktop
# Open the file with the default application
xdg-open file.mp3

Clock

# To see the time and date
date
# To time something as with a stopwatch, use
time cat
# then stop it with Ctrl+C
# To create a timer, use sleep
sleep 10s
# Append && mpv ... to play some sound afterwards

NMCLI

# View status
nmcli dev status

# See whether wifi is enabled
nmcli radio wifi
# To turn it on, use
nmcli radio wifi on

# To list available wifi connections, use
nmcli dev wifi list

# To connect to a known network, use
sudo nmcli dev wifi connect network-ssid

# To connect to an unknown network, use
sudo nmcli --ask dev wifi connect network-ssid

# To turn on autoconnection, use
nmcli device set IFNAME autoconnect yes
# where IFNAME is from
nmcli device

# You can use nmtui to connect to uni wifi.

# To add a WPA connection, follow this:
nmcli con add type wifi ifname wlan0 con-name CONNECTION_NAME ssid SSID
nmcli con edit id CONNECTION_NAME
# From there, with nmcli>, enter
set ipv4.method auto
set 802-1x.eap peap
set 802-1x.phase2-auth mschapv2
set 802-1x.identity USERNAME
save
activate

# You may also need to add
nmcli> set 802-1x.password PASSWORD
nmcli> set wifi-sec.key-mgmt wpa-eap

# If this doesn't work, use network-manager-gnome with stalonebar

Bluetoothctl

# To check if the bluetooth servive is running
sudo systemctl status bluetooth

# To unblock (or block) bluetooth from running
rfkill list
# then
rfkill unblock 0

# Then
bluetoothctl
# and within that, scan for devices after it says [bluetooth]#
scan on
# Locate the one you want to connect to
devices
# then connect to it
pair XX:XX:XX:XX:XX:XX
connect XX:XX:XX:XX:XX:XX
# If you want to trust the device
trust XX:XX:XX:XX:XX:XX

# If connecting didn't work, you might need pulseaudio-module-bluetooth
sudo apt install pulseaudio-module-bluetooth 
pulseaudio -k
pulseaudio --start

Pass

# To move between machines find key from
gpg --list-keys
# then use it in 
gpg --output MY_FILENAME_public.gpg --armor --export GPG_PUB_KEY
gpg --output MY_FILENAME_secret.gpg --armor --export-secret-key GPG_PUB_KEY
# From the other machine,
gpg --import MY_FILENAME_public.gpg
gpg --allow-secret-key-import --import MY_FILENAME_secret.gpg
# Then, make sure to trust the new key (KEY-ID the new key id from gpg -K)
gpg --edit-key [KEY-ID]
> trust
> 5
> quit
# Then, do 
pass init [KEY-ID] --path ~/.password-store/
# Also, make sure to remove any other keys on the machine (KEY-ID the old key
# id from gpg -k)
gpg --delete-secret-and-public-keys [KEY-ID]

# One time password
# Requisites: pass, pass-otp and zbar-tools (or zbarcam-qt)
# Download the image of the QR code and open it read it with zbarimg (or
# zbarcam-qt)
zbarimg -q canvas.png
pass otp add name-of-otp
# Then enter the entire string from the QR code: otpauth://...
pass otp name-of-otp
# and use the code generated.

# To change the password, get key
gpg --list-keys
gpg --edit-key your-key-id
# At the gpg> prompt, enter
passwd
# After typing it, type
save

# It is possible for a key to expire, and this will prevent you from adding new
# passwords. You may get this error:
gpg: [stdin]: encryption failed: No public key
Password encryption aborted.
# Then, you can update the expiry date as follows:
gpg --list-keys
# You will see
uid           [ expired]
# Take the key name, e.g. EF654SDSA456DW897Q123SXG4S5ASD45WF456XZC and edit it.
gpg --edit-key [KEY-ID]
# Then from the gpg prompt, to get an extra year:
> expire
> 1y
> save
# If it said
gpg: WARNING: Your encryption subkey expires soon.
gpg: You may want to change its expiration date too.
# then using the same key
gpg --edit-key [KEY-ID]
> key 1
> expire
> 1y
> save
# will give an extra year for the subkey (key 1)

Customisation directories

# Folder for GTK themes. Use lxappearance to to choose GTK theme
/usr/share/themes
# Folder for kvantum (qt) themes. Use qt5-style-kvantum and qt5ct to use themes
~/.config/Kvantum
# Folders for icons. Use lxappearance to choose icon theme
~/.local/share/icons
~/.icons/
# Folder for cursors
/usr/share/icons/
# Then, either use lxappearance to choose mouse theme, or add
Inherits=gruvbox-cursors
# To the index.theme file in ./default copied from the cursor's folder
# Folder for fonts
/usr/local/share/fonts
# Then, once put in folder, make sure the permissions of the fonts are 644 (see above), and run
fc-cache
# Also, make sure you use antialiasing, RGB sub-pixel geometry and full hinting
# in lxappearance

Coloured Manual Pages

# Put these in ~/.bashrc
# Get color support for 'less'
export LESS="--RAW-CONTROL-CHARS"

# Use colors for less, man, etc.
[[ -f ~/.LESS_TERMCAP ]] && . ~/.LESS_TERMCAP

# Then, put this in ~/.LESS_TERMCAP
export LESS_TERMCAP_mb=$(tput bold; tput setaf 2) # green
export LESS_TERMCAP_md=$(tput bold; tput setaf 6) # cyan
export LESS_TERMCAP_me=$(tput sgr0)
export LESS_TERMCAP_so=$(tput bold; tput setaf 3; tput setab 4) # yellow on blue
export LESS_TERMCAP_se=$(tput rmso; tput sgr0)
export LESS_TERMCAP_us=$(tput smul; tput bold; tput setaf 7) # white
export LESS_TERMCAP_ue=$(tput rmul; tput sgr0)
export LESS_TERMCAP_mr=$(tput rev)
export LESS_TERMCAP_mh=$(tput dim)
export LESS_TERMCAP_ZN=$(tput ssubm)
export LESS_TERMCAP_ZV=$(tput rsubm)
export LESS_TERMCAP_ZO=$(tput ssupm)
export LESS_TERMCAP_ZW=$(tput rsupm)
export GROFF_NO_SGR=1         # For Konsole and Gnome-terminal

# Related: 
# Coloured less output from dmesg (see similar options for other programs)
sudo dmesg --color=always | less -R

PDF

# Print this file to a pdf
pandoc --pdf-engine=xelatex --toc --shift-heading-level-by=-1 -V geometry:margin=1in -N -o /home/jeremy/Documents/Cheatsheets/terminal-cheatsheet.pdf /home/jeremy/Documents/Cheatsheets/metadata.yaml terminal.md

# Find all pdfs in directory that contains word stages (case insensitive)
pdfgrep -i "stages" *

# Convert all jpegs into a pdf
img2pdf *.jp* --output combined.pdf
# Join all pdfs in a directory to a single pdf
gs -dBATCH -dNOPAUSE -q -sDEVICE=pdfwrite -dAutoRotatePages=/None -sOutputFile=finished.pdf *

# Crop pdf for ereader, use k2pdfopt
k2pdfopt thing.pdf -dev kpw -de 1.5 -o /home/jeremy/Downloads/%b -ac -0.009 -as -col 1 -h 1024 -w 758
# Then hit enter

# To get a range of pages, use 
qpdf input.pdf --pages . 1-10 -- output.pdf 
# Alternatively, where you want the second to the second last page,
pdftk infile.pdf cat 3-r2 output outfile.pdf

# To put two A4 pages per A4 page
pdfjam --nup 2x1 in.pdf --outfile out.pdf --landscape

# To put 4 a6 pdfs (ideally 1 page each) into 1 a4 pdf
pdfnup --nup 2x2 --no-landscape --paper a4paper --noautoscale true --outfile a4.pdf a6.pdf a6.pdf a6.pdf a6.pdf

# To separate a pdf into its individual pages
pdfseparate default.pdf default-%d.pdf

Mounting Android

# First install mtp-tools and jmtpfs
# Then, create a directory to mount to and gain ownership
mkdir -p Android
sudo chown jeremy:jeremy Android
# To mount
jmtpfs Android
# and unmount until success:
fusermount -u Android

Tags

Categories

If you have questions, you can email me at jeremy.w.cains at gmail.com. View page source here.