-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathniiMover
executable file
·67 lines (58 loc) · 1.72 KB
/
niiMover
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
#/bin/bash
# modify this as needed to arrange .nii files into subfolders for preprocessing
#
# input: a directory of .nii files named nnAAA.nii
# where n are numberical digits and AAA is any other non digit characters
#
# .nii files will be moved into sub-directories named sub0000nn
#
# James Valenti, October 2015
if [[ $# -lt 1 ]]
then
echo "take a path to a directory of files and move them into subfolders"
echo "Where subfolders are based upon the file name (look at code for regex)"
echo "Usage: $0 PATH"
echo "or to test what would be done: $0 -t PATH"
echo " ie ./ret -t /data2/archived/CMU_60"
exit 1
fi
test=0
if [[ "$1" == '-t' ]]
then
test=1
# remove -t, the next paramater should be a directory
shift
fi
##########################################
# Edit this regular expression as needed #
##########################################
# select the leading numbers from the file name
regex='(^[0-9]+)*'
if [ -d "$1" ]
then
# for all files in the path passed into the script
for file_name in $(ls $1)
do
# if there is a match
if [[ $file_name =~ $regex ]]
then
# directory name to create and file to move into it
newDir=$(printf "$1/sub%06d/\n" ${BASH_REMATCH[1]})
fq_fn="$1/$file_name"
if [[ $test -eq 1 ]]
then
# test: jsut show what would be done
echo "$fq_fn --> $newDir"
else
# create directory and move file
mkdir $newDir
mv $fq_fn $newDir
fi
else
# warn that there is something in the directory that doesnt match
echo "** $1 does not match regular expression, skipping"
fi
done
else
echo "ERROR: $1 is not a directory or does not exist"
fi