Moving files from folder to folders efficiently

Hi there, I will soon have in hand 279 BAM files I will need to categorize for independent analysis. These are all stored in the same folder; however, on the side I have 279 empty folders for each one of these samples where I will need to store the results of their evaluations.

The samples have different names all ending in .bam, what I would like to do is sequentially move one sample to one folder where I will then follow up with specific tests inside the folder environment; all folders start with sgdp_ followed by the cardinal number up to 279 in three digit format.

Is there an easy way to do so with mv and some tweaking of sort? Keep in mind it is not important which samples goes in which folder: say the first sample in the common folder does not necessarily have to live in the sgdp_001 folder and even so this would not be a problem.

If anyone has a solution other than manually move each one of them, please let me know! Thanks in advance.

@overcraft, hi, give a couple of worked examples - much simpler and (usually) self explanitory to avoid any ambiguties from interpretation of your requirement.

**NB:**use markdown tags (see the menu options at the top of the dialog box you type into) to enclose code in .

hi i have ....

sample-1

list of files (3 or 4 will suffice)

and I need to get the following (repeated for all instances of files in sample-1

examples of results based upon the initial sample-1

Hi @munkeHoller I see what you mean; here is a workable example:

I have these samples atm

$ ls -l
abh107.bam
BulgarianB4.bam
BulgarianC1.bam
HG00190.bam
.
.
.

which are within one folder (and there are many more). What I would like to do is to move each individual sample to a folder (one for each one of them) where I will conduct specific analysis. I've sequentially created these folders with:

mkdir sgdp_{001..279}

and the end result should be equivalent to do something like:

mv abh107.bam sgdp_001

for each sample which, however, would not only be confusing but also time consuming. To be noted that whatever the first sample will be there is no bound to which folder will end up in as long as there is only one sample per folder.

the brace expansion {001...279} does not quite generate the directories in the manner you want
the leading zero's are ignored
but you are on the right lines

something along the following ...

touch $RANDOM-filename{001..010}.bam

ls -1
191-filename8.bam
19568-filename2.bam
24786-filename9.bam
25837-filename3.bam
29509-filename1.bam
31036-filename5.bam
3121-filename4.bam
4377-filename10.bam
9844-filename6.bam
9964-filename7.bam

inc=1;for file in *.bam; do v=$(printf '%3.3d' $inc); echo "mkdir -p sgdb_$v && mv $file sgdb_$v/"; ((inc++)); done
mkdir -p sgdb_001 && mv 191-filename8.bam sgdb_001/
mkdir -p sgdb_002 && mv 19568-filename2.bam sgdb_002/
mkdir -p sgdb_003 && mv 24786-filename9.bam sgdb_003/
mkdir -p sgdb_004 && mv 25837-filename3.bam sgdb_004/
mkdir -p sgdb_005 && mv 29509-filename1.bam sgdb_005/
mkdir -p sgdb_006 && mv 31036-filename5.bam sgdb_006/
mkdir -p sgdb_007 && mv 3121-filename4.bam sgdb_007/
mkdir -p sgdb_008 && mv 4377-filename10.bam sgdb_008/
mkdir -p sgdb_009 && mv 9844-filename6.bam sgdb_009/
mkdir -p sgdb_010 && mv 9964-filename7.bam sgdb_010/
1 Like

Thank you so much @munkeHoller, of course I removed the echo and quotations from the mkdir block. Intuitively, I thought about a cycle but couldn't really figure out the logic myself, thanks again!

I ended up editing the instruction as follow

inc=1;for file in *.bam; do v=$(printf '%3.3d' $inc); mv $file sgdp_$v/; ((inc++)); done

since I have already created my folders, though I'm not sure why the command

mkdir sgdp_{001..279}

works differently for you, on my distro at least I'm able to create folders with the full three-digit sequential numbers starting at 001. Nonetheless, this approach is more concise!

1 Like

np, glad you've a working solution.

mkdir -p (assuming appropriate permission ...) - if no directory then create, if already there ... do nothing. it'd also cater for directories with differing number of files to be moved.

anyways, you're happy == we're happy :slight_smile:

With mkdir -p sgdp_{001..279} you have to know the number of files beforehand.
The mkdir -p sgdb_$v is adaptive.

In a recent bash you can do
printf -v v '%03d' $((inc++))
Because it works directly, you can use a postfix increment. In contrast, in a v=$( ) the $( ) is a sub shell; variables are not copied back to the main shell.