简体   繁体   English

如果父文件夹与bash同名,则移动子文件夹的内容

[英]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.

相关问题 如何在bash脚本中基于父文件夹和子文件夹的名称重命名文件 - How to rename file based on parent and child folder name in bash script 创建与父文件夹同名的子文件夹并将文件移动到其中 - Create subfolder with same name as parent and move files into it Bash脚本,将文件排序到具有相同名称的文件夹中 - Bash script that sorts files into folder with same name 如何在父shell和子shell之间使用相同的bash变量 - How to use the same bash variable between a parent shell and child shell 在查找和移动Bash脚本中包含相同名称的不同文件 - Including different files with the same name in a Find and Move Bash Script 将Maildir内容移到父目录 - Move Maildir contents to parent directory 将所有文件夹和文件移动到 Linux 目录中具有相同主题名称的文件夹中 - move all the folders and files to the folder with the same subject name in the directory Linux 复制/移动不同文件夹中同名的多个文件 - copy/move same name multiple files in different folder 如果目标文件夹还包含同名文件,则移动文件并重命名 - Move a file and rename it if the destination folder contains also a file with the same name 如果已有同名文件,如何将文件复制到文件夹(Linux BASH) - How to copy a file to a folder if there is already a file with the same name (Linux BASH)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM