mirror of
https://github.com/bytedream/scripts.git
synced 2025-05-09 12:15:12 +02:00
Added hashify filename
This commit is contained in:
parent
634a2420cf
commit
111fb3e505
20
hashify-filename/README.md
Normal file
20
hashify-filename/README.md
Normal file
@ -0,0 +1,20 @@
|
||||
## Hashify 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 .
|
||||
```
|
61
hashify-filename/hashify-filename.sh
Executable file
61
hashify-filename/hashify-filename.sh
Executable file
@ -0,0 +1,61 @@
|
||||
#!/bin/bash
|
||||
|
||||
SAVEIFS=$IFS
|
||||
IFS=$(echo -en "\n\b")
|
||||
|
||||
recursive=false
|
||||
|
||||
while getopts ":r?" opt; do
|
||||
case $opt in
|
||||
r)
|
||||
recursive=true
|
||||
;;
|
||||
*)
|
||||
echo $usage
|
||||
exit 1
|
||||
esac
|
||||
done
|
||||
shift $((OPTIND-1))
|
||||
|
||||
function rename() {
|
||||
local base="$1"
|
||||
shift 1
|
||||
|
||||
for file in $*; do
|
||||
file=${file//[\\]/}
|
||||
filepath="$base/$file"
|
||||
ext="${file##*.}"
|
||||
if [ "$ext" == "$file" ]; then
|
||||
ext=""
|
||||
else
|
||||
ext=".$ext"
|
||||
fi
|
||||
|
||||
if [ -d $filepath ]; then
|
||||
if $recursive && [ ! -L $filepath ]; then
|
||||
rename "$filepath" $(ls -b $filepath)
|
||||
fi
|
||||
continue
|
||||
fi
|
||||
filename="$(md5sum $filepath | awk '{print $1}')$ext"
|
||||
mv "$filepath" "$base/$filename"
|
||||
done
|
||||
}
|
||||
|
||||
if [ "$#" -eq 0 ]; then
|
||||
echo "At least one file must be given"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
declare -a files=()
|
||||
|
||||
for file in $@; do
|
||||
if [[ $file != /* ]]; then
|
||||
file="$(pwd)/$file"
|
||||
fi
|
||||
files+=("$file")
|
||||
done
|
||||
|
||||
rename "/" "${files[@]}"
|
||||
|
||||
IFS=$SAVEIFS
|
Loading…
x
Reference in New Issue
Block a user