-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfilernd
75 lines (70 loc) · 1.38 KB
/
filernd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
while getopts ":p:l:" arg; do
case $arg in
p) prefix=$OPTARG;;
l) length=$OPTARG;;
esac
done
if [[ $* = *"-d"* ]]
then
doDir=true
else
doDir=false
fi
if [[ -z $prefix || -z $length ]]; then
echo "Usage: filernd -l (new length of files) -p (prefix of files)"
echo "set -d to include directories"
exit 1
fi
if [[ $length -le 0 ]];
then
echo "Length cannot be less than 0"
exit 1
fi
if [[ $prefix -le -1 ]];
then
echo "Prefix cannot be cannot be negative"
exit 1
fi
same=0
for i in *;
do
set=1
while [ "$set" -eq 1 ]
do
set=0
random=$(head /dev/urandom | tr -dc A-Za-z0-9 | head -c $(($length-$prefix)) )
fileprefix=$(echo ${i::$prefix})
if [[ $i == *.* ]]
then
end=$(echo $i |awk -F"." '{print "." $NF}')
newfile="$random""$end"
else
newfile="$random"
fi
towrite="$fileprefix""$newfile"
if [ -f "$towrite" ] || [ -d "$towrite" ]
then
echo "Same name detected. Trying again"
set=1
((same++))
if [[ $same -gt 10000 ]]
then
echo -e "\nGot file exists 10,000 times in a row"
echo "We may have run out of file names."
echo "Try setting the length a bit higher. Exiting"
exit
fi
else
if [ -d "$i" ] && [[ $doDir = true ]]
then
mv "$i" "$towrite"
echo "Changing directory $i to $towrite"
elif [ ! -d "$i" ]
then
mv "$i" "$towrite"
echo "Changing file $i to $towrite"
fi
same=0
fi
done
done