I have been looking to all possible answers about "copying file from multiple directories to one single directory" but I cannot find my answer. I need something slightly more specific for my database treatment.
I have a similar question to:
copying files from multiple directory to another multiple directory
but how can I copy these files (they all have exactly the same name) to a new directory and add them the parent directory name, so that I can recognize from which directory they were copied ?
Yes I could copy and rename each of name one after the other, if I hadn't 100 of them 🙂
I tried loops with find . etc, but still, I cannot add the copied files the parents directory name.
I am still junior in this field, and so I am starting with regex. I guess there is something to do here with them, but I cannot figure out how exactly.
So, this is my question graphically. How to do that with a simple command, or a loop, on my unix terminal:
DIR_a1 --> output_file.bam
DIR_a2 --> output_file.bam
DIR_a3 --> output_file.bam
Copy all output_files.bam
in NEWDIR_output
so that they appear with parent directory name:
output_file_a1.bam
output_file_a2.bam
output_file_a3.bam
In advance, very thankful for any input
Best Answer
Copying files from a source- into a target directory, adding the containg folder's name to the file
The script below will copy files recursively from a source directory into a flat directory, adding the name of the (direct) superior directory to the file's name. The directory's name will be inserted before possible extension on the file, exacvtly like your example.
The script
How to use
copyandrename.py
Run it with the source- and target directories as arguments:
My test command was e.g.
As always, please test on a sample first!