-
Notifications
You must be signed in to change notification settings - Fork 0
/
bash_blind_rename.sh
executable file
·121 lines (104 loc) · 3.42 KB
/
bash_blind_rename.sh
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
#!/bin/bash
# Bash generate alphanumeric string UPPERCASE and numeric to obfuscate file names
# Can't be run as superuser or root
# Ignore name_dictionary.csv file for renaming
# Insensitive to filenames and folders with empty spaces
# Keep file extension untouch.
# Control if folder have been already randomized by existence of name_dictionary.csv
# Check that folder it is in fact a folder and do not goes in subfolders
# Reduce risk of filename collisions with shasum filename, cut in half
# Create file Analysis_file.csv with only new names to be used for manual analysis
IFS=$'\n'
N=$(find "$1" -maxdepth 1 -type f | wc -l)
if [ "$N" -ge 200 ]
then
echo "There are too many files in $1 for manual quantification"
echo
read -p -r "Are you sure to continue? (Y/N)" answer
case ${answer:0:1} in
y|Y )
echo
echo
echo
if [ "$(id -u)" == "0" ]; then
echo "Sorry, do not run this as superuser."
exit 1
else
if [ ! -d "$1" ]; then
echo "$1 does not exists"
exit 1
else
if [ -f "$1/name_dictionary.csv" ] || [ -f "$1/Analysis_file.csv" ] ;then
echo "Files on folder already renamed"
exit 1
else
if [ -f "$1/name_dictionary_DEPRECATED.csv" ];then
echo "Files on folder already randomized once and reverted"
echo "Please delete name_dictionary_DEPRECATED.csv on $1"
exit 1
else
echo "Oldname,Newname" > "$1/name_dictionary.csv"
echo "Newname" > "$1/Analysis_file.csv"
#for file in $(find "$1" -maxdepth 1 -type f -printf "%f\n" ) #doesn't work on OSX
for file in $(ls -p "$1" | grep -v /)
do
if [ "$file" == "name_dictionary.csv" ] || [ "$file" == "Analysis_file.csv" ] ; then
continue;
fi
NEWNAME=$(echo $file | shasum | tr 'a-z' 'A-Z' |tr -dc 'A-Z0-9' | cut -c 1-20 )
ext=${file##*.}
echo "$file,$NEWNAME.$ext" >> "$1/name_dictionary.csv"
mv "$1/$file" "$1/$NEWNAME.$ext"
echo "$NEWNAME.$ext" >> "$1/Analysis_file.csv"
done
fi
fi
fi
fi
echo "DONE"
;;
*)
echo "Exit without any modification"
exit 1
esac
else
echo
echo
echo
if [ "$(id -u)" == "0" ]; then
echo "Sorry, do not run this as superuser."
exit 1
else
if [ ! -d "$1" ]; then
echo "$1 does not exists"
exit 1
else
if [ -f "$1/name_dictionary.csv" ] || [ -f "$1/Analysis_file.csv" ] ;then
echo "Files on folder already renamed"
exit 1
else
if [ -f "$1/name_dictionary_DEPRECATED.csv" ];then
echo "Files on folder already randomized once and reverted"
echo "Please delete name_dictionary_DEPRECATED.csv on $1"
exit 1
else
echo "Oldname,Newname" > $1/name_dictionary.csv
echo "Newname" > $1/Analysis_file.csv
#for file in $(find "$1" -maxdepth 1 -type f -printf "%f\n" ) #doesn't work on OSX
for file in $(ls -p "$1" | grep -v /)
do
if [ "$file" == "name_dictionary.csv" ] || [ "$file" == "Analysis_file.csv" ] ; then
continue;
fi
NEWNAME=$(echo $file | shasum | tr 'a-z' 'A-Z' |tr -dc 'A-Z0-9' | cut -c 1-20 )
ext=${file##*.}
echo "$file,$NEWNAME.$ext" >> "$1/name_dictionary.csv"
mv "$1/$file" "$1/$NEWNAME.$ext"
echo "$NEWNAME.$ext" >> "$1/Analysis_file.csv"
done
fi
fi
fi
fi
echo "DONE"
fi