Skip to main content

Shell | Helpful Hacks

This document will cover everything about using Shell in the Linux/Mac/Win OS all there is to know for me.

Create dummy files

linux
fallocate -l 10G gentoo_root.img #LinxOs
MacOs
mkfile -n 1g temp_1GB_file #MacOS
WinOs
fsutil file createnew temp_10GB_file 10000000000 #windows

Fix corrupted compressed zip file

zip -FFv Corrupted.zip --out Fixed_Courrupred.zip

Linux Cheatsheet

Linux Command Line Cheat Sheet

Check Public IP Address

curl -s https://checkip.amazonaws.com/
dig +short myip.opendns.com @resolver1.opendns.com
curl -s checkip.dyndns.org|sed -e 's/.*Current IP Address: //' -e 's/<.*$//'

Install Anydesk on Linux

wget -qO - https://keys.anydesk.com/repos/DEB-GPG-KEY | apt-key add -

echo "deb http://deb.anydesk.com/ all main" > /etc/apt/sources.list.d/anydesk-stable.list

apt update

apt install -y anydesk

Get/Find Anydesk ID headless

#MacOS
cat ~/.anydesk/system.conf | grep ad.anynet.id | cut -c 14-

#LinuxOS
cat /etc/anydesk/system.conf | grep ad.anynet.id | cut -c 14-

Send Anydesk id through Telegram via shell

TOKEN="<token>" 
ID="-<chatid>"
URL="https://api.telegram.org/bot$TOKEN/sendMessage"
p() {
cat /etc/anydesk/system.conf | grep ad.anynet.id | cut -c 14-
}
q=$(p)

curl -s -X POST $URL -d chat_id=$ID -d text="Your $HOSTNAME Anydesk ID : $q"

Check website is up/alive

Dig Commands Cheatsheet

dig domain.com
ping domain.com

Kill linux running jobs

kill $(jobs -p)

Linux Date Format Options

Date Format OptionMeaningExample Output
date +%clocale’s date timeSat May 9 11:49:47 2020
date +%xlocale’s date05/09/2020
date +%Xlocale’s time11:49:47
date +%Alocale’s full weekday nameSaturday
date +%Blocale’s full month nameMay
date +%m-%d-%YMM-DD-YYYY date format05/09/2020
date +%DMM/DD/YY date format05/09/2020
date +%FYYYY-MM-DD date format09/05/2020
date +%THH:MM:SS time format11:44:15
date +%uDay of Week6
date +%UWeek of Year with Sunday as first day of week18
date +%VISO Week of Year with Monday as first day of week19
date +%jDay of Year130
date +%ZTimezonePDT
date +%mMonth of year (MM)5
date +%dDay of Month (DD)9
date +%YYear (YY)2020
date +%HHour (HH)11
date +%HHour (HH) in 24-hour clock format11
date +%IHour in 12-hour clock format11
date +%plocale’s equivalent of AM or PMAM
date +%Psame as %p but in lower caseam

Check command update in interval | Watch command

watch is used to run any arbitrary command at regular intervals and displays the output of the command on the terminal window.

# watch <custom>
watch ls -lhrt

Check file creation/modification time details

How to find recently modified files in Linux

stat [File_Name]

SCP file transfer Local to Remote / Remote to Local

scp with port number specified

-P port | -r Directory

# Remote to Local
scp -P 2200 root@server-url:/home/some_random_dir/somefile.zip /Users/youruser/yourdesktop/

ShutDown/Reboot Log check

How to Check Shutdown and Reboot Date in Linux - Make Tech Easier

who -b
last -x shutdown
last -x reboot

Kill any running process

ps -ef | grep XXXX | grep -v grep | awk '{print $2}' | xargs kill
sudo kill `ps aux | grep java | grep jboss | awk {'print $2'}`  || echo "Nothing"
ps -ef | grep ./directd | grep -v grep | awk '{print $1,$2,$9 }'
ps -ef | grep directd | grep -v grep  | sed -e 's/^\(.\{120\}\).*/\1/'
ps -ef | grep .sh | grep -v grep | awk '{print $1,"    |    ", $2}'
ps -fC java | grep oneliving | grep -v grep | awk '{print $1,"    |    ", $2,"    |    ", $12}'

Grep Usage

Clear terminal, bash, shell logs

cat /dev/null > ~/.bash_history && history -c && exit

media file conversation m3u8 to mp4

ffmpeg -i "url.m3u8" -c copy file.mp4