Skip to content

Instantly share code, notes, and snippets.

@mtntruong
Last active October 31, 2025 04:47
Show Gist options
  • Select an option

  • Save mtntruong/c2b66cafe77e5f744e04b0796454afa2 to your computer and use it in GitHub Desktop.

Select an option

Save mtntruong/c2b66cafe77e5f744e04b0796454afa2 to your computer and use it in GitHub Desktop.
Personal collection of utility bash scripts/commands
#---------------------------------------------------------
# Timimg
#---------------------------------------------------------
#!/bin/bash
start=$(date +%s.%N)
# HERE BE CODE
end=$(date +%s.%N)
runtime=$(python -c "print(${end} - ${start})")
echo "Runtime was $runtime"
#---------------------------------------------------------
#---------------------------------------------------------
# Real folder size (without metadata)
#---------------------------------------------------------
# Sum
find <folder(s)> -type f -print0 | xargs -0 stat --print='%s\n' | awk '{total+=$1} END {printf("%.0f\n",total)}'
# Each folder separately
for i in `ls`; do echo $i; find $i -type f -print0 | xargs -0 stat --print='%s\n' | awk '{total+=$1} END {printf("%.0f\n",total)}'; done
#---------------------------------------------------------
#---------------------------------------------------------
# Diff two folders, filename only
#---------------------------------------------------------
diff <(cd /path/to/dir1; find . | sort) <(cd /path/to/dir2; find . | sort)
#---------------------------------------------------------
#---------------------------------------------------------
# Find and convert PNG to JPG (requires imagemagick)
#---------------------------------------------------------
find . -iname '*.png' | while read i; do mogrify -format jpg "$i" && rm "$i"; echo "Converted $i to ${i%.*}.jpg"; done
#---------------------------------------------------------
#---------------------------------------------------------
# Remove `deinstall` packages
#---------------------------------------------------------
sudo dpkg --purge `dpkg --get-selections | grep deinstall | cut -f1`
#---------------------------------------------------------
#---------------------------------------------------------
# Authorize X11 display when using sudo
#---------------------------------------------------------
sudo xauth add $(xauth -f /home/user/.Xauthority list|tail -1)
#---------------------------------------------------------
#---------------------------------------------------------
# Download video from youtube and convert to WEBM
#---------------------------------------------------------
# Install yt-dlp
pip install "yt-dlp[default]"
# Get list of availables formats
yt-dlp https://www.youtube.com/watch?v=xxx --list-formats
# Download the desired format using format ID
yt-dlp https://www.youtube.com/watch?v=xxx -f <ID>
# Convert to WEBM, decrease -crf value for better quality, resize by changing scale
ffmpeg -i xxx.mp4 -c:v libvpx-vp9 -crf 30 -b:v 0 -c:a libvorbis -vf scale=-1:360 xxx.webm
#---------------------------------------------------------
#---------------------------------------------------------
# Cut video by keyframe
#---------------------------------------------------------
# List timestamps of all keyframes
ffprobe -v error -select_streams v:0 -skip_frame nokey -show_entries frame=pkt_pts_time -of csv=p=0 <video_name>
# Cut video using timestamps of desired keyframes, choose terminating decimal timestamps
ffmpeg -ss <start_time> -i <input_file> -to <end_time> -c copy <output_file>
#---------------------------------------------------------
#---------------------------------------------------------
# Disk benchmark (requires fio)
#---------------------------------------------------------
# Sequential READ speed with big blocks QD32
fio --name TEST --eta-newline=5s --filename=fio-tempfile.dat --rw=read --size=500m --io_size=10g --blocksize=1024k --ioengine=libaio --fsync=10000 --iodepth=32 --direct=1 --numjobs=1 --runtime=60 --group_reporting
# Sequential WRITE speed with big blocks QD32
fio --name TEST --eta-newline=5s --filename=fio-tempfile.dat --rw=write --size=500m --io_size=10g --blocksize=1024k --ioengine=libaio --fsync=10000 --iodepth=32 --direct=1 --numjobs=1 --runtime=60 --group_reporting
# Random 4K read QD1
fio --name TEST --eta-newline=5s --filename=fio-tempfile.dat --rw=randread --size=500m --io_size=10g --blocksize=4k --ioengine=libaio --fsync=1 --iodepth=1 --direct=1 --numjobs=1 --runtime=60 --group_reporting
# Mixed random 4K read and write QD1 with sync
fio --name TEST --eta-newline=5s --filename=fio-tempfile.dat --rw=randrw --size=500m --io_size=10g --blocksize=4k --ioengine=libaio --fsync=1 --iodepth=1 --direct=1 --numjobs=1 --runtime=60 --group_reporting
#---------------------------------------------------------
#---------------------------------------------------------
# NVIDIA GPU power limit
#---------------------------------------------------------
# Set persistence mode for all GPU
sudo nvidia-smi -pm 1
# Set gpu max power in watts
sudo nvidia-smi -pl 200
# Set Power level of specific GPU in watts
sudo nvidia-smi -i 2 -pl 200
#---------------------------------------------------------
#---------------------------------------------------------
# Extract files from VirtualBox's VDI
#---------------------------------------------------------
# Convert VDI to IMG
VBoxManage clonehd --format RAW Win7.vdi Win7.img
# Find offset using GParted
parted Win7.img
# Then "u" -> "p" inside parted
# Mount with offset to "./mnt"
sudo mount -t ntfs-3g -o loop,offset=105906176 ./Win7.img ./mnt
# Unmount
sudo umount mnt
#---------------------------------------------------------
#---------------------------------------------------------
# Mount multi-partition disk image
#---------------------------------------------------------
# Add to /dev/loop
sudo losetup -Pf disk.img
# Mount
sudo mkdir /mnt/loop0p1
sudo mount /dev/loop0p1 /mnt/loop0p1
# Unmount
sudo umount /mnt/loop0p1
# Remove from /dev/loop
sudo losetup -d /dev/loop1
#---------------------------------------------------------
#---------------------------------------------------------
# Clone disk compressing free space
#---------------------------------------------------------
# Fill unused blocks with zeros then delete the temporary file
dd if=/dev/zero of=/dev/sdXn/tmpzero.txt
rm /dev/sdXn/tmpzero.txt
# dd with compression/decompression
sudo sh -c 'dd if=/dev/sdX | gzip -c | dd of=image.raw.gz'
gunzip -c image.raw.gz | sudo dd of=/dev/sdX
#---------------------------------------------------------
#---------------------------------------------------------
# [TeX] Merge PDFs and include XMP metadata (not bash)
#---------------------------------------------------------
\documentclass[a4paper]{article}
\usepackage{pdfpages}
\usepackage{xmpincl}
\includexmp{metadata}
\begin{document}
\includepdf[pages=-]{pdf_1}
\includepdf[pages=-]{pdf_2}
\end{document}
#---------------------------------------------------------
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment