스크립트에서 파일을 사용한 후 파일을 삭제하려면 안전한 명령을 사용하는 것이 좋습니다.

스크립트에서 파일을 사용한 후 파일을 삭제하려면 안전한 명령을 사용하는 것이 좋습니다.

연결 후 *.dump 파일이 크고 연결 파일과 덤프 파일을 위한 공간이 없기 때문에 해당 파일을 안전하게 삭제하는 줄을 추가하려는 이 스크립트가 있습니다. 젤리는 아래와 같이 게놈 ID의 줄별 목록이 포함된 txt 파일입니다.

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

두 개의 열이 있는 게놈 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 -nwc줄을 제거하고 줄에 echo+를 추가할 수 있습니다 read. 이는 wc단어 수를 의미 wc -l하지만 실제로는 줄 수를 의미합니다.)

관련 정보