Bash:从./将所有目录的内容移动到各自的../

时间:2022-06-02 19:52:27

I have a list of 20 directories, each one has another directory with the contents I need (i.e. the contents are needlessly one level deeper than they need to be), the names of each super-directory is different to one another and their sub-directories are the same as their parent but without "_100bp" suffix:

我有一个包含20个目录的列表,每个目录都有另一个目录,其中包含我需要的内容(即内容不必要地比它们需要的深一层),每个超级目录的名称彼此不同,并且它们的子目录与其父目录相同但没有“_100bp”后缀:

E.g. I have: directory_100bp/directory/<some content>

例如。我有:directory_100bp / directory /

E.g. I need: directory_100bp/<some content>

例如。我需要:directory_100bp /

I know I can create a bash script to iterate for each directory:

我知道我可以创建一个bash脚本来迭代每个目录:

mv ./directory/* ../

But is there a way I can do this one or two lines? I don't mind if the empty sub-directory remains after moving the contents one level up.

但有一种方法可以做到这一两行吗?我不介意在将内容向上移动一级后是否保留空子目录。

1 个解决方案

#1


1  

If your top level directory is named mydir:

如果您的*目录名为mydir:

find mydir -mindepth 1 -maxdepth 1 -type d -exec bash -c 'mv {}/* mydir; rm -ri {}' \;

This will prompt you for each directory, you can change the -i flag to -f to force deletion without prompting.

这将提示您输入每个目录,您可以将-i标志更改为-f以强制删除而不提示。

#1


1  

If your top level directory is named mydir:

如果您的*目录名为mydir:

find mydir -mindepth 1 -maxdepth 1 -type d -exec bash -c 'mv {}/* mydir; rm -ri {}' \;

This will prompt you for each directory, you can change the -i flag to -f to force deletion without prompting.

这将提示您输入每个目录,您可以将-i标志更改为-f以强制删除而不提示。