スクリプトでファイルを使用した後にファイルを削除するには、安全なコマンドを使用することをお勧めします。

スクリプトでファイルを使用した後にファイルを削除するには、安全なコマンドを使用することをお勧めします。

接続後に* .dumpファイルが大きくなり、接続ファイルとダンプファイルのスペースがないため、そのファイルを安全に削除する行を追加したいスクリプトがあります。ゼリーは、以下に示すように、ゲノムIDの行別リストを含むtxtファイルです。

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

2つの列を持つゲノムIDの名前の付いたダンプファイル名がたくさんあります。 GCA...1.dumpは次のとおりです。

A 57575757
C 6656565..

前任者。 GCA...2.dumpは次のとおりです。

AA 6565656
AT 6565656...

したがって、各ゲノムIDには14個のダンプファイル(1〜14 ngram)があります。そのため、各ゲノムIDに対して1〜14をすべてリンクしてから、ゼリーファイルに基づいて使用されたダンプファイルを削除したいと思います。結局のところ、私が作成した新しいディレクトリに* .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 -n、実際には行数を意味します。)wcechoreadwcwc -l

おすすめ記事