将文件夹名称的一部分附加到其中的所有.gz文件中 [英] Append part of folder name to all .gz within

查看:52
本文介绍了将文件夹名称的一部分附加到其中的所有.gz文件中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个具有以下结构的数据文件夹文件夹:

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

我想通过添加样本名称而不是随机数来修改每个样本文件夹中的所有data.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

似乎这应该是一个简单的mv for循环,但我一直无法弄清楚如何使用basename拉取一部分文件夹名称.

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

我不知道如何使文件保留在其原始文件夹中,但出于我的目的,可以将所有文件都移至新文件夹("fastq")

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")

推荐答案

我想"sampleName"部分不包含破折号.在这种情况下,请使用标准的模式删除扩展: %% .也就是说,假设您的完整路径(相对于目录根目录)存储在 $ path 中,只需执行 $ {path %%-*} 来提取"sampleName"部分.在 Bash参考手册 %% >有关更多详细信息.举一个简单的例子:

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

否则,您还可以使用基于正则表达式的更高级的子字符串提取.请参见 BashFAQ/100

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.

更新.这是执行所描述工作的完整命令,它完全是shell固有的:

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

这篇关于将文件夹名称的一部分附加到其中的所有.gz文件中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆