bash从特定文件扩展名在文件夹中创建子目录

Justaguy

bash下面,我尝试根据具有特定.bam扩展名的文件在目录内创建子目录.bam文件被修剪,结果是保存在文件夹名称$RDIR,也可从原来的文件found.There可能有多个上一级.bam文件,但它们始终是相同的格式。我也发表了评论。谢谢 :)。

重击

DIR=/home/cmccabe/Desktop/folder   ## define data directory path
cd "$DIR" || exit 1  # check directory exists or exit
for RDIR in R_2019* ; do  ## start processing matching "R_2019*" to operate on desired directory and expand
 cd "$RDIR"/BAM   ## change directory to subfolder inside $RDIR
  bam=$(find . -type f -name "*.bam")   # extract .bam
  sample="$(echo $bam|cut -d_ -f3-)" # remove before second underscore
  mkdir -p "${sample%.*}" && mv "$sample" "RDIR"/"${x%.*}"  ## make directory of sample id one level up
done  ## close loop

的结构/home/cmccabe/Desktop/folder---这是$ DIR-

R_2019_00_00_00_00_00_xxxx_xx-0000-00  --- this is $RDIR ---
     BAM   ---subdirectory---
       IonCode_0241_19-0000-Last-First.bam.bai
       IonCode_0241_19-0000-Last-First.bam
       IonCode_0243_19-0001-Las-Fir.bam.bai
       IonCode_0243_19-0001-Las-Fir.bam
     QC    ---subdirectory---

脚本结构之后/home/cmccabe/Desktop/folder---这是$ DIR ---

R_2019_00_00_00_00_00_xxxx_xx-0000-00  --- this is $RDIR ---
     BAM                  ---subdirectory---
     19-0000-Last-First   ---subdirectory---
     19-0001-Las-Fir      ---subdirectory---
     QC                   ---subdirectory---

设置-x

bash: cd: R_2019*/BAM: No such file or directory
++ find . -type f -name '*.bam'
+ bam=
++ echo
++ cut -d_ -f3-
+ sample=
+ mkdir -p ''
mkdir: cannot create directory ‘’: No such file or directory
Vivek Kanadiya

以下行似乎有问题:

 bam=$(find . -type f -name "*.bam")   # extract .bam
 sample="$(echo $bam|cut -d_ -f3-)" # remove before second underscore

修改内容:

这可以一行完成:

i=$(find . -type f -name "*.bam" -print | while read f;do echo "$f" | cut -d_ -f3-;done| cut -f 1 -d '.') ## To take the file names and then cut.

然后添加for循环来制作目录:

for x in $i
        do mkdir -p $DIR/$x
        done

最终脚本:

DIR=/home/vvek/MyLearning/Linux/bam/   ## define data directory path
cd "$DIR" || exit 1  # check directory exists or exit
for RDIR in R_2019* ; do  ## start processing matching "R_2019*" to operate on desired directory and expand
 cd "$RDIR"/BAM   ## change directory to subfolder inside $RDIR
i=$(find . -type f -name "*.bam" -print | while read f;do echo "$f" | cut -d_ -f2-;done| cut -f 1 -d '.')  # extract .bam

for x in $i
        do mkdir -p $DIR/$x
        done
done  ## close loop

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

如何在Java ..中基于特定文件的扩展名过滤文件夹(目录和子目录)?

从所有子目录复制具有特定扩展名的所有文件

如何在QtCreator中为项目创建文件夹或子目录?

使用git查看子目录中与特定文件扩展名相关的所有日志

删除所有没有特定扩展名的文件夹和子目录

R:计算不同子目录/文件夹中具有特定扩展名的文件数

如果文件扩展名与子目录扩展名匹配,则将文件移动到父目录

bash重命名文件/在文件名中添加整数,在多个子目录中具有各种扩展名

无法在/ home / user /的子目录中创建文件夹或文档

在文件夹的所有子目录中创建相同的文件夹

如何忽略一个目录及其子目录中的特定扩展名的所有文件?

如何从特定给定的目录和所有子目录中搜索具有特定扩展名的文件

删除特定文件夹的文件和子目录

如何在Mac OS X的子目录中更改文件扩展名

递归遍历所有子目录,如果存在具有特定扩展名的文件,则在该文件夹中运行一次命令

我可以浏览目录的子目录并将特定扩展名的文件复制到新文件夹吗?

清空每个子目录中的特定文件夹

bash脚本,该脚本在子目录中找到特定的文件夹并在所有目录中递归创建目录

在特定目录及其子目录中,找到所有以 .tmp 扩展名结尾的文件夹

Bash - 检查子目录是否包含文件夹

如何获取在项目文件夹中创建的最新子目录

将子目录中的文件复制到只有 1 个文件扩展名的临时文件夹

如何在多个子目录中找到相同扩展名的所有文件并使用 python 将它们移动到单独的文件夹?

如何将具有特定扩展名的文件从子目录移动到另一个目录,包括使用 CMD 创建文件所在的子目录?

在R的不同子目录中创建相同的文件夹

创建 .htaccess 重定向规则,删除从根目录而不是子目录提供的文件扩展名?

将具有特定扩展名的文件从目录移动到 PHP 中的子目录

从多个父文件夹中删除子目录

文件夹树中特定子目录上的 list.files()