Code examples of some useful Linux/Unix commands.
Some of the multi-line commands can be condensed into one-liners by simply separating the commands with an ;
. The ones that I have left in multi-line format are ones that I've created into scripts and are in their own files.
If you are downloading a bunch of files or generating new ones this command will show you its progress in bytes.
#!/bin/bash
while true
do
clear
ls -alt | awk '{print $5}' | head -n 2
sleep 1
done
#!/bin/bash
sha1=$(echo -n "$1" | sha1sum | awk '{print toupper($1)}' | tr -d '\n');
curl -s -H $'Referer: https://haveibeenpwned.com/' https://api.pwnedpasswords.com/range/$(echo -n $sha1 | cut -c1-5) | grep -i $(echo -n $sha1 | cut -c6-40);
#!/bin/bash
cat /dev/null > ~/.local/share/recently-used.xbel
The following requires that the latest version of imagemagick be installed on your system. The -background transparent
portion is optional.
#!/bin/bash
convert -background transparent $1 -define icon:auto-resize=16,32,48,64,128 favicon.ico
faad --stdio INPUT.m4b | lame --preset standard - OUTPUT.mp3
I've never needed this, but if I ever do, well I have it :)
#!/bin/bash
START=33 # Range of printable ASCII characters (decimal).
END=126 # Will not work for unprintable characters (> 126).
echo " Decimal Hex Character" # Header.
echo " ------- --- ---------"
for ((i=START; i<=END; i++))
do
echo $i | awk '{printf(" %3d %2x %c\n", $1, $1, $1)}'
# The Bash printf builtin will not work in this context:
# printf "%c" "$i"
done
exit 0
The following is a python script that uses f-strings so requires 3.6 and above.
#!/usr/bin/env python3.6
from secrets import choice
import string
alphabet = string.ascii_letters + string.digits
while True:
password = ''.join(choice(alphabet) for i in range(10))
if (any(c.islower() for c in password)
and any(c.isupper() for c in password)
and sum(c.isdigit() for c in password) >= 3):
break
# On standard Linux systems, use a convenient dictionary file.
# Other platforms may need to provide their own word-list.
with open('/usr/share/dict/words') as f:
words = [word.strip() for word in f]
xkcd = ' '.join(choice(words) for i in range(4))
print(f'RANDOM PASSWORD: {password}')
print(f' XKCD PASSWORD: {xkcd}')
ffmpeg -i INPUT.mp4 -vn -acodec copy OUTPUT.m4a
My Linux Mint machine dims the screen if I unplug the power, which is a normal behavior for power management. The problem arises later when I plug the power back it, it doesn't restore the brightness to what it normally is. Hence, this hack:
#!/bin/bash
me=`whoami`
max=`cat /sys/class/backlight/radeon_bl0/max_brightness`
sudo chown ${me}:${me} /sys/class/backlight/radeon_bl0/brightness
sudo chmod o+x /sys/class/backlight/radeon_bl0/brightness
ls -al /sys/class/backlight/radeon_bl0/brightness
echo ${max} > /sys/class/backlight/radeon_bl0/brightness
sudo chmod 444 /sys/class/backlight/radeon_bl0/brightness
sudo chown root:root /sys/class/backlight/radeon_bl0/brightness
exit
If you start getting all kinds of library errors while trying to start a Steam game, this little command will most likely fix you up.
#!/bin/bash
find ~/.steam/steam/ubuntu12_32/steam-runtime \( -name "libgcc_s.so*" -o -name "libstdc++.so*" -o -name "libxcb.so*" -o -name "libgpg-error.so*" \) -print -delete
I don't know why, but there are times when updates just start failing because of bad keys. Here's a quick way of restoring your system back to normal.
#!/bin/bash
sudo apt-get clean
cd /var/lib/apt
sudo mv lists lists.old
sudo mkdir -p lists/partial
sudo apt-get clean
sudo apt-get update
If your updates stop working, try this before you panic:
#!/bin/bash
sudo rm -r /var/cache/apt/var/lib/apt/lists
sudo apt-get update
Make sure you know what device your USB stick is before attempting this.
It's useful to run a dmesg
right after connecting it, to see what was
detected.
#!/bin/bash
umount /dev/sdc
mkfs.vfat -I /dev/sdc
mount /dev/sdc
I ran into an issue trying to update to a new kernel. It turns out that the old kernels aren't removed by default so my /boot/ partition ran out of space! This little script will list all of the kernels that you are currently installed and display the command that you have to use to remove them.
NOTE: Keep the most recent kernel and at least one more to fall back to in case of any issues. I just keep the two most recent ones.
#!/bin/bash
clear
echo "##################"
echo "# CURRENT KERNEL #"
echo "##################"
echo ""
uname -r
echo ""
echo "######################"
echo "# INSTRALLED KERNELS #"
echo "######################"
echo ""
dpkg --list | grep linux-image | awk '{print $2}'
echo ""
echo "######################"
echo "# INSTRALLED HEADERS #"
echo "######################"
echo ""
dpkg --list | grep linux-headers | awk '{print $2}'
echo ""
echo "Remove unwated kernels with:"
echo " sudo apt autoremove"
echo " sudo apt-get purge linux-image-x..."
I keep forgetting this command, so I put it into a script. Although I think the system tells you how to remove it when it finds that the key has changed...
#!/bin/bash
ssh-keygen -f "/home/`whoami`/.ssh/known_hosts" -R $1
For some tasks, it's easier to deal with files that don't have any spaces. Used the following to remove spaces and then put them back.
despace.sh
#!/bin/bash
j=`echo $1 | sed 's# #_#g' - `
mv "$1" "$j"
respace.sh
#!/bin/bash
j=`echo $1 | sed 's#_# #g' - `
mv "$1" "$j"
#!/bin/bash
sudo apt-get install libgnome-keyring-dev
cd /usr/share/doc/git/contrib/credential/gnome-keyring
sudo make
git config --global credential.helper /usr/share/doc/git/contrib/credential/gnome-keyring/git-credential-gnome-keyring
#!/bin/bash
sudo dmidecode | awk '/^Memory\ Device$/,/Size/{if ($0~/Size/)print}'
#!/bin/bash
echo $(ifconfig | awk '/inet /{print $2}' | grep -v ^127)
Need your machine's serial number but the label is damaged or you're just too lazy to look, don't worry, I've got your back!
#!/bin/bash
sudo /usr/sbin/dmidecode -s system-serial-number
The -segment_time
value is the amount of seconds that the audio segment should be.
ffmpeg -i INPUT.mp3 -f segment -segment_time 3600 -c copy %03d-OUTPUT.mp3