Sunday, May 19, 2024
0
rated 0 times [  0] [ 0]  / answers: 1 / hits: 831  / 2 Years ago, sun, april 3, 2022, 9:36:28

I'm new to ubuntu and also to the programming world, and I don't really know how to do this!


I have a folder with lots of files named as follows:


000_S_001_mpc_asd.json
000_S_001_mpc_asd.nii
000_S_001_mpc_asd_aa.nii
011_S_001_mpc_asd.json
011_S_001_mpc_asd.nii
011_S_001_mpc_asd_aa.nii
000_S_002_mpc_asd.json
000_S_002_mpc_asd.nii
000_S_002_mpc_asd_aa.nii
000_S_001_dtd_rty.bval
000_S_001_dtd_rty.bvec
000_S_001_dtd_rty.nii
000_S_001_dtd_rty.json
011_S_001_dtd_rty.bval
011_S_001_dtd_rty.bvec
011_S_001_dtd_rty.nii
011_S_001_dtd_rty.json
000_S_002_dtd_rty.bval
000_S_002_dtd_rty.bvec
000_S_002_dtd_rty.nii
000_S_002_dtd_rty.json
011_S_001_flf_lkj.json
011_S_001_flf_lkj.nii
011_S_001_flf_lkj_aa.nii
000_S_001_flf_lkj.json
000_S_001_flf_lkj.nii
000_S_001_flf_lkj_aa.nii
000_S_002_flf_lkj.nii
000_S_002_flf_lkj_aa.nii

Let's say xxx_S_xxx is the principal name, and the rest of the file's name gives secondary information (let's call it a secondary name).


I would like to find a specific name into the secondary name and make a folder with this name (for example mpc, dtd or flf), then make subfolders named as the principal name of each file, and into those folders put the respective files.
Probably an image will explain better what I'm trying to say.


So for example, the output for the names I gave you above would look like this:


Desired output:


Is this possible to do from the terminal?
I would appreciate your help.


My OS is Ubuntu 20.04 LTS


More From » command-line

 Answers
3

I'm not quite sure how to do it purely in the terminal without it getting too difficult to read, but I think you could get the result you are looking for, or at least get started, with something along these lines:


Edit: updated with info from the comment. Also swapped secondary and primary since those were backwards.


Edit2: realized that while the secondary name no longer relied on placement, the principle name relied on secondary's placement.


#!/bin/bash

input_directory="/path/to/your/data"
output_directory="/path/to/your/output"

cd "$input_directory"
for file in *; do

if [ ! -f "$file" ]; then
continue;
fi

# place your secondary names inside "(mpc|flf|dtd)" seperated by '|'
secondary=$(echo "$file" | grep -o -E "(mpc|flf|dtd)");
princple=$(echo "$file" | grep -o -E "([0-9]+_S_[0-9]+)");

# skip over and alert that a secondary match was not found for a file
if [ "$secondary" == "" ]; then
echo "No secondary match found!! Skipping $file";
continue;
fi

destination="${output_directory}/$secondary/$princple"

# create the directories if they don't exist
if [ ! -d "$destination" ]; then
mkdir -p "$destination";
fi

# uncomment to move the files to the new directories if the test output
# from echo is correct
#mv "$file" "$destination"

# test to print result of moving the files
from=$(readlink -f "$file")
echo "$from -> $destination/$file"
done

grep -o -E "(mpc|flf|dtd)" searches the filename for one of the secondary name keywords, e.g. (mpc, dtd or flf), and saves that word to the secondary variable.


grep -o -E "([0-9]+_S_[0-9]+) Same idea, by looks for the xxx_S_xxx pattern.


It can be run as: bash script.sh


The input_directory and output_directory variable will need to be filled in with the correct paths. Also, the fields "(mpc|flf|dtd)" in the grep statement can be filled in with other secondaries.


[#1452] Monday, April 4, 2022, 2 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
terose

Total Points: 185
Total Questions: 125
Total Answers: 131

Location: Venezuela
Member since Mon, Dec 13, 2021
2 Years ago
terose questions
;