-
Notifications
You must be signed in to change notification settings - Fork 1
/
Smooth_Name.sh
executable file
·89 lines (76 loc) · 3.19 KB
/
Smooth_Name.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
#!/usr/bin/env bash
# Author: Lilian BESSON
# Email: Lilian.BESSON[AT]ens-cachan[DOT]fr
# Date: 07-12-2016
# Web: https://bitbucket.org/lbesson/bin/src/master/Smooth_Name.sh
#
# A small script to rename all files in a directory semi-automatically.
# The main purpose is to smooth filenames in order for generatejplayer to work more nicely.
#
# WARNING: the files are renamed (ie. move in place) with no control : it might NOT work.
#
# Requires [smoothnameone.sh](https://bitbucket.org/lbesson/bin/src/master/smoothnameone.sh)
#
# Licence: GPL v3
#
version='1.5'
LANG='fr'
log=/tmp/$(basename "$0").log
# Options: --batch, --test, --onlyfiles
if [ "${1}" = "--batch" ]; then
echo -e "Nothing asked to the user : batch mode (option --batch)."
MV="mv -vf"
shift
elif [ "${1}" = "--test" ]; then
echo -e "Just a test (option --test)."
MV="echo mv -vf"
shift
elif [ "${1}" = "--onlyfiles" ]; then
echo -e "Working only on files (option --onlyfiles)."
MV="mv -vf"
onlyfiles="true"
shift
else
MV="mv -vi"
fi
# Change sub-directory
allfolder=$(find . -type d -iname [\.0-9A-Za-z]'*')
allfolder="${allfolder//' '/%20}"
for folder in ${allfolder}; do
folder="${folder//'%20'/ }"
# folder="${folder#./}"
newfolder="$(smoothnameone.sh "${folder}")"
echo -e "${green}Working with the directory ${u}'${folder}'${U}.${white}"
echo mv -vi "${folder}" "${newfolder}"
$MV "${folder}" "${newfolder}"
folder="${newfolder}"
# Now I use inode, to fix https://bitbucket.org/lbesson/bin/issues/5/handle-new-lines-in-file-names
# TODO use ls -i to get the inode (unique identifier of the file)
# Cf. http://www.softpanorama.org/Tools/Tips/renaming_files_with_special_characters.shtml
allinode="$(find ./"${folder}" -maxdepth 1 -type f -exec ls -i {} \; | grep -o "^[0-9]* ./" | sed s_' ./'_''_)"
for INUM in ${allinode}; do
# Example:
# find . -type f -inum 31467125 -exec mv {} new_name.html \;
file=$(find . -type f -inum "$INUM")
echo -e "${black}Working with the file ${magenta}${u}'${file}'${U}${white}."
# DONE? implement a small test, display a warning if the name of the file is weird
nbline1=$(echo $file | wc -l) # Without quotes
nbline2=$(echo "$file" | wc -l) # With quotes
if [ $nbline1 -lt $nbline2 ]; then
echo -e " ${red}Warning${white}: this file might have weird characters in his name (I guess ${black}$(( nbline2 - nbline1 ))${white}) ..."
fi
localname="$(find . -type f -inum "$INUM")" # The leading ./ has to be removed
# Then use:
# find . -inum "$INUM" mv {} "..."
if [ "X$onlyfiles" = "Xtrue" ]; then
find . -type f -inum "$INUM" -exec $MV {} ./"$(smoothnameone.sh --file ${localname:2})" \; 2>>$log
# $MV "$file" "$(smoothnameone.sh --file "$file")" 2>>$log
## Réf: http://abs.traduc.org/abs-5.3-fr/ch19.html#ioredirref
else
# TODO add the second case without the option --file
find . -type f -inum "$INUM" -exec $MV {} ./"$(smoothnameone.sh ${localname:2})" \; 2>>$log
# $MV "$file" "$(smoothnameone.sh "$file")" 2>>$log
fi
done
done
# END