Recent Posts
Get Random Number in Bash
system variable $RANDOM can be used to generate a random number between 0 and 32767, in case you want to get a random number between 0 and 10, you may do the following
$ declare -i number=$RANDOM*10/32767 $ echo $number declare options/typeset: -i integer: following variable will be an integer -r readonly: following variable will be readonly, cannot be reset. -a array: following variable will be an array. -f function -x export: following variable will be exported.
read more
Double Parentheses
Double-Parentheses permits arithmetic expansion and evaluation. it allowing C-style manipulation of variables; for example, ((var++))
$ a=$((8+8)) $ echo $a #16 $ ((a++)) $ echo $a #17 # we can also use let $ let a++ $ echo $a #18
read more
Remove Duplicate Files in Bash
Using the following command can remove duplicate files in current directory:
md5 * | sort -k 4 | uniq -f 3 -d | tr -d '()' | cut -d " " -f 2| xargs rm -v md5: generate file checksums sort -k 4: sort output by column 4 uniq -f 3 -d: ignore the first 3 columns and only shows duplicated lines tr -d ‘()’: remove the ‘(’, ‘)’ characters so we can get duplicated file name by cut cut -d " " -f 2: using space as delimiter and get the file name we want to remove xargs rm -v: remove the duplicated files you may need to modify the command based on your md5 standard output.
read more