简体   繁体   中英

Merging Sub-Folders together, Linux

I have a main folder "Abc" which has about 800 sub-folders. Each of these sub-folders contains numerous files (all of the same format, say ".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.

The cp -r command copies it but leaves the files in the sub-folders, so it doesn't really help much. 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 ). 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:

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/

Edit: This would leave all the subfolders in place (but they'd be empty now)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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