From 306a4ec86cd17e883a14d5dd6bab889c02adc816 Mon Sep 17 00:00:00 2001 From: ByteDream Date: Mon, 24 May 2021 23:07:00 +0200 Subject: [PATCH] Added randomize filename --- randomize-filename/README.md | 20 ++++++ randomize-filename/randomize-filename.sh | 79 ++++++++++++++++++++++++ 2 files changed, 99 insertions(+) create mode 100644 randomize-filename/README.md create mode 100755 randomize-filename/randomize-filename.sh diff --git a/randomize-filename/README.md b/randomize-filename/README.md new file mode 100644 index 0000000..2bdbe10 --- /dev/null +++ b/randomize-filename/README.md @@ -0,0 +1,20 @@ +## Randomize Filename + +With this little script you can randomize the names of given files. File extension won't be changed. + + +#### Arguments + +The file takes several arguments + +`-r` - recursive: If any directory where given while calling this command, all files in it will be renamed recursively. + +`-a` - all (the name is a little misleading): Normally directories are not renamed, but with this argument they will. + +`-l LENGHT` - lenght: The lenght of the randomized filenames (default is 16). Replace `LENGHT` with the lenght the randomized names should have. + +#### Usage + +``` +$ randomize-filename.sh -r . +``` diff --git a/randomize-filename/randomize-filename.sh b/randomize-filename/randomize-filename.sh new file mode 100755 index 0000000..5a562f4 --- /dev/null +++ b/randomize-filename/randomize-filename.sh @@ -0,0 +1,79 @@ +#!/bin/bash + +SAVEIFS=$IFS +IFS=$(echo -en "\n\b") + +usage="$0 [-r] [-a] [-l LENGHT] files..." + +recursive=false +all=false +lenght=16 + +while getopts ":r?a?l:" opt; do + case $opt in + r) + recursive=true + ;; + a) + all=true + ;; + l) + lenght=$OPTARG + echo $lenght + if [[ ! $lenght =~ ^[0-9]+$ ]]; then + echo $usage + exit 1 + fi + ;; + *) + echo $usage + exit 1 + esac +done +shift $((OPTIND-1)) + +function rename() { + local base="$1" + shift 1 + + for file in $*; do + file=${file//[\\]/} + echo $file + filepath="$base/$file" + ext="${file##*.}" + if [ "$ext" == "$file" ]; then + ext="" + else + ext=".$ext" + fi + filename="$(tr -dc A-Za-z0-9