-
Notifications
You must be signed in to change notification settings - Fork 2
/
rename-recent
executable file
·90 lines (76 loc) · 2.7 KB
/
rename-recent
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
#!/usr/bin/env bash
set -euo pipefail
trap 'e=$?; [ $e -ne 0 ] && echo "$0 exited in error"' EXIT
env | grep ^DRYRUN= && DRYRUN=echo || DRYRUN=""
usage(){
cat <<H
USAGE:
$(basename $0) rename_regexp outdir file_pattern [rename options]
EXAMPLE:
rename-recent 's/_e\d+(_ph?)(.nii.gz|.json)$/\$2/' ../subjs/sub-11_2489_20201217/fmap/ '*phase*'
SYNOPSIS:
only works on .nii.gz, .json, and .nii files!
use regular express "rename_regexp" (see 'rename --man')
to rename files in "outdir"
matching "file_pattern" (-iname "\$file_pattern")
there were created in the last day (find -mtime -1)
optionally give e.g. -n at the end for dryrun
(options passed on to 'rename')
ALT USAGE:
$(basename $0) have?
check that we have the right 'rename' command. print it if we do. error to STDERR otherwise
NOTE:
* some work is done to make sure perl-rename is being used instead of another rename utility
* created for 'mknii'
H
exit 1
}
find_rename(){
# don't have admin access to make perl's rename the defacto
#
# if rename exists it could be as perl-rename or normal utils rename
if command -v perl-rename >/dev/null; then
cmd="perl-rename"
else
cmd="$(which rename)"
fi
# perl's rename has "perl" in the man output
# system name just prints "call: rename from to files..."
# cannot use grep -q. $cmd --man might return failing status code
[ -n "$($cmd --man 2>&1 | grep perl)" ] && echo $cmd && return 0
echo "wrong/no 'rename' command ('$cmd') in \$PATH! try 'cpanm File::Rename' or rearrange PATH" >&2
return 1
}
my_rename(){
local cmd=$(find_rename)
[ -z "$cmd" ] && exit 1
# use it
[ -n "$DRYRUN" ] && dr="-n" || dr=""
$cmd $dr "$@";
}
main(){
env|grep -q ^VERBOSE=. && set -x
# just run "find_rename" -- for debugging and checking
if [ $# -gt 0 ] && [ "$1" == "have?" ]; then
find_rename
exit $?
fi
[ $# -lt 3 ] && usage
# rename using: replace pattern, outdir, file pattern
# prev. used find|xargs rename. but working around old systems w/o admin access
# using "my_rename"
# xargs -r rename 's/[12]?_e(\d.(nii.gz|json))$/$1/'
# xargs -r rename 's/_e\d+(_ph?)(.nii.gz|.json)$/$2/'
local rename_regexp="$1"; shift
local outdir="$1"; shift
local pattern="$1"; shift
[ $# -gt 0 ] && ARGS="$*" || ARGS="" # allow -n to sneak in for dry run tests
torename=( $(find $outdir -type f -mtime -1 -iname "$pattern" \
\( -iname '*nii.gz' -or -iname '*json' -or -iname '*.nii' \)) )
[ -n "$DRYRUN" ] && echo "# rename-recent top level: '$rename_regexp' for '$outdir/$pattern'" &&
echo "# have '${#torename[@]}' matches"
for f in "${torename[@]}"; do
my_rename $ARGS "$rename_regexp" "$f"
done
}
[[ "$(caller)" != "0 "* ]] || main "$@"