【问题标题】:Find files in one directory from a list, copy to a new directory, and execute script从列表中查找一个目录中的文件,复制到新目录,然后执行脚本
【发布时间】:2019-07-26 19:16:17
【问题描述】:

我有一个如下所示的文件:
file Gibbs kcal rel SS6.out -1752.138493 -1099484.425742 2.270331 S5.out -1752.138532 -1099484.450215 2.245858 SS3.out -1752.140319 -1099485.571575 1.124498 SS4.out -1752.140564 -1099485.725315 0.970758 SS1.out -1752.141887 -1099486.555511 0.140562 SS2.out -1752.142111 -1099486.696073 0.000000

我想要做的是找到第一列中列出的文件。这些文件与我正在从中读取文件列表的文件位于同一目录中。然后我想把这些找到的文件复制到一个新目录中。对于新目录中的复制文件,我想执行更多命令。我希望这一切都在同一个 bash 脚本中完成,因为这个文件的生成是在这个脚本中完成的。

老实说,我对如何执行此操作一无所知。我在想一些看起来像的线条

cat lowE | cut -d ' ' -f 1 >> lowfiles 调用起始文件并在新文件中列出文件

mkdir high将新目录设为high

find | grep -f lowfiles查找lowfiles中列出的文件

我不知道如何将这些列出的文件复制到新目录中并移动脚本,以便它现在将在该新目录中的文件上执行脚本中的所有其他行。

【问题讨论】:

    标签: bash file copy mkdir


    【解决方案1】:

    我不清楚lowfileshigh 是什么意思,所以我将使用sourcedestination

    #!/bin/bash
    # I'm piping a command into a while loop here since I don't care
    # that it's creating a subshell. If you need to access variables
    # used inside the loop, or other context, then you should use a
    # while loop with a process substitution redirected to the done
    # statement instead.
    
    # I'm assuming the filenames don't contain any white space characters
    
    dest=destination
    mkdir "$dest"
    
    awk '{print $1}' lowE | while read -r filename
    do
        cp "$filename" "$dest"
        # each of your commands will go here if you are operating on files one at a time, e.g.
        do_the_thing_to "$filename"
    done
    

    如果您将文件作为一个组进行操作,或者您想分开处理,而不是将您的命令包含在上面的循环中:

    对他们所有人做点什么:

    do_something "$dest"/*
    another_thing "$dest"/*
    

    或使用另一个循环并遍历这些文件:

    for filename in "$dest"/*
    do
        one_thing "$filename"
        another_thing "$filename"
    done
    

    在任何一种情况下,您仍然会使用顶部的第一个循环,只是不包含您的命令。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-10-15
      • 2015-07-19
      • 1970-01-01
      • 2016-04-29
      • 2016-02-20
      • 2016-06-14
      • 2017-11-18
      • 1970-01-01
      相关资源
      最近更新 更多