-
Notifications
You must be signed in to change notification settings - Fork 0
/
fzf_bulkrename.sh
executable file
·91 lines (74 loc) · 2.16 KB
/
fzf_bulkrename.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
#!/bin/sh
# path: /home/klassiker/.local/share/repos/fzf/fzf_bulkrename.sh
# author: klassiker [mrdotx]
# github: https://github.com/mrdotx/fzf
# date: 2024-10-21T08:15:14+0200
# speed up script and avoid language problems by using standard c
LC_ALL=C
LANG=C
# config
edit="$EDITOR"
cmd="mv -vi"
# help
script=$(basename "$0")
help="$script [-h/--help] -- script to bulk rename files with a text editor
Usage:
$script [--files] <file> [file1] [file2]
Settings:
[--files] = files to rename
Examples:
$script
$script --files file.md file1.csv file2.sh
Config:
edit = $edit
cmd = $cmd"
bulkrename() {
original_file="original"
modify_file="modify"
script_file="bulkrename.sh"
# create cache
cache_folder=$(mktemp -t -d "fzf_bulkrename.XXXXXX")
cat > "$cache_folder/$original_file"
[ -s "$cache_folder/$original_file" ] \
|| return
# modify file names
cp -f "$cache_folder/$original_file" "$cache_folder/$modify_file"
"$edit" "$cache_folder/$modify_file"
# create bulk rename script
printf "%b" \
"#!/bin/sh\n\n" \
"# This script will be executed when you close the editor.\n" \
"# Please check everything! Clear the file to abort.\n\n" \
> "$cache_folder/$script_file"
awk -v cmd="$cmd" 'NR==FNR { a[NR]=$0; next }
$0!=a[FNR] { print cmd" \\\n\t\""$0"\" \\\n\t\""a[FNR]"\"" }' \
"$cache_folder/$modify_file" "$cache_folder/$original_file" \
>> "$cache_folder/$script_file"
# check bulk rename script
"$edit" "$cache_folder/$script_file"
# execute bulk rename script
chmod 755 "$cache_folder/$script_file"
"$cache_folder/$script_file"
# delete cache
[ -d "$cache_folder" ] \
&& rm -rf "$cache_folder"
}
case $1 in
-h | --help)
printf "%s\n" "$help"
exit
;;
--files)
shift
for file_name in "$@"; do
printf "%s\n" "$file_name"
done | bulkrename
;;
*)
find . -maxdepth 1 2> /dev/null \
| sed -e 1d -e 's/^.\///' \
| sort -fV \
| fzf -m +s \
| bulkrename
;;
esac