[英]Move contents of a child folder if parent has the same name with bash
When doing tar extract operations sometimes the contents extract directly to the parent folder which is awful since they get all disordered. 进行tar提取操作时,有时内容会直接提取到父文件夹,这很糟糕,因为它们会变得无序。 By example:
例如:
tar -xzf foo1.tar.gz
Extracts: 提取物:
./file1
./file2
./file3
The solution to that is to create an specific folder for the tar.gz By example: 解决方案是为tar.gz创建一个特定的文件夹,例如:
tar -C foo -xzf foo1.tar.gz
Extracts: 提取物:
./foo1/file1
./foo1/file2
./foo1/file3
So if I have many .tar.gz to extract I will just do: 因此,如果要提取许多.tar.gz,我将这样做:
find -name \*.tar.gz | xargs -n 1 basename -s .tar.gz | xargs -I {} tar -C {} -xzf {}.tar.gz
To extract them safely, but with that I'll endup with things like this: 为了安全地提取它们,但最终我将得到如下结果:
./foo3/foo3/file1
./foo3/foo3/file2
./foo3/foo3/file3
Is there an automatic way to remove the duplicated child folder with bash for those cases that need it? 对于那些需要使用bash的情况,是否有一种自动的方法可以使用bash删除重复的子文件夹?
计算目录中条目的数量(提示: dotglob
, (*)
和${#var[@]}
),如果单个目录条目具有相同的名称,则复制属性并移动内容(请注意,在该目录中也有相同的名称),然后删除现在为空的目录。
This is a possible solution 这是一个可能的解决方案
To move files from the child to the toplevel directory: 要将文件从子目录移动到顶层目录:
find ! -name . -maxdepth 1 -type d | xargs -I {} sh -c "find {}/{} -maxdepth 1 | xargs -I [] echo \"mv [] {}\""
This works because if the child folder doesnt exists find would do nothing: 这行得通,因为如果子文件夹不存在,则find不会执行任何操作:
find foo4/foo4
find: 'foo4/foo4': No such file or directory
To remove reapted child directory 删除重新配置的子目录
find ! -name . -maxdepth 1 -type d | xargs -I {} find {}/{} -type d -maxdepth 1 | xargs rmdir
I think it did what it was intended for. 我认为它确实达到了预期的目的。 Disadvantage:
坏处:
It shows many harmless errors. 它显示了许多无害的错误。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.