AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • 主页
  • 系统&网络
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • 主页
  • 系统&网络
    • 最新
    • 热门
    • 标签
  • Ubuntu
    • 最新
    • 热门
    • 标签
  • Unix
    • 最新
    • 标签
  • DBA
    • 最新
    • 标签
  • Computer
    • 最新
    • 标签
  • Coding
    • 最新
    • 标签
主页 / unix / 问题 / 755262
Accepted
Paulo Sergio Schlogl
Paulo Sergio Schlogl
Asked: 2023-08-30 01:50:01 +0800 CST2023-08-30 01:50:01 +0800 CST 2023-08-30 01:50:01 +0800 CST

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

  • 772

我有这个脚本,我想在其中添加一行,在串联后,安全地删除 *.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,但还有更好的方法吗?

谢谢你们。

保罗

rm
  • 1 1 个回答
  • 59 Views

1 个回答

  • Voted
  1. Best Answer
    minorChaos
    2023-08-30T20:27:22+08:002023-08-30T20:27:22+08:00

    我猜原来的应该是这样运行的

     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实际上是行数。)

    • 1

相关问题

  • 缺少操作数 - rm 命令

  • 如何将 mlocate.db 与现在存在的进行比较?

  • 使用 Bash 删除文档根目录下所有 CMS 的所有缓存目录的好方法

  • 从什么时候开始 POSIX 和 GNU rm 不删除 /?

  • 另一个用户的非空子目录是否可以安全地从我的目录中删除?

Sidebar

Stats

  • 问题 205573
  • 回答 270741
  • 最佳答案 135370
  • 用户 68524
  • 热门
  • 回答
  • Marko Smith

    模块 i915 可能缺少固件 /lib/firmware/i915/*

    • 3 个回答
  • Marko Smith

    无法获取 jessie backports 存储库

    • 4 个回答
  • Marko Smith

    如何将 GPG 私钥和公钥导出到文件

    • 4 个回答
  • Marko Smith

    我们如何运行存储在变量中的命令?

    • 5 个回答
  • Marko Smith

    如何配置 systemd-resolved 和 systemd-networkd 以使用本地 DNS 服务器来解析本地域和远程 DNS 服务器来解析远程域?

    • 3 个回答
  • Marko Smith

    dist-upgrade 后 Kali Linux 中的 apt-get update 错误 [重复]

    • 2 个回答
  • Marko Smith

    如何从 systemctl 服务日志中查看最新的 x 行

    • 5 个回答
  • Marko Smith

    Nano - 跳转到文件末尾

    • 8 个回答
  • Marko Smith

    grub 错误:你需要先加载内核

    • 4 个回答
  • Marko Smith

    如何下载软件包而不是使用 apt-get 命令安装它?

    • 7 个回答
  • Martin Hope
    user12345 无法获取 jessie backports 存储库 2019-03-27 04:39:28 +0800 CST
  • Martin Hope
    Carl 为什么大多数 systemd 示例都包含 WantedBy=multi-user.target? 2019-03-15 11:49:25 +0800 CST
  • Martin Hope
    rocky 如何将 GPG 私钥和公钥导出到文件 2018-11-16 05:36:15 +0800 CST
  • Martin Hope
    Evan Carroll systemctl 状态显示:“状态:降级” 2018-06-03 18:48:17 +0800 CST
  • Martin Hope
    Tim 我们如何运行存储在变量中的命令? 2018-05-21 04:46:29 +0800 CST
  • Martin Hope
    Ankur S 为什么 /dev/null 是一个文件?为什么它的功能不作为一个简单的程序来实现? 2018-04-17 07:28:04 +0800 CST
  • Martin Hope
    user3191334 如何从 systemctl 服务日志中查看最新的 x 行 2018-02-07 00:14:16 +0800 CST
  • Martin Hope
    Marko Pacak Nano - 跳转到文件末尾 2018-02-01 01:53:03 +0800 CST
  • Martin Hope
    Kidburla 为什么真假这么大? 2018-01-26 12:14:47 +0800 CST
  • Martin Hope
    Christos Baziotis 在一个巨大的(70GB)、一行、文本文件中替换字符串 2017-12-30 06:58:33 +0800 CST

热门标签

linux bash debian shell-script text-processing ubuntu centos shell awk ssh

Explore

  • 主页
  • 问题
    • 最新
    • 热门
  • 标签
  • 帮助

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve