建议使用安全命令在脚本使用文件后删除文件

建议使用安全命令在脚本使用文件后删除文件

我有这个脚本,我想在其中添加一行,在串联后,安全地删除 *.dump 文件,因为它们很大,而且我没有空间用于串联文件和转储文件。 jelly 是一个 txt 文件,其中包含一行一行的基因组 ID 列表,如下所示:

GCA....
GCA....and so on.

我有一堆转储文件名,以基因组 id 命名,有两列:例如。 GCA...1.dump 就像:

A 57575757
C 6656565..

前任。 GCA...2.dump 就像:

AA 6565656
AT 6565656...

所以我有每个基因组 ID 的 14 个转储文件(1-14 ngrams)。因此,我想为每个基因组 ID 连接所有 1-14,然后删除基于 jelly 文件的使用过的转储文件。最后,我只需要在创建的新目录中有一个名为 *.counts 的文件。

#! usr/bin/env bash

dir_in=$1 # Jelly_count
super=$2 # Archaea/Bacteria
group=$3 # Asgard_group/Pseudomonadota
sub=$4 # Aquificota
dir_out=Counts/"${super}"/"${group}"/"${sub}"/CHR

if [ ! -d "${dir_out}" ]; then
  mkdir -p "${dir_out}"
fi
# read the ids in jelly file
for id in $(cat "${dir_in}"/"${super}"/"${group}"/"${sub}"/jelly);
do
  echo "Concatenating files from genome ${id}"
  # make a loop from 1 to 14 or any other range I need
  for i in $(seq $5 $6);
    do
      # concatenate all 14 tsv files in one
      csvtk concat "${dir_in}"/"${super}"/"${group}"/"${sub}"/CHR/"${id}"_"${i}".dump >> "${dir_out}"/"${id}"_chr_kmer.counts
     # then delete all the 14 dump files
     # MAYBE ?????
     **find "${dir_in}"/"${super}"/"${group}"/"${sub}". -name '*.dump' -delete**
    done
    
done

我尝试过rm,但还有更好的方法吗?

谢谢你们。

保罗

答案1

据我猜测,原来的运行方式是

 myscript   Jelly_count  Archaea/Bacteria Asgard_group/Pseudomonadota Aquificota 1 14

可能首选项是cat "$ff" >> "$F" && rm "$ff"我首先设置ff为文件名的位置。

我会尝试以下方法。我希望那里没有太多/任何拼写错误。

#!/bin/bash
#This file is named   script1

set -e    # exit on most errors. (This may be safer.)

dir_in="$1"    # Jelly_count
super="$2"     # Archaea/Bacteria
group="$3"     # Asgard_group/Pseudomonadota
sub="$4"      # Aquificota

#   $5  $6     means take counts from $5 to $6

dir_out=Counts/"${super}"/"${group}"/"${sub}"/CHR

if [ ! -d "${dir_out}" ]; then
  mkdir -p "${dir_out}"
fi
# read the ids in jelly file
for id in $(cat "${dir_in}"/"${super}"/"${group}"/"${sub}"/jelly);
do
  echo "Concatenating files from genome ${id}"
  # make a loop from 1 to 14 or any other range I need
  F="${dir_out}"/"${id}"_chr_kmer.counts
  if [ -e "$F" ]; then
       echo "File $F already exists, I do not like that";
       # exit; # the safest
       echo "<ENTER> to skip this and continue; <Ctrl-C> to stop";
       read
       # # An alternative: leave the file be and `continue`
       continue
       # # An alternative:
       # mv "$F" "$F"-old-"$(date)"-$$
       # # Alternative2 : just print warning:
       # echo "File $F already exists, we will make it even bigger!"
       # # Alternative3 : delete file
       # echo "File $F already exists, I am deleting it now."
       # rm "$F"
  fi
  for i in $(seq $5 $6);
    do
      ff="${dir_in}"/"${super}"/"${group}"/"${sub}"/CHR/"${id}"_"${i}".dump
      echo -n "about to delete: "
      wc -l "$ff"  # Print number of lines
      # cat and remove
      cat "$ff" >> "$F" && rm "$ff"
    done
    echo -n "The new file   : "
    wc -l "$F"  # Print number of lines

    echo "<ENTER> to proceed with the next; <Ctrl-C> to stop";
    read

done

这将再次运行,就像

 script1   Jelly_count  Archaea/Bacteria Asgard_group/Pseudomonadota Aquificota 1 14

(稍后可以去掉echo -nwc行,行加上echo+ read。这里的wc意思是字数,wc -l实际上是行数。)

相关内容