-
Notifications
You must be signed in to change notification settings - Fork 0
/
number_photos.sh
executable file
·92 lines (86 loc) · 2.25 KB
/
number_photos.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
#! /bin/bash
function remove_duplicates()
{
for f in $(/bin/ls);
do
sha1sum $f >> sha1.txt
done
for sha1 in $(cat sha1.txt | sed 's/ .*//' | sort | uniq -c | grep -v "1 " | sed 's/ *. *//');
do
echo "Removing duplicated sha1 $sha1";
for f in $(grep $sha1 sha1.txt | sed 's/.* //' | tail -n +2 );
do
rm -v $f
done
done
rm sha1.txt
}
function print_help()
{
echo "Usage $0 DOSSIER_LOCAL DOSSIER_NAS"
echo ""
echo "exemple:"
echo './renommage_date.sh "DOSSIER_A_TRIER'
echo ""
echo "Options:"
echo " --dry-run, -d : Execution en mode simulation, la copie ne sera pas effectuée"
echo " --help, -h : Ce message d'aide"
echo ""
}
dry_run=false
for arg in "$@"; do
case "$arg" in
-h | --help) print_help; exit 0;;
--dry-run | -d) dry_run=true;;
*)
if [[ -z "$DIR" ]]; then
DIR="$arg";
else
echo "Argument en trop : $arg"
exit 1
fi
;;
esac
done
if [[ ! -d "$DIR" ]]; then
echo "Erreur : usage incorrect ou chemins invalides"
print_help
exit 1
fi
echo "Lancement du script de renommage des dates:"
echo " - Dossier à trier : $DIR"
if [ "$dry_run" = true ]; then
echo "Options de lancement de simulation : les opérations seront fictives"
fi
total=0
#To disable the splitting of files with a whitespace in the name
IFS=$(echo -en "\n\b")
TMP="$DIR/temp_tri"
if [ "$dry_run" = false ]; then
mkdir -pv "$TMP"
fi
last_date=""
for f in $(find $DIR -type f -printf "%T@|%p\n" | sort -u | sed 's/.*|//g'); do
((total++))
date=$(stat -c %y "$f" | cut -d ' ' -f 1) # Uncomment to remove year | sed 's/[0-9]*-//')
if [[ "$last_date" != "$date" ]]; then
i=1
fi
last_date=$date
extension="${f##*.}"
destfile="$date ($i).$extension"
destpath="$TMP/$destfile"
echo "Source=$DIR/$f -> $destpath -> $DIR/$destfile"
if [ "$dry_run" = false ]; then
mv "$f" "$destpath"
fi
((i++))
done
if [ "$dry_run" = false ]; then
echo ""
echo "Rappatriement de $TMP vers $DIR"
mv -v "$TMP"/* "$DIR"
fi
test -d $TMP && rmdir $TMP
echo ""
echo "Terminé : renommage de $total fichiers"