-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmoving_dicoms_to_subj_root.sh
executable file
·33 lines (26 loc) · 1.12 KB
/
moving_dicoms_to_subj_root.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
#!/bin/bash
# Check if at least one argument is provided
if [ "$#" -lt 1 ]; then
echo "ERROR"
echo "Usage: ./moving_dicoms_to_subj_root.sh <path_to_sourcedatafolder> [directory_name_pattern]"
echo "Example: ./moving_dicoms_to_subj_root.sh /path/to/sourcedatafolder scans"
exit 1
fi
# Assign the first argument to sourcedatafolder
sourcedatafolder=$1
# Assign the second argument to iname_pattern, default to "scans" if not provided
iname_pattern=${2:-scans}
# Loop through each subject directory
for subject_dir in "$sourcedatafolder"/*; do
# Find the directory matching the pattern, even if it is nested
target_dir=$(find "$subject_dir" -type d -iname "$iname_pattern" | head -n 1)
# Check if a directory was found and it's not already in the root
if [ -n "$target_dir" ] && [ "$target_dir" != "$subject_dir/$iname_pattern" ]; then
# Move the directory to the subject's root directory
mv "$target_dir" "$subject_dir"
# Remove any empty directories left behind
find "$subject_dir" -type d -empty -delete
fi
done
# Echo completion message
echo "Moving is completed"