I’ve been cataloging various shell commands and scripts that have been quite useful for various Linux (and even Windows via cygwin/git-shell) activities. The ones I list here are either not found via Google search or required a bit of hackery to suit my purposes.
Add a copyright (or author) comment to a bunch of Java files.
find src -type f -exec sh -c "sed -i '1s/^/\/\/ Copyright 2013 Erik Reed ([email protected])\n\n/' {}" \; |
Result:
// Copyright 2013 Erik Reed ([email protected]) package java.com.tons.of.nested.packages.for.no.reason.coolapp; import java.io.Serializable; import java.text.ParseException; // ... |
Convert many image formats in parallel (jpegs to png here). Using 64 processes in this case:
find . -type f -name '*.jpg' -print0 | xargs -0 -n1 -P 64 sh -c 'convert "$1" "${1%.jpg}.png"' sh |
Make file extensions lowercase (a common issue I’ve had with mixing Windows/Linux).
find . -name '*.*' -exec sh -c ' a=$(echo {} | sed -r "s/([^.]*)\$/\L\1/"); [ "$a" != "{}" ] && mv "{}" "$a" ' \; |
Rsync files to/from remote machine as root (e.g. to backup multiple users, root owned files, or a full disk backup).
rsync -avze ssh --rsync-path='sudo rsync' myUser@REMOTE_SERVER:/REMOTE_PATH/ LOCAL_PATH |
Clear disk cache and swap:
# clear cache sync; echo 3 > /proc/sys/vm/drop_caches # clear swap swapoff -a; swapon -a |
Print the mean and standard deviation of a file/stdin (in one line):
awk '{ sum += $1; sumsq += $1*$1 } END { printf "Mean: %f, Std: %f\n", sum/NR, sqrt(sumsq/NR - (sum/NR)^2) }' |