将具有相同名称的文件夹移动到新目录Linux,Ubuntu

时间:2014-11-13 11:32:58

标签: linux ubuntu grep

我有一个包含100,000个子文件夹的文件夹。 由于大小我无法打开文件夹。 现在我正在寻找一个shell脚本来帮助我移动或拆分文件夹。

Current =文件夹研究:包含100,000个子文件夹。 (A,B,C,D分类)

Needed = New folder所有以name A-science开头的文件夹。应该转移到新的 文件夹AScience。 所有以B-Science ..开头的文件夹都应移至新文件夹BScience

我在下面找到了这个脚本。但不知道如何使其发挥作用。

find /home/spenx/src -name "a1a2*txt" | xargs -n 1 dirname | xargs -I list mv list /home/spenx/dst/
find ~ -type d -name "*99966*" -print

1 个答案:

答案 0 :(得分:0)

我看了一下你提供的命令,看看它做了什么。这是每个命令的作用(如果我错了,请纠正我)

| = pipes output of command to the left of pipe to the input of the command on the right
find /home/spenx/src -name "a1a2*txt" = finds all files within given directory that match between "" and pipes output 
xargs -n 1 dirname = takes in all the piped files outputted by the find command and gets the directory name of each file and pipes to output
xargs - I list mv list /home/spenx/dst = takes in all piped folders and and puts them into list variable, mv all items in list to the given folder
find ~ -type d -name "**" -print = runs a test for all files within given name to see if they exist and print any found out (this line is only a test command, it's not necessary for the actual move)


/home/spenx/src = folder to look in (absolute file path, or just folder name without '/')
/home/spenx/dst = folder to move all files to (absolute file path, or just folder name without '/')
"a1a2*txt" = files to look for (since you only care about folders, just use *.* to catch all files
"*99966* = files to test for but I'm not sure what you would put here

我看了一下这个命令并决定对它进行一些修改,但它仍然不会将每个文件夹类别(即A-science,B-science)移动到一个单独的目录中,这只会得到所有给定目录中的文件夹并将它们移动到给定目标,或者至少据我所知。 您可能想尝试查找每个类别的所有文件夹(A-Science)并将它们逐个移动到Ascience的目标文件夹中

find /home/spenx/src -name "A-science/*.*" | xargs -n 1 dirname | sort -u | xargs - I list mv list /home/spenx/dst/Ascience

find /home/spenx/src -name "B-science/*.*" | xargs -n 1 dirname | sort -u | xargs - I list mv list /home/spenx/dst/Bscience

再次,在将命令用于实际文件之前测试该命令。

您可能需要查看this question,具体来说:

LIST.TXT

1abc
2def
3xyz

要运行的脚本:

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