Skip to main content

Bash tricks

  • Centraliser toutes les images dans un dossier récursivement sans override, sans doublons, et sans charactères étranges (en option peut aussi supprimer les originaux)
# Repeat the following process for every file format you wish (.jpg .JPG .png .PNG) 

# For each .JPG file found, copy it to temp-photos/ directory verbosely and avoid override by renaming files with a numbered backup suffix
fd \.JPG$ -X cp -v --backup=numbered "{}" temp-photos/

# Go in the directory and remove any duplicate files
cd temp-photos/
fdupes . -dI

# Rename all files to remove the weird suffix name to a proper filename (+ verbose)
rename -vo .JPG.~1~ 1.JPG *
rename -vo .JPG.~2~ 2.JPG *
rename -vo .JPG.~3~ 3.JPG *
rename -vo .JPG.~4~ 4.JPG *

# Last check to avoid weird filenames, ideally that should respond nothing
ls *~

# WARNING only do this if you're really sure → will delete all the original files after the copy (+ verbose)
cd ..
fd --exclude temp-photos/ \.JPG$ -X rm -vf "{}"
  • Convertir toutes les images d'un dossier d'un format à l'autre
# This is an example that convert all .JPG files to .avif files

# WARNING might reduce image quality or make the file unreadable by some systems
cd temp-photos # Will get back on the new directory
fd \.JPG$ -x convert -verbose "{}" "{}.avif" # Will convert all .JPG files to .avif files
rename -vo .JPG.avif .avif *.JPG.avif # Will change file extensions to something less weird
rm -vf *.JPG # Will remove all the precedent files to spare space 
  • Shrink the file size of a video by re-encoding it (it can sometimes go as far as a x10 times smaller)
# You might want to switch the codec to VP-9 for even crazier results
ffmpeg -i input.mkv -vcodec libx265 -crf 28 output.webm

# To make sure the result video is uploadable on social media with a preview
ffmpeg -i output.webm -pix_fmt yuv420p output-discord-friendly.mp4
  • Download a youtube video (or even playlists, channels, or content from pretty much any platform)
# Download the resource(s) in best format
yt-dlp <link>
# List available formats
yt-dlp <link> -F
# Pick a format to download
yt-dlp <link> -f <format>
  • Scan the current directory recursively to know where are the biggest resources that take space
ncdu
  • Download all files attached with a Discord message from the HTML code
cat message.html  | grep -Eo 'https://cdn.discordapp.com/attachments/.+?(?=")' | xargs -I{} wget "{}"