![스크립트에서 파일을 사용한 후 파일을 삭제하려면 안전한 명령을 사용하는 것이 좋습니다.](https://linux55.com/image/223988/%EC%8A%A4%ED%81%AC%EB%A6%BD%ED%8A%B8%EC%97%90%EC%84%9C%20%ED%8C%8C%EC%9D%BC%EC%9D%84%20%EC%82%AC%EC%9A%A9%ED%95%9C%20%ED%9B%84%20%ED%8C%8C%EC%9D%BC%EC%9D%84%20%EC%82%AD%EC%A0%9C%ED%95%98%EB%A0%A4%EB%A9%B4%20%EC%95%88%EC%A0%84%ED%95%9C%20%EB%AA%85%EB%A0%B9%EC%9D%84%20%EC%82%AC%EC%9A%A9%ED%95%98%EB%8A%94%20%EA%B2%83%EC%9D%B4%20%EC%A2%8B%EC%8A%B5%EB%8B%88%EB%8B%A4..png)
연결 후 *.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 -n
및 wc
줄을 제거하고 줄에 echo
+를 추가할 수 있습니다 read
. 이는 wc
단어 수를 의미 wc -l
하지만 실제로는 줄 수를 의미합니다.)