简体   繁体   English

合并子文件夹,Linux

[英]Merging Sub-Folders together, Linux

I have a main folder "Abc" which has about 800 sub-folders. 我有一个主文件夹“ Abc”,其中包含大约800个子文件夹。 Each of these sub-folders contains numerous files (all of the same format, say ".doc"). 这些子文件夹中的每个子文件夹都包含许多文件(所有文件都具有相同的格式,例如“ .doc”)。 How do I create one master folder with all these files (and not being distributed into subfolders). 如何使用所有这些文件创建一个主文件夹(而不分发到子文件夹中)。 I am doing this on a Windows 7 machine, using cygwin terminal. 我正在Windows 7计算机上使用cygwin终端执行此操作。

The cp -r command copies it but leaves the files in the sub-folders, so it doesn't really help much. cp -r命令复制它,但将文件保留在子文件夹中,因此它并没有太大帮助。 I'd appreciate assistance with this. 非常感谢您的协助。 Thank you! 谢谢!

Assuming there could be name collisions and multiple extensions, this will create unique names, changing directory paths to dashes (eg a/b/c.doc would become abc.doc ). 假设可能存在名称冲突和多个扩展名,这将创建唯一的名称,将目录路径更改为破折号(例如a/b/c.doc将变为abc.doc )。 Run this from within the folder you want to collapse: 在要折叠的文件夹中运行以下命令:

# if globstar is not enabled, you'll need it.
shopt -s globstar
for file in */**; do [ -f "$file" ] && mv -i "$file" "${file//\//-}"; done
# get rid of the now-empty subdirectories.
find . -type d -empty -delete

If you can guarantee unique names, this will move the files and remove the subdirectories. 如果可以保证唯一的名称,则将移动文件并删除子目录。 You can change the two . 您可以更改两个. s to the name of a folder and run it from outside said folder: s到文件夹的名称,然后从所述文件夹外部运行它:

find . -depth \( -type f -exec mv -i {} . \; \) -o \( -type d -empty -delete \)

This may not be the most elegant or efficient way to do it, but I believe it'd accomplish what you want: 这可能不是最优雅或最有效的方法,但是我相信它可以实现您想要的目标:

for file in `find abc`
do
  if [ -f $file ]
  then
    mv $file `basename $file`
  fi
done

Iterate through everything in abc, check if it's a file (not a directory) and if it is then move it from its current location (eg abc/d/example.txt) to abc/ 遍历abc中的所有内容,检查它是否是文件(不是目录),然后将其从当前位置(例如abc / d / example.txt)移动到abc /

Edit: This would leave all the subfolders in place (but they'd be empty now) 编辑:这会将所有子文件夹保留在原位(但现在它们为空)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM