Skip to content

Instantly share code, notes, and snippets.

@jarun
Last active April 23, 2023 17:14
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save jarun/0f6f4fc8e69ebe80baa473a590506a7f to your computer and use it in GitHub Desktop.
Save jarun/0f6f4fc8e69ebe80baa473a590506a7f to your computer and use it in GitHub Desktop.
Random collection of useful commands

A collection of awesome commands, functions, aliases to save your time

Play random music from your media storage:
# BASH shell

boom()                                                                           
{                                                                                
    find $@ -type f \( -iname "*.mp3" -o -iname "*.flac" -o -iname "*.webm" -o -iname "*.wma" \) | sort -R | head -n 100 | xargs -d "\n" smplayer > /dev/null 2>&1 &
    disown
}
# FISH shell

function boom --description 'Play random music'                                  
    find $argv -type f \( -iname "*.mp3" -o -iname "*.flac" -o -iname "*.webm" -o -iname "*.wma" \) | sort -R | head -n 100 | xargs -d "\n" smplayer > /dev/null 2>&1 &
    disown
end
Merge video and audio
ffmpeg -i video.mkv -i audio.aac -c copy out.mkv
mkvmerge -o out.mkv video.mkv audio.aac
Merge multimedia files
mkvmerge -o output.mkv input1.mkv +input2.mkv
Extract subtitle (tracks) from media
ffmpeg -i media.mkv -map 0:2 -an -vn -scodec copy out.srt
mkvinfo media.mkv
mkvextract tracks video.mkv track_id:out.srt
Cut media files
ffmpeg -i media.mp4 -ss 00:00:01 -t 01:00:00 -c copy out.mp4
POSIX script to cut media files
#!/usr/bin/env sh

if [ "$2" = "" ]; then
    ffmpeg -i "$nnn" -ss "$1" -c copy "$nnn".mp4
else
    ffmpeg -i "$nnn" -ss "$1" -to "$2" -c copy "$nnn".mp4
fi
Cut and extract 2 streams from a multistream video
ffmpeg -i media.mkv -ss 00:00:55 -t 02:19:53 -vcodec copy -acodec copy -map 0:0 -map 0:2 out.mkv
Batch script to iteratively cut and extract 2 streams from multistream videos and remove initial specified number of chars from output file
#!/bin/bash

for file in *; do
    outfile=`echo "$file" | cut -c "$1"-`
    ffmpeg -i "$file" -map 0:0 -map 0:1 -c copy "$outfile"
done
Cut media file, copy video, extract stereo from 5.1 channel audio at 128 kbps as aac
ffmpeg -i media.mkv -ss 00:01:08 -t 01:36:56 -vcodec copy -strict experimental -c:a aac -ac 2 -b:a 128k out.mkv
Lossless (approx.) convert m4a to mp3
#!/bin/bash

mkdir newfiles

for f in *.m4a; do ffmpeg -i "$f" -vn -codec:a libmp3lame -q:a 2 newfiles/"${f%.m4a}.mp3"; done
Convert webm to mp3
ffmpeg -i in.webm -vn -ab 128k -ar 44100 -y out.mp3
Upgrade specific packages
sudo apt install --only-upgrade packageA packageB
Dropbox synced notes
alias note='vi ~/Dropbox/Public/Docs/Notes/note'
Upload files to transfer.sh
# BASH/ZSH shell

transfer()
{
    fname=`basename $1`
    curl -T $1 https://transfer.sh/$fname
    echo
}
# FISH shell

function transfer
    set fname (basename $argv[1])
    curl -T $argv[1] https://transfer.sh/$fname
    echo
end
Current Google stock value and exchange rate (USD to INR)
googl()
{
    curl -s "http://download.finance.yahoo.com/d/quotes.csv?s=googl&f=l1c1"
    wget -qO- "https://finance.google.com/finance/converter?a=1&from=USD&to=INR" | sed '/res/!d;s/<[^>]*>//g'
}
Generate tags from c, cpp, h files
ta()
{
    rm -rf tags                                                                     
    rm -rf cscope.files                                                             
    rm -rf cscope.out                                                               
    find $PWD | egrep -i '\.(c|h|cpp)$' > cscope.files                              
    ctags -R . *.c *.h *.cpp --tag-relative=yes ./
}
youtube-dl: list available formats
youtube-dl -F "https://www.youtube.com/watch?v=o8NPllzkFhE"
youtube-dl: download best quality
youtube-dl -f best "https://www.youtube.com/watch?v=o8NPllzkFhE"
youtube-dl: download playlist
youtube-dl -cit -f best "https://www.youtube.com/watch?v=o8NPllzkFhE&list=PL9PwPs7-UT5w0-LnDt9dbNdO1UdtmC_QO" --merge-output-format mp4
List RPM/DEB package dependencies
rpm -qpR package.rpm
dpkg -I package.deb
Create a signed Git tag
git tag -s v1.x -m "Release v1.x"
git push origin --tags
Remove a Git tag from local and remote
git tag --delete v1.x
git push --delete origin v1.x
Terminal reader
rd()
{
    elinks "http://justread.mpgarate.com/read?url=$1"
}
function rd
    elinks "http://justread.mpgarate.com/read?url=$argv"
end
#!/bin/bash

curl -s "$1" | html2text -utf8 -style pretty
Remove executable permission for all files recursively
chmod -R -x+X .
Base64 encode/decode
b64()                                                                                                                                                                                               
{                                                                                                                                              
    echo -n $@ | base64                                                                                                                        
}
function b64                                                                                                                                
    echo -n $argv | base64                                                                                                                  
end
bd64()                                                                                                                                         
{                                                                                                                                              
    echo -n $@ | base64 -d                                                                                                                     
}
function bd64                                                                                                                               
    echo -n $argv | base64 -d                                                                                                               
end
List only instances of a single process in top
toP()                                                                                                                                          
{                                                                                                                                              
    top -p $(pgrep -d',' "$1")                                                                                                                 
}
function toP                                                                                                                                
    top -p (pgrep -d',' $argv[1])                                                                                                           
end
Join multiple PDF files

Install poppler-utils on Ubuntu. To join PDFs in specified order, run:

pdfunite 1.pdf 2.pdf ... n.pdf out.pdf
xfce4-terminal is quake or drop-down mode

Bind F1 to xfce4-terminal --hide-borders --drop-down

Torify a terminal

function torgo export http_proxy=http://127.0.0.1:8118/ export HTTP_PROXY=http://127.0.0.1:8118/ export https_proxy=http://127.0.0.1:8118/ export HTTPS_PROXY=http://127.0.0.1:8118/ end

Check IP address from terminal
alias myip='curl ifconfig.me'
To (un)mount and eject drives manually using commands:
lsblk
udisksctl mount -b /dev/sdXn
udisksctl unmount -b /dev/sdXn
udisksctl power-off -b /dev/sdXn
Show a clock in the terminal (bash)
while sleep 1;do tput sc;tput cup 0 $(($(tput cols)-29));date;tput rc;done &
Awesome expression calculator (apcalc) that works with multiple bases
http://www.isthe.com/chongo/tech/comp/calc/
http://manpages.ubuntu.com/manpages/bionic/man1/calc.1.html
Recursively dump du of a directory
ls -1hskR
Secure erase SSD
blkdiscard -s /dev/sdX
man: http://man7.org/linux/man-pages/man8/blkdiscard.8.html
using hdparm: https://askubuntu.com/questions/604426/i-need-to-run-a-security-erase-tool-on-my-drive
Silence mpv
# remove autoload.lua from ~/.config/mpv/scripts
mpv --no-terminal video.mpv
Run nethogs as regualr user
sudo setcap "cap_net_admin,cap_net_raw=ep" "$(which nethogs)"
Split, join PDFs
pdfunite out1.pdf out2.pdf final.pdf
pdfseparate -f 1 input.pdf out%d.pdf
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment