Linux Cheat-sheet

Linux Cheat-sheet

Linux permissions

r w x Binary Decimal rwx Meaning
1 1 1 111 7 rwx Read, Write, Execute
1 1 0 110 6 rw- Read, Write
1 0 1 101 5 r-x Read, Execute
1 0 0 100 4 r-- Read
0 1 1 011 3 -wx Write, Execute
0 1 0 010 2 -w- Write
0 0 1 001 1 --x Execute
0 0 0 000 0 --- Nothing

List contents of .tgz file

tar tf /home/user/my_file.tgz

Compress file to a .gz file

gzip my_file.ext

Compress file to a .gz file keeping the original file

gzip --keep my_file.ext

Compress folder into a .tgz file

tar cvzf /home/user/my_folder.tgz my_folder

Compress folder into a .tgz file with date

tar cvzf /home/user/my_folder_$(date +%d-%m-%Y_%H-%M-%S).tgz my_folder

Uncompress .gz files

gzip -d my_file.gz

Uncompress .tgz file into a folder

tar xzvf my_file.tgz -C /home/user/my_folder

Uncompress .tar.xz file into a folder

tar -Jxvf my_file.tar.xz -C /home/user/my_folder

SSH Copy file (local > remote) (Upload)

scp my_file.tgz user@host:/home/user/my_file.tgz

SSH Copy file (remote > local) (Download)

scp user@host:/home/user/my_file.tgz my_file.tgz

SSH Copy directory (local > remote) (Upload)

scp -r /home/source user@host:/home/user/destination

SSH Copy directory (remote > local) (Download)

scp -r user@host:/home/user/source /home/destination

Check Linux distro and version

lsb_release -a

or

cat /etc/issue

or

cat /etc/os-release

or

hostnamectl

Follow journal and grep something

journalctl -f | grep something

Follow journal and grep several things

journalctl -f | grep -e orange -e peach -e raspberry

Follow journal and grep regex

journalctl -f | egrep '(orange|peach|raspberry)'

Clear console, restart a service, follow journal, and grep regex

clear && clear && systemctl restart my_service && journalctl -f | egrep '(something)'

Unzip into a specific folder

unzip my_file.zip -d my_folder

Find string in files

grep -r "string to find" /home/dir/*.ext

Find a file

find /home/user/ -name file_name.ext

See file sizes inside a given folder

du /home/* -sh

See size of current folder

du -sch

See size of file system

df -h

Modify user password

passwd userName

Add user

adduser newUserName

Add user to SUDO group

usermod -aG sudo userName

Grep .gz log files

zgrep 'StringIASearchingFor' logFileName.gz

Check OS version

lsb_release -a

Copy ownership from one file to another

chown --reference=SourceFile DestinationFile

Copy permissions from one file to another

chmod --reference=SourceFile DestinationFile

Create a file

touch MyFileName

Write to a file from command line

echo "Hello World!" > MyFileName
echo '{ "myArray": [ { "something": { "somethingElse": [ 1, 2, 3 ] } } ] }' > MyJsonFileName

Empty a file

truncate -s 0 MyFile.txt

Check IP address

ip a

Install and enable SSH server

apt update
apt install openssh-server
systemctl status ssh
ufw allow ssh

Stop and disable SSH server

sudo systemctl stop ssh
sudo systemctl disable ssh

Install NVM (https://github.com/nvm-sh/nvm)

curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.0/install.sh | bash
source ~/.bashrc

Install CURL

sudo apt update && sudo apt upgrade
sudo apt install curl
curl --version

Hash file (https://www.gnu.org/software/coreutils/manual/html_node/Summarizing-files.html)

b2sum MyFileName.Ext
md5sum MyFileName.Ext
sha1sum MyFileName.Ext
sha224sum MyFileName.Ext
sha256sum MyFileName.Ext
sha384sum MyFileName.Ext
sha512sum MyFileName.Ext

Hash string

echo -n "My String To Be Hashed" | b2sum | tr -d ' -'
echo -n "My String To Be Hashed" | md5sum | tr -d ' -'
echo -n "My String To Be Hashed" | sha1sum | tr -d ' -'
echo -n "My String To Be Hashed" | sha224sum | tr -d ' -'
echo -n "My String To Be Hashed" | sha256sum | tr -d ' -'
echo -n "My String To Be Hashed" | sha384sum | tr -d ' -'
echo -n "My String To Be Hashed" | sha512sum | tr -d ' -'

Generate SSH RSA Key pair

ssh-keygen -t rsa -b 4096

Scan available WiFi networks

nmcli dev wifi

List USB devices

lsusb

Uncompress bzip2 files

bzip2 -d fileName.bz2

Uncompress .tar file into a folder

tar xvf my_folder.tar -C /home/user/my_folder

Search and Replace strings in NANO

Press Ctrl + \
Enter search string [press Enter]
Enter replacement string [press Enter]
Press A to replace all instances

Unmask service

systemctl unmask ServiceName
systemctl enable ServiceName
systemctl start ServiceName

Connect to WiFi network (didn't work for me)

sudo ifconfig wlan0 up
sudo iwlist wlan0 scan | grep ESSID
sudo iwconfig wlan0 essid MyWiFiNetwork key s:MyKey
sudo dhclient wlan0
sudo ifconfig wlan0 down

Install Node.JS 17.4.0 in a separate folder

mkdir /usr/local/lib/nodejs
wget https://nodejs.org/download/release/v17.4.0/node-v17.4.0-linux-x64.tar.gz
tar -xzvf node-v17.4.0-linux-x64.tar.gz
rm -f node-v17.4.0-linux-x64.tar.gz
ln -s /usr/local/lib/nodejs/node-v17.4.0-linux-x64/ current-nodejs
ln -nsf /usr/local/lib/nodejs/node-v18.15.0-linux-x64/ current-nodejs
unlink /usr/local/lib/nodejs/current-nodejs

List all known coded character sets

iconv -l

Change character set encoding of a file

iconv -f US-ASCII -t UTF-8 MyUS-ASCIIFile.txt -o MyUTF8File.txt

Rename all .jpg and .JPG files with a specific name and number

ls *.{jpg,JPG} | cat -n | while read n f; do mv "$f" "photo$n.jpg"; done

Select a random .jpg or .JPG file from a folder

ls *.{jpg,JPG} | sort -R | tail -1 | while read file; do 
    # Do something $file
done

Select several random files from a folder

ls | sort -R | tail -$N | while read file; do 
    # Do something $file
done

Encode string to base64

echo -n 'MyStringToBeEncoded' | base64

Display content of .gz files

zcat MyGZfile.gz

List Hardware

lshw

List duplicated files in a directory

rdfind -dryrun true /some/directory

Delete duplicated files in a directory

rdfind -deleteduplicates true /some/directory

Update Linux Kernel

uname –sr
sudo apt-get update
sudo apt-get dist-upgrade

Delete ACL from file / folder

setfacl -b folder_or_file_name

Get ACL info from file / folder

getfacl folder_or_file_name
sed -n "<line_number> p" <file_name>

Properly hash strings in linux

# Source: https://bytefreaks.net/gnulinux/bash/creating-an-md5-hash-of-a-string-in-bash

echo -n "https://nodejslab.com" | sha1sum  # Prints b99c071333d4dbca0d9298e5c8d7480f176cafdc
echo -n "https://nodejslab.com" | md5sum   # Prints 0e4e3b2681e8931c067a23c583c878d5

Linux - Install .deb files

sudo apt install ./my_file.deb

Run AppImages in Linux

./MyAppImageFileName.AppImage

Command History stuff in Linux

# How it works:
user@vm$ echo thisWillBeSaved
user@vm$  echo thisWontBeSaved
#        ^ (extra space to not save the command)

# Show the command history:
user@vm$ history

# Delete a specific line:
user@vm$ history -d 123

# Write the changes into the $HISTFILE (in case it was already written)
user@vm$ history -w

Install flex

sudo apt-get update
sudo apt-get install flex

Install bison

sudo apt-get update
sudo apt-get install bison

CURL to display total time

curl --request GET 'https://www.somewhere.com' --write-out '\nTOTAL TIME: %{time_total} seconds'

CURL to POST body from JSON file

curl --request POST 'https://www.example.com' -d @MyJSONFile.json --header "Content-Type: application/json"

Set bash as default terminal

# For current user
chsh -s $(which bash)

# For another user
sudo chsh -s $(which bash) username

Get SHA-1 of all file inside current folder ignoring node_modules, sort them, and save them to a file

find . -type f -not -path "./node_modules/*" -print0 | xargs -0 sha1sum | sort > sha1sums.txt

High file compression ratio

# Compression
xz -9 -e -T 0 <filename>

# Decompression
xz -d <filename>.xz