首页 > 解决方案 > 使用特定文件复制目录

问题描述

我(非常清楚)不知道 Bash。如果这是一个多余的问题,我将不胜感激被指出正确的方向——如果我没有找到合适的线程,我深表歉意。谢谢你,一如既往。

我有一个文件结构如下:

/quants
   sample1
      a bunch of extra stuff
      aux_info
         unmapped_names.txt
   sample2
      a bunch of extra stuff
      aux_info
         unmapped_names.txt
   sample3
      a bunch of extra stuff
      aux_info
         unmapped_names.txt

在每个示例子目录中,除了 aux_info 和 unmapped_names.txt 之外,还有更多的目录和文件,但这些是我有兴趣复制的。

下面的方法在 /quants 内创建一个未映射的新目录。结果如下:

/quants
   sample1
      a bunch of extra stuff
      aux_info
         unmapped_names.txt
   sample2
      a bunch of extra stuff
      aux_info
         unmapped_names.txt
   sample3
      a bunch of extra stuff
      aux_info
         unmapped_names.txt
   unmapped
      sample1
         unmapped_names.txt
      sample2
         unmapped_names.txt
      sample3
         unmapped_names.txt

下面的代码有效,但速度极慢。我很感激有关如何更有效地做到这一点的建议。

getUnmapped(){
# =====================================================================
# description: create new dir called unmapped
# input: quant filepath (output from mapSalmon)
# output: 
# =====================================================================

# enable glob (for mac)
shopt -s extglob

# store original workingDir
local workingDir=$(pwd)
# store list of all directories (sample_rep names) in quant dir
local sample_dirs=$1/*

# cd to inputted quants dir
cd $1

# create directory in quants dir called unmappped
mkdir unmapped
cd unmapped

# create sample_rep directories in unmapped
for sample_rep in $sample_dirs;
  do
    if [ $(basename ${sample_rep%_quant}) != "unmapped" ]
      then
        local sample_file=$(basename ${sample_rep%_quant})
        mkdir $sample_file
        cp $sample_rep/aux_info/unmapped_names.txt ${1}/unmapped/${sample_file}
      fi
  done

cd $workingDir

} # end getUnmapped

标签: bash

解决方案


您正在处理多少个文件?

您可以做的一件事是预先计算并存储表达式:

$(basename ${sample_rep%_quant})

像这样:

sample_file=$(basename ${sample_rep%_quant})

然后在您的代码中将表达式替换为 $sample_file。这将使您免于对表达式进行两次评估。但是我不认为这就是它运行缓慢的原因,因为性能可能受到 Mac 的文件系统 I/O 的限制。

对于 800MB 的大文件,复制速度会很慢。在这种情况下,使用 'ln -s ...' 进行符号链接会更快。


推荐阅读