从列表中查找一个目录中的文件,复制到一个新目录,并执行脚本
Find files in one directory from a list, copy to a new directory, and execute script
我有一个如下所示的文件:
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
中列出的文件
我不知道如何将这些列出的文件复制到新目录并移动脚本,以便它现在将对新目录中的文件执行脚本中的所有其他行。
我不清楚 lowfiles
和 high
是什么意思,所以我将使用 source
和 destination
。
#!/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 }' 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
在任何一种情况下,您仍然会使用顶部的第一个循环,只是不包含您的命令。
我有一个如下所示的文件:
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
我不知道如何将这些列出的文件复制到新目录并移动脚本,以便它现在将对新目录中的文件执行脚本中的所有其他行。
我不清楚 lowfiles
和 high
是什么意思,所以我将使用 source
和 destination
。
#!/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 }' 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
在任何一种情况下,您仍然会使用顶部的第一个循环,只是不包含您的命令。