I have this parent directory X and these two subdirectories A and B (see Photo 1):enter image description here Folder A contains files for specific subjects that I want to move to further corresponding folders within Folder B. For example, in folder A, I have a lot of files for different subjects that end with different extensions (eeg.eeg, eeg.json, eeg.vhdr, and eeg.vmrk) (see Photo 2).enter image description here

I want to take all these files for each subject and move them to subfolders of B, where there is a subfolder for all subjects: B>>"subject_id">>ses-t1>>eeg. The destination folders should be the "eeg" folders in each participant's folder. enter image description here.

The desired result should be as is depicted in the photo below (Photo 3): enter image description here

The good thing is, I think, that the starting name of the files in folder A, correspond with the subject subfolder name. For example, the file starts with sub-CDPC0001_ses-01_task-rest_eeg.eeg, which is similar to the subject folder for this particular subject within folder B (sub_CDPC0001)

In the past, there has been a mini script used for this, but I don't think works properly on this arrangement right now:

for dir in $(ls -d */ses-t1); do
find "./$dir/" -name '*_eeg.eeg' -exec mv {} "$dir/eeg" \;
find "./$dir/" -name '*_eeg.vhdr' -exec mv {} "$dir/eeg" \;
find "./$dir/" -name '*_eeg.vmrk' -exec mv {} "$dir/eeg" \;
find "./$dir/" -name '*_eeg.json' -exec mv {} "$dir/eeg" \;
#find "./$dir/" -name '*_anat.nii.gz' -exec mv {} "$dir/t1" \;

This lists all the files from folder A but fails to move them to the desired eeg folder. I would appreciate an answer very much, as there are a lot of data, and moving them manually is prone to error and very tedious.

  • Please don't post images of text. Copy and paste the text itself into your question and format it as code by selecting it and pressing Ctrl-K or by using the editor's {} icon. In this case, it's probably best to post a minimally comprehensive excerpt from the output of find A/ B/
    – cas
    Nov 11, 2022 at 16:09

1 Answer 1


First, install the perl rename utility. You seem to be using a Mac, so you'll probably need to use brew, it has perl rename packaged - see https://formulae.brew.sh/formula/rename.

Once that's installed, you should be able to run something like:

$ find A/ -regex '.*_eeg.\(eeg\|vhdr\|vmrk\|json\)' \
    -exec rename -n 's=^.*/(sub-[^_]*)_=B/$1/eeg/$1_=' {} +

NOTE: I'm not sure exactly which version of find is installed on Macs these days. I'm assuming it's a FreeBSD's version of find and has a -regex predicate. If not, use brew to install GNU find and use that instead of the default Mac find. Without -regex you could do it by OR-ing several -name predicates together (in parentheses to force precedence):

find A/ \( -name '*_eeg.eeg' -o -name '*_eeg.vhdr' -o -name '*_eeg.vmrk' -o -name '*_eeg.json' \) -exec rename ...`

Also note that rename's -n option makes it a dry run, so it will only show what it would do without actually renaming any files. Remove the -n, or replace it with -v for verbose output, when you've confirmed that it does what you want. Modify the rename script if it's not quite right until it works as you want it.

BTW, this assumes that B/subject/ and B/subject/eeg/ already exist. If they don't and you want the rename script to create the directories before renaming the files, insert the following immediately before the s=== substitution operation.

if (m=^.*/(sub-[^_]*)_=) { mkdir "B/$1"; mkdir "B/$1/eeg" };

Sample run (with only one filename because I couldn't be bothered typing in more filenames from your pictures of text for the touch command, but it would work with the .vhdr, .vmrk, and .json files too if they existed in my A/ directory):

$ mkdir -p A B/sub-CDPC0001/eeg/
$ touch A/sub-CDPC0001_ses-01_task-rest_eeg.eeg

$ find A/ -regex '.*_eeg.\(eeg\|vhdr\|vmrk\|json\)' \
    -exec rename -v 's=^.*/(sub-[^_]*)_=B/$1/eeg/$1_=' {} +
A/sub-CDPC0001_ses-01_task-rest_eeg.eeg renamed as B/sub-CDPC0001/eeg/sub-CDPC0001_ses-01_task-rest_eeg.eeg

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .