1

I have a folder of data folders with the following structure:

sampleName1-randomNumbers/subfolder1/subfolder2/subfolder3/data1.gz
sampleName1-randomNumbers/subfolder1/subfolder2/subfolder3/data2.gz
sampleName2-randomNumbers/subfolder1/subfolder2/subfolder3/data1.gz

I want to modify all the data.gz within each sample folder by appending the sample name but not the random numbers to get:

sampleName1-randomNumbers/subfolder1/subfolder2/subfolder3/sampleName1_data1.gz
sampleName1-randomNumbers/subfolder1/subfolder2/subfolder3/sampleName1_data2.gz
sampleName2-randomNumbers/subfolder1/subfolder2/subfolder3/sampleName2_data1.gz

It seems like this should be a simple mv for loop but I haven't been able to figure out how to pull part of a folder name using basename.

for i in */Data/Intensities/BaseCalls/*.gz; do mv $i "fastq""/"${i%%-*}"."`basename $i`; done

I couldn't figure out how to make the files stay in their original folder but for my purposes it works to have all the files go to a new folder ("fastq")

thermophile
  • 143
  • 1
  • 7

1 Answers1

1

I suppose the "sampleName" part doesn't include dashes. In that case, use the standard pattern removal expansion: %%. That is, suppose your full path (relative to directory root) is stored in $path, just do ${path%%-*} to extract the "sampleName" part. Search for %% in the Bash Reference Manual for more details. As a simple example:

> path=sampleName1-randomNumbers/subfolder1/subfolder2/subfolder3/data1.gz
> echo ${path%%-*}
sampleName1

Otherwise, you could also use more advanced substring extraction based on regex. See BashFAQ/100 or Manipulating Strings from the TLDP Advanced Bash Scripting Guide.

Update. Here's the full command to perform the job described, and it is entirely native to the shell:

for file in */Data/Intensities/BaseCalls/*.gz; do
    mv "$file" "${file%/*}/${file%%-*}_${file##*/}"
done
4ae1e1
  • 7,228
  • 8
  • 44
  • 77
  • thanks that got me on the correct path. I've edited my question with the for loop that does what I need – thermophile Oct 27 '15 at 19:55
  • What do you mean by "I couldn't figure out how to make the files stay in their original folder"? What prevents you from doing `$(dirname "$i")`? Also, is your problem solved? If it is, you can accept the answer. – 4ae1e1 Oct 27 '15 at 20:09
  • I didn't try dirname, I was trying different configurations of ${path } with % or # – thermophile Oct 27 '15 at 20:21
  • Good. I pointed you to `dirname` because you were using `basename` already, and it is easier to understand for people who are new to parameter expansion. But since you asked for it, do note that using parameter expansion instead of `dirname` and `basename` is more performant, because it is native to the shell and doesn't require forking, which is one of the most expensive operations in shell scripts. I have updated my answer to show you how to do the job entirely natively. – 4ae1e1 Oct 27 '15 at 20:32
  • AH! thanks for your complete example, I wasn't understanding the distinction between %% and % or ## and # – thermophile Oct 30 '15 at 15:38