leponceau.org

Programming And Stuff, You Know The Thing…

Useful BASH Commands

Posted at — Apr 15, 2006

fix file name encoding

for f in *; do
    echo -n "$f" > a
    ff=`iconv -f utf8 -t iso-8859-1 a`
    mv "$f" "$ff"
done

generate m3u for http

#!plain
ls | while read l; do perl -e 'use URI; print "http://192.168.0.7:81/Vortraege_und_Interviews/UCBerkeley/Economics1/mp3/".URI->new(shift @ARGV)->canonical.$/;' "$l"; done  | grep -v "\.m3u$" > all.m3u

convert flv to mp3

#!plain
find . -name '*.flv' -print0 | xargs -0 -P$(grep -c ^proc /proc/cpuinfo) -I '{}' ffmpeg -y -i '{}' '{}'.mp3

sleepy disk scan

#!plain
set -x
i=0
while test -f $i || dd if=/dev/sdc of=/dev/null bs=1M count=102400 iflag=direct skip=$((i*102400)); do
  touch $i
  i=$((i+1))
  sleep 1800
done

get absolute path

#!plain
echo $(readlink -f $(dirname "a/b"))

parallel execution

#!plain
cat ../hosts | xargs -P50 -n1 -I '{}' bash -c '
    set -x
    test -e {} || (
        echo ssh -o PasswordAuthentication=no -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no -o ConnectTimeout=300 \
            -o TCPKeepAlive=yes -o BatchMode=yes -o ServerAliveInterval=3 -o ServerAliveCountMax=40 \
            root@{} "set -e ; if pidof dmeventd; then kill -9 \`pidof dmeventd\`; fi" && touch {}
    )'

dvd copy with error retry

#!plain
## NOT WORKING! ##
nblocks=$(( $(blockdev --getsz /dev/sr0) / 2048 + 10))
for i in $(seq 0 $nblocks); do
    if [[ -e $i ]]; then continue; fi
    dd if=/dev/sr0 of=$i bs=1M count=1 skip=$i || rm $i
done

shorten filenames

#!plain
# beware of the fact that we include the path in the file name!
# (you should use this always against the current working directory, if possible)
find . -type f | while read f; do base=${f%.*}; ext=${f##*.}; if [[ ${#base} -gt 150 ]] && [[ -n $ext ]] && [[ -n $base ]]; then mv -v "$f" "${base:0:150}.$ext"; fi ; done

replace spaces with underscores in filenames

#!plain
find . -type f -name '* *' -maxdepth 1| while read l; do mv -v "$l" "${l// /_}"; done
# update file lists
cat md5sums.checkme | perl -ne 's/(?!.*\*) /_/g;print' > md5sums

find the most recently updated files/directories

#!plain
find . -printf '%T@  %Tc  %p\n' | sort -k 1 -n | tail -n 1000 | while read l; do echo "${l#*  }"; done
LC_ALL=C find . -printf '%S  %Tc  %p\n' | LC_ALL=C sort -k 1 -n | tail -n 1000 | while read l; do echo "${l}"; done

LC_ALL=C find /usr /sbin /bin /opt /var /root /home /lib /tmp -printf '%A@  %Ac  %p\n' | cut -b -10,23- | sort -k 1 -n

# better:

LC_ALL=C find . -printf '%T+  %p\n' | LC_ALL=C sort -k 1 | tail -n 1000 | while read l; do echo "${l}"; done

# files last accessed (not working with noatime mount option!):
LC_ALL=C find . -printf '%a+  %p\n' | LC_ALL=C sort -k 1 | tail -n 1000 | while read l; do echo "${l}"; done

play sound when server is down

#!plain
while true; do while ping -w 10 k6; do sleep 3; done ; aplay ~/Sounds/kalarm.wav; sleep 3; done

stop and restart a user’s processes

#!plain
killall -STOP -u username
killall -CONT -u username

Kill a user’s second rsync process

In case he runs the same transfer twice on a low bandwidth connection…

#!plain
while [ 1 ]; do
  sleep 2
  [[ $(ps -U mw | grep rsync | grep -v grep | wc -l) -lt 2 ]] && continue
  kill $(ps -U mw | grep rsync | grep -v grep | tail -n 1 | awk '{ print $1 }')
done

Auto rename

#!plain
mkdir dest
while [ 1 ]; do
  sleep 1
  dt=$(date +%Y%m%d-%H%M%S)
  while read f; do
    [ "$f" == "${f%.part}" ] && mv "$f" "dest/$dt ${f#./}"
  done < <(find . -type f -mindepth 1 -maxdepth 1)
done

Total File Size of File List

#!plain
let "s=0";while read f; do t=$(find "$f" -printf %k); let "s=s+t"; done < top_cafe/filelists/Alpgen_W+lp_inclusive.list

Extract URLs from HTML

#!plain
sed index.html -e 's:.*HREF="::' -e 's:">.*::' | grep "^http" > out.m3u

Encode OGG Files

#!plain
# "-" -> " - "
while read f; do
  track=${f%%-*}
  title=${f%.wav}
  title=${title#*-}
  mv "$f" "$track - $title.wav"
done < <(ls *.wav)

# encode
# format: "01 - title.wav", the " - " is important!
while read f; do
  track=${f%% - *}
  track=${track#0}
  title=${f%.wav}
  title=${title#* - }
  oggenc -q 5 -t "$title" -N $track -a "Iron Maiden" -l Killers "$f"
done < <(ls *.wav)


#!plain
# $ ls *.wav
# CD1 - 01 - Preludio.wav
# CD1 - 02 - Che faceste.wav
# CD1 - 03 - Giorno non vidi mai si fiero e bello.wav
# CD1 - 04 - Due vaticini compiuti or sono.wav
# ...

while read f; do
  cdn=${f%% - *}
  track=${f#* - }
  track=${track%% - *}
  title=${f%.wav}
  title=${title#* - }
  title=${title#* - }
  oggenc -q 5 -t "$title" -N $track -a "Giuseppe Verdi" -l "Macbeth ($cdn)" -d 1959 -G Opera "$f"
done < <(ls *.wav)

HTTP Uptime Check

#!plain
while [ 1 ]; do
  wget --no-check-certificate -t 2 https://somehost.com -O /dev/null || {
    date >> log 
    echo ERROR >> log
    echo >> log
  }
  sleep 180
done

Misc

#!plain
qstat -u username | grep LONG | while read l; do qdel ${l%%.*}; done


#!plain
# ebuild test
dnotify . -q 0 -e xterm -e "USE=-selinux ebuild openssh-krb5-3.8.1_p1.ebuild digest ; emerge -B openssh-krb5 ; echo "done" ; read"

Load Notification

#!plain
while [ 0 == 0 ]; do
    sleep 30
    LAVG=`cat /proc/loadavg`
    LAVG=${LAVG%% *}
    if perl -e "if ( $LAVG < 0.2 ) { exit 0; } exit 1;" ; then
        cat /proc/loadavg | mail -s "Load notification for host `hostname`" kalium@gmx.de
        exit
    fi
done