-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmove_rename_dupes
executable file
·74 lines (62 loc) · 1.9 KB
/
move_rename_dupes
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
#!/bin/bash
usage() {
echo "Moves all files and directories from source_path to dest_path."
echo "If a file exists in dest_path then the new file from source_path will be renamed (name.ext -> name_dup.ext)."
echo "For dry run (print the commands but not run them) pass the '-dry' option."
echo -e "\nUsage:\n$0 [-dry] source_path dest_path"
exit 1
}
if [[ $# -eq 3 ]] ; then
if [[ $1 != "-dry" ]] ; then
usage
fi
dry="$1"
src="$(readlink -m "$2")"
dst="$(readlink -m "$3")"
elif [[ $# -eq 2 ]] ; then
src="$(readlink -m "$1")"
dst="$(readlink -m "$2")"
else
usage
fi
if [ -z $dry ] ; then
my_mkdir='mkdir'
my_mv='mv'
else
my_mkdir='echo mkdir'
my_mv='echo mv'
fi
find "$src" -type f -print0 |
while IFS= read -r -d '' file; do
target="${dst}/${file#${src}/}"
# bash parameter expansion (%%, ##, etc.): https://stackabuse.com/guide-to-parameter-expansion-in-bash/
source_last_dir="$(basename "$(dirname "$file")"%%/)"
source_last_dir="${source_last_dir%%/}"
target_last_dir="$(basename "$(dirname "$target")"%%/)"
target_last_dir="${target_last_dir%%/}"
target_dir="$(dirname "$target")"
target_dir="${target_dir%%/}"
filename="$(basename "$target" "$fullfile")"
if [ "$source_last_dir" == "$target_last_dir" ]; then
target_dir="${target_dir}_dup"
target="${target_dir}/$filename}"
fi;
echo "\"target_dir=$target_dir\""
echo "\"target=$target\""
#echo "forgi1 target=$target"
#echo "forgi2 dst=$dst"
#echo "forgi3 file=$file"
#echo "forgi4 src=$src"
#echo "\"source_last_dir=$source_last_dir\""
#echo "\"target_last_dir=$target_last_dir\""
if [[ ! -e "$target" ]]; then
if [[ -z "$target_dir" ]]; then
$my_mkdir -p "$$target_dir"
fi
$my_mv "$file" "$target"
else
extension="${filename##*.}"
filename="${filename%.*}_dup"
$my_mv "$file" "$target_dir/$filename.$extension"
fi;
done