The english version of this page is here: 🇬🇧

Einige Linux Shell Skripten

Suche nach doppelten Dateien

Zuerst präsentiere ich ein Linux-Shell-Skript, das nach doppelten Dateien basierend auf ihrem Inhalt sucht. Die Idee dahinter ist, den SHA256-Hash des Datei-Inhalts zu berechnen. Wenn zwei Dateien den gleichen SHA256-Hash haben, wird davon ausgegangen, dass sie identisch sind.

                    
#! /bin/bash
# usage: dupsearch pattern
# where pattern can be *.jpg or *.png or *.mp3 or something else
# creates 2 temporary files

tmpfile1=$(mktemp /tmp/dupjpg.XXXXXX)
tmpfile2=$(mktemp /tmp/dupjpg.XXXXXX)

# connects the files with filedescriptor 3 and 4
exec 3>"$tmpfile1"
exec 4>"$tmpfile2"

# finds the files, calculates the SHA256-hash and sorts the file
find . -name "$1" 2>/dev/null|xargs sha256sum -b 2>/dev/null|sort >&3 

# the sorted file is in order of the hash codes, but the codes 
# are together with the filenames. cut the codes from the filenames
# and leave only duplicate hash codes. save them to file tmpfile2
cut -d ' ' -f 1 "$tmpfile1"|uniq -d >&4

# read the codes from the file and look for the filenames
# the filenames are in in the position beginning after position 67 in 
# the file
grep -f "$tmpfile2" "$tmpfile1" | cut -c 67-

# delete the temporary files and close the filedescriptors
rm "$tmpfile1"
rm "$tmpfile2"
exec 3>&-
exec 4>&-
                    
                

 

Eine Diashow aus einer Playlist anzeigen

Dieses Skript zeigt eine Diashow aus einer Playlist mit Dateinamen, Zeile für Zeile, in einer Datei an. Das Interessante an dem Skript ist, dass /dev/stdin als Dateideskriptor 3 geöffnet wird. Und obwohl die Eingabe für die while-Schleife auf die Datei umgeleitet wird, aus der die Playlist gelesen wird, kann man trotzdem Tastatureingaben von /dev/stdin lesen. So hat man zwei Eingabeströme. Ansonsten ist das Skript ziemlich einfach. Die Bilder werden mit feh angezeigt.

                                                  
#! /bin/bash

exec 3< /dev/stdin

read -n 1 -p "Press any key to begin slideshow";
while IFS= read -r line;
do
    echo $line
    feh -g 1024x800 --scale-down $line
    read -n 1 -p "Press any key to continue ^C to exit" <&3
done < $1