18 résultats taggé bash

Some useful Bash commands


# Batch download
youtube-dl -i -F -a ~/Desktop/list.txt # Get the format
youtube-dl -i -f <format> -a ~/Desktop/list.txt # Use the format

# Automagically download the desired resolution
youtube-dl -i -f "bestvideo[height<=720]+bestaudio/best[height<=720]" -a ~/Desktop/list.txt

## Avoid throttling
youtube-dl -i --external-downloader aria2c --external-downloader aria2c --external-downloader-args "-j 8 -s 8 -x 8 -k 5M" -f mp4-240p -a ~/Desktop/list.txt
youtube-dl -f 18 -4 -i https://www.youtube.com/watch?v=uhikhkih # Use IPv4

## Rate limit
youtube-dl -i -r 120k -a ~/Desktop/list.txt


# Batch download
wget -r -H -nc -np -nH --cut-dirs=1 -e robots=off -l1 -i ~/Desktop/list.txt

# Rate limit
wget --no-check-certificate --limit-rate 60k https://trucmuche.com/truc.mp3
wget --no-check-certificate --limit-rate 20k -i ~/Desktop/list.txt # Batch download

# Recursively fetch a directory
wget -r --no-parent --reject "index.html*" -e robots=off  http://example.com/configs/.vim/

## To avoid downloading the directory structure as well
wget -r -nH -nd -np -R index.html* -e robots=off http://example.com/configs/.vim/

# Avoid the robots.txt issue
 -e robots=off  (See http://www.krazyworks.com/wget-and-user-agent-header/)

# Download one type only
 -A jpeg,jpg,bmp,gif

# Use a user-agent
 -U -U "Mozilla/5.0 (X11; U; Linux i686; pl-PL; rv: Gecko/20121223 Ubuntu/9.25 (jaunty) Firefox/3.8"

# So, get only fonts in an entire site
wget -r --no-parent --reject "index.html*" -e robots=off --limit-rate 60k -A otf,ttf,ttc,dfont 

Batch stuff

# Convert multiple video to audio
for i in *.mp4; do ffmpeg -i "$i" -vn "${i%mp4}mp3"; done
# Convert avi to mp4
for i in *.avi; do ffmpeg -i "$i" -c:a aac -b:a 128k -c:v libx264 -crf 23 -preset fast -profile:v baseline -level 3.0 -threads 2 "${i%avi}mp4"; done
# Convert mkv to mp4
for i in *.mkv; do ffmpeg -i "$i" -c:a aac -b:a 128k -c:v libx264 -crf 23 -preset fast -s 720x480 -threads 2 "${i%mkv}mp4"; done
# Convert .png to .jpg
for i in *.png; do mogrify -format jpg "$i" && rm "$i"; echo "$i converted to ${i%.*}.jpg"; done
# Same, but recursive
find . -iname '*.png' | while read i; do mogrify -format jpg "$i" && rm "$i"; echo "Converted $i to ${i%.*}.jpg"; done

# Randomize name
for i in *.mp4; do mv -i "$i" "${RANDOM}.mp4"; done

# Recursive copy
find . -name "*.zip" -exec cp -t ./direction_dir {} +
find . -type f -name "*.mp3" -exec cp {} /tmp/MusicFiles \ # Alternative
# Recursive deletion
find . -name "*.*" -print0 | xargs -0 rm -rf
# Recursive md5sum
find -type f -exec md5sum "{}" + > checklist.chk

# Recursive rename with modified date
j=1; for i in `find . -type f`; do mv -- "$i" "$(date +%Y-%m-%d_%H\'%M\'%S -r $i)_$j.${i##*.}"; j=$((j + 1)); done
## Will output: 2021-02-01_15'16'06_foo.webm
## Note: use IFS=$'\n' before the loop, for find to understand space in filename (mandatory on Windows)

# Recursive move in folders
for j in {01..31}; do for i in `find . -name "*\-$j\_*"`; do mkdir -p "$j" && cp "$i" "$j/"; done; done
## Use case: we have a bunch of files named 2021-02-01_15'16'06_bar.webm, we want them to be moved into the folder corresponding to their month (01 to 31)

Video to gif

# Simple
for i in *.mp4; do ffmpeg -i "$i" -an -s 320x240 -pix_fmt rgb24 -r 10 "${i%mp4}gif"; done
# Optimal with compression
for i in *.webm; do ffmpeg -i "$i" -vf fps=10,scale=320:-1:flags=lanczos,palettegen "$i.png"; ffmpeg -i "$i" -i "$i.png" -filter_complex 'fps=10,scale=320:-1:flags=lancz os[x];[x][1:v]paletteuse' "${i%webm}gif"; rm "$i.png"; done
# Recursive on the first 15 seconds with deletion of step's products
for i in `find . -name "*.webm"`;do ffmpeg -ss 0 -t 15 -i "$i" -vf fps=10,scale=320:-1:flags=lanczos,palettegen "$i.png"; ffmpeg -ss 0 -t 15 -i "$i" -i "$i.png" -filter_complex 'fps=10, scale=320:-1:flags=lanczos[x];[x][1:v]paletteuse' "${i%webm}gif"; rm "$i.png"; rm "$i";done


# Get filename and extension
filename=$(basename -- "$fullfile")

Remplacements multiples de termes dans plusieurs fichiers

La ligne de commande qui peut vous sauver plusieurs heures de boulot :

while read -r pattern replacement; do sed -i -- "s/$pattern/$replacement/" *.tex ; done < patterns.txt

Ça remplace toutes les occurrences de plusieurs termes dans tous les fichiers d'un type donné.

Ça prend en entrée un fichier patterns.txt constitué d'une liste comme suit :

foo bar
truc muche

Où foo et truc seront remplacés respectivement par bar et muche.

Désactiver le touchpad avec Bash

# Enable/Disable touchpad
# $device is found by using "xinput list"

mode="$(xinput --list-props $device | grep $property | cut -d':' -f2)"

if [ $mode -eq "1" ]
    xinput --set-prop $device $property 0
    xinput --set-prop $device $property 1

Supprimer les sauts de ligne en trop dans un sous-titre en .srt

On convertit avec iconv (et détection automatique de l'encoding avec uchardet), car certains sous-titres sont en utf16. Puis on supprime les retour chariot Windows qui empêchent de faire quoi que ce soit. On supprime alors les lignes commençant par un newline. On rajoute un newline aux lignes ne comportant que l'index. Enfin, on supprime la première ligne (qui alors ne contient qu'un newline).

for file in *.srt; do cp "$file" "$file.bak"; encoding=$(uchardet "$file"); iconv -f $encoding -t utf8 "$file" -o "$file.utf8"; tr -d '\r' < "$file.utf8" | sed '/^$/d' | sed 's/^\([[:digit:]]\+\)$/\n\1/gm' | tail -n +2 > "$file"; rm "$file.utf8"; done

Find duplicates


fdupes  /home/chris
fdupes -r /home/chris # Recursive
fdupes -d /home/chris # Delete the duplicates (interactive)

Utilisation de find exec commandes imbriquées

Récupération du nom de fichier sans l'extension pour renommage :

find toto -name "*.exe" -type f -exec bash -c "mv '{}' \`dirname '{}'\`/\`basename '{}' .exe\`.txt" \;

Downloading an Entire Web Site with wget

wget \
     --recursive \
     --no-clobber \
     --page-requisites \
     --html-extension \
     --convert-links \
     --restrict-file-names=windows \
     --domains website.org \
     --no-parent \

Keeping an eye on Chrome's memory usage

ps -eo pmem,comm | grep chromium | cut -d " " -f 2 | paste -sd+ | bc

Let Sudo Insult You When You Enter Incorrect Password

Open /etc/sudoers and edit:

Defaults    insults

How to quickly format a HDD with bad sectors (in linux)?

dd bs=512k if=/dev/zero of=/dev/...

Using any gamepad to simulate xbox360 controller for shank/brutal legend/etc

xboxdrv --evdev /dev/input/event15 --evdev-absmap ABS_X=x1,ABS_Y=y1,ABS_RZ=x2,ABS_Z=y2,ABS_HAT0X=dpad_x,ABS_HAT0Y=dpad_y --axismap -Y1=Y1,-Y2=Y2 --evdev-keymap BTN_TOP=x,BTN_TRIGGER=y,BTN_THUMB2=a,BTN_THUMB=b,BTN_BASE3=back,BTN_BASE4=start,BTN_BASE=lb,BTN_BASE2=rb,BTN_TOP2=lt,BTN_PINKIE=rt,BTN_BASE5=tl,BTN_BASE6=tr --mimic-xpad --silent

Linux Delete / Remove MBR

Delete MBR including all partitions:

dd if=/dev/zero of=/dev/sdc bs=512 count=1

Delete MBR only:

dd if=/dev/zero of=/dev/sdc bs=446 count=1

TheFuck - La commande qui corrige vos commandes

Options incluses dans Bash :

!!      = dernière commande
!chaîne     = dernière commande commençant par chaîne
!?chaîne    = dernière commande incluant chaîne
^old^new    = dernière commande en remplaçant la 1e occurrence de old par new
!!:gs/old/new   = dernière commande en remplaçant toutes les occurrences de old par new

How can I replace a string in a file(s)?

Replacing all occurrences of one string with another in all files in the current directory recursively (including hidden files):

 find . -type f -exec sed -i 's/foo/bar/g' {} +

Replace only if the file name matches another string or extension:

find . -type f -name "*baz*" -exec sed -i 's/foo/bar/g' {} +

Replace only if the string is found in a certain context:

sed -i 's/foo\(.*baz\)/bar\1/' file

Parsing JSON with Unix tools

curl -s 'https://api.github.com/users/lambda' | jq -r '.name'
curl -s 'https://api.github.com/users/lambda' | \
    python3 -c "import sys, json; print(json.load(sys.stdin)['name'])"
curl -s 'https://api.github.com/users/lambda' | \
    python2 -c "import sys, json; print json.load(sys.stdin)['name']"

How to decode URL-encoded string in shell?

$ function urldecode() { : "${*//+/ }"; echo -e "${_//%/\\x}"; }


urldecode https%3A%2F%2Fgoogle.com%2Fsearch%3Fq%3Durldecode%2Bbash

Aspirer un site web avec la commande Wget

wget -r -k -np --user-agent=Firefox url-du-site

How to batch rename files (.jpg) randomly?

for f in *.jpg; do mv -n "$f" "${f/*/$RANDOM.jpg}"; done