简体   繁体   English

从列表中查找一个目录中的文件,将其复制到新目录并执行脚本

[英]Find files in one directory from a list, copy to a new directory, and execute script

I have a file that looks like this: 我有一个看起来像这样的文件:
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

What I want to do is find the files that are listed in the first column. 我要做的是找到第一列中列出的文件。 These files are in the same directory as the file I am reading the list of files out of is. 这些文件与我正在从中读取文件列表的文件位于同一目录中。 I then want to take these found files and copy them into a new directory. 然后,我要获取这些找到的文件并将其复制到新目录中。 To the copied files in the new directory, I want to execute more commands. 对于要复制到新目录中的文件,我想执行更多命令。 I want this to all be done in the same bash script as the generation of this file is done in this script. 我希望所有这些都在同一bash脚本中完成,因为此文件的生成是在此脚本中完成的。

I honestly have very little idea on how to go about executing this. 老实说,我对如何执行此操作几乎一无所知。 I was thinking about some lines that look like 我在想一些看起来像

cat lowE | cut -d ' ' -f 1 >> lowfiles cat lowE | cut -d ' ' -f 1 >> lowfiles to call up the starting file and make a list of files in a new file cat lowE | cut -d ' ' -f 1 >> lowfiles以调用起始文件并在新文件中列出文件列表

mkdir high To make the new directory called high mkdir high将新目录命名为high

find | grep -f lowfiles find | grep -f lowfiles To find the files listed in lowfiles find | grep -f lowfiles查找lowfiles中列出的文件

I don't know how to then copy those listed files into the new directory and shift the script so that it will now execute all further lines in the script on the files that are in that new directory. 我不知道如何将列出的文件复制到新目录中并移动脚本,以使其现在在该新目录中的文件上执行脚本中的所有其他行。

It's not clear to me what lowfiles and high mean so I'll use source and destination . 我不清楚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

If you're operating on the files as a group or you want to separate your processing, instead of including your commands in the loop above either: 如果您要对文件进行分组操作,或者要分开处理,则不要在以上循环中包含命令:

Do something to all of them: 对所有人都做点事:

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

or use another loop and iterate over those files: 或使用另一个循环并遍历这些文件:

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

In either case you would still use the first loop at the top, just without including your commands in it. 无论哪种情况,您仍将在顶部使用第一个循环,只是不包含命令。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM