grep的文件名匹配的模式,并移动到所需的文件夹 [英] grep filenames matching a pattern and move to desired folder

查看:152
本文介绍了grep的文件名匹配的模式,并移动到所需的文件夹的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有图案的.txt文件的列表。 [LIST.TXT]。在LIST.TXT的foreach行,我想找到的所有文件在此与LIST.TXT指定的模式开始的位置,然后将这些文件移动到另一个位置。
考虑一个示例情况。

I have a list of patterns in a .txt file. [list.txt]. Foreach line in list.txt, I want to find all the files at a location which begin with the specified pattern in list.txt, and then move these files to another location. Consider an example case.

〜/家/ ANA / folder_a LIST.TXT ,它看起来像这样...

at ~/home/ana/folder_a I have list.txt, which looks like this...

LIST.TXT

1abc
2def
3xyz

在这个位置即 /家庭/ ANA / folder_a / ,还有一些是在LIST.TXT模式开始的多个文件。所以,有喜欢1abc_a.txt,1abc_c.txt,1abc_f.txt,2def_g.txt,3xyz_a.txt

At this location i.e /home/ana/folder_a/, there are multiple files which are beginning with the patterns in list.txt. So, there are files like 1abc_a.txt, 1abc_c.txt, 1abc_f.txt, 2def_g.txt, 3xyz_a.txt

所以,我想实现的是:

for i in cat list.txt; do 
  ls | grep '^$i' [thats the pattern] | 
  mv [files containing the pattern] to /home/ana/folder_b/

请注意,在其他位置,即/家庭/ ANA / folder_b /我已创建目录,针对每个模式。

Please note that at the other location, i.e /home/ana/folder_b/ I have already created directories, specific for each pattern.

所以/家庭/ ANA / folder_b /包含像1ABC /,2def /,3xyz /子目录
实际上,我想从移动所有匹配模式'1ABC'的文件,'2def'和'​​3xyz'/家庭/ ANA / folder_a /到在/ home / ANA / folder_b /,这样的/ home各自的子目录/ ANA / folder_b / 1ABC将有1abc_a.txt,1abc_c.txt和1abc_f.txt; /家庭/ ANA / folder_b / 2def /将有2def_g.txt和/ home / ANA / folder_b / 3xyz /将有3xyz_a.txt

So /home/ana/folder_b/ contains subdirectories like 1abc/ , 2def/ , 3xyz/ In effect, I wish to move all the files matching pattern '1abc', '2def' and '3xyz' from /home/ana/folder_a/ to their respective sub-directories in /home/ana/folder_b/, such that /home/ana/folder_b/1abc will have 1abc_a.txt , 1abc_c.txt , and 1abc_f.txt ; /home/ana/folder_b/2def/ will have 2def_g.txt and /home/ana/folder_b/3xyz/ will have 3xyz_a.txt

推荐答案

我觉得水珠扩张,是为了去这里的方式:

I think glob expansion is the way to go here:

while read pattern; do
  mv "${pattern}"* ../folder_b/"$pattern"
done < list.txt

回声 MV 命令前启动,并删除它,当你快乐的输出。

Start with an echo in front of the mv command, and remove it when you're happy with the output.

这篇关于grep的文件名匹配的模式,并移动到所需的文件夹的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆