-
Notifications
You must be signed in to change notification settings - Fork 14
/
ren-from-csv.sh
77 lines (56 loc) · 2.29 KB
/
ren-from-csv.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
#!/bin/bash
# Utility to Rename a set of files in current dir using a .csv input file - portable and fairly simple
# REQUIRES: awk, column
# 2022.Dec Kingneutron
# HOWTO:
# cd to dir you want to work on
# Populate a temporary file with bare filenames: (NOTE don't have to use ls, can use "find" or whatever)
# ls -1 >tmp.csv
# Preprocess tmp file and add comma + duplicate filename:
# awk '{print $1",\t"$1}' tmp.csv |column -t >rename.csv
# Edit tmp file and replace 2nd filename after comma with preferred filename
# IMPORTANT: Delete any lines that you DONT want renamed
# Then Run this script
# failexit.mrg
# REF: https://sharats.me/posts/shell-script-best-practices/
function failexit () {
echo '! Something failed! Code: '"$1 $2" >&2 # code (and optional description)
exit $1
}
# xxx TODO EDITME
infile=rename.csv
[ -e $infile ] || failexit 44 "$infile not found in $PWD"
logf=$HOME/ren-from-csv.log
date >>$logf
pwd >>$logf
errlog=$HOME/ren-from-csv-errs.log
>$errlog # clearit
OIFS=$IFS
IFS="
"
#set -x # DEBUGG
while read inputline; do
f2r=$(echo "$inputline" |awk -F',' '{print $1}')
rento=$(echo "$inputline" |awk -F',' '{print $2}' |sed 's/ //g') # no spaces
/bin/mv -v "$f2r" "$rento" >>$logf 2>>$errlog
done < $infile
IFS=$OIFS
echo "DONE - $(date)" >>$logf
ls -lh $logf $errlog
exit;
# HOWTO:
# cd to working dir 1st
# $ ls -1 >tmp.csv # populate temp output file (can also use find or whatever)
wallpapersden.com_olivia-taylor-dudley-2018_2160x3840.jpg
watchmen-youre-locked-in-here-with-me--fark_9jLJBkZ86xroJXgJl_PxH0pBQf8.jpg
we-dont-need-no-stinkin-gadgets--fark_mqkB3Y1u50LMlRwXXyDPjOkwltw.jpg
wunmi-mosaku.jpg
xev-fark_SWJwl7Tm8mZKQydJ60dKAqVJlI0.jpg
# Add comma+tab and Dup to field 2 ++ make easy to read
awk '{print $1",\t"$1}' tmp.csv |column -t >rename.csv
#awk '{print $0",\t"$1}' tmp.csv >rename.csv
# Edit rename.csv and edit 2nd field = new filename to rename to
$ cat rename.csv
we-beat-em-before-red-hat-nazis--fark_D0aakxPNV05v8A6OUS2P6AZAYms.jpg, we-beat-em-before-red-hat-naztis--fark_D0aakxPNV05v8A6OUS2P6AZAYms.jpg
we-dont-need-no-stinkin-gadgets--fark_mqkB3Y1u50LMlRwXXyDPjOkwltw.jpg, we-dont-need-no-steenkin-gadgets--fark_mqkB3Y1u50LMlRwXXyDPjOkwltw.jpg
wunmi-mosaku.jpg, wunmi-mosaku-ren.jpg