Search code examples
piperenamemkdir

use the output of rename to make directory wo renaming the files


I have a directory with multiple files that they all end as "_1.fastq.gz" or "_2.fastq.gz"

ERR3258060_1.fastq.gz  
ERR3258060_2.fastq.gz  
ERR3258861_1.fastq.gz  
ERR3258861_2.fastq.gz

What I want to do (in a first step) is take all the files that end in _1.fastq.gz keep the first 10 characters of the filename (the ERR32.....) and make a directory with that

therefore the directories would look like ERR3258060 ERR3258861

using bash I have so far tried the following

for f in *_1.fastq.gz
do
echo $f | rename -n 's/_1.fastq.gz//' * | mkdir
done

The command produces the filename the way i want it but mkdir will not take the output of the rename, it will simply say "missing operand" how can I make mkdir take the result of the previous command?

thanks


Solution

  • No need to use rename. You can just use sed. In the first step, you create the directory name and then use it in mkdir.

    for f in *_1.fastq.gz
    do
        dir=$(echo $f | sed 's/_1.fastq.gz//')
        mkdir $dir
    done