How do you "explode" a folder?


Recommended Posts

My friend recovered the contents of my hard drive for me. When I got it back the recovery program had grouped the files into folders by sector or some ****. Anyway, is there a way to save me a load of time and "explode" a folder? I mean like, so it takes the contents of the folder, moves it to the directory above it, and then deletes the now-empty folder?

So like it goes:

/media/Backup drive/[whatever number]/[files]

/media/Backup drive/[files]

This would be really handy as there's hundreds of folders :D

Link to comment
https://www.neowin.net/forum/topic/598007-how-do-you-explode-a-folder/
Share on other sites

  PureLegend said:
My friend recovered the contents of my hard drive for me. When I got it back the recovery program had grouped the files into folders by sector or some ****. Anyway, is there a way to save me a load of time and "explode" a folder? I mean like, so it takes the contents of the folder, moves it to the directory above it, and then deletes the now-empty folder?

So like it goes:

/media/Backup drive/[whatever number]/[files]

/media/Backup drive/[files]

This would be really handy as there's hundreds of folders :D

you could zip them up and then extract all to single folder?

I'm not sure what you mean that the backup is sorted by sector. :unsure:

Or by "exploding" folders, either... :ermm:

Did you want files presented as in a flat file system with no subdirectory structure? If so, you could run into duplicate file name issues.

  markjensen said:
I'm not sure what you mean that the backup is sorted by sector. :unsure:

Or by "exploding" folders, either... :ermm:

Did you want files presented as in a flat file system with no subdirectory structure? If so, you could run into duplicate file name issues.

I thought exploding was a good way of describing it :p

Okay, say like I have a folder called A (it doesn't matter where A is stored or what A is contained in). Inside A there are folders called B1, B2, B3 etc...let's take B1 for an example. I want to take the contents of B1 and move them into A. Then I want to delete B1.

I want to do this to all of the folders in A.

Hope that's a better explanation :)

maybe put

cd /path/to/target
mv * ../
cd ..
rmdir target

into a little bash script that loops though all the folders? youre going to have to populate a list of folders beforehand though because if you dont, you are going to end up losing all folders after a certain letter if you arent careful. also as mark said, you are risking overwriting files with the same name with this method

  rson451 said:
maybe put

cd /path/to/target
mv * ../
cd ..
rmdir target

into a little bash script that loops though all the folders? youre going to have to populate a list of folders beforehand though because if you dont, you are going to end up losing all folders after a certain letter if you arent careful. also as mark said, you are risking overwriting files with the same name with this method

easier to just do:

cd /path/to/A
mv */* . -iv

If you just wanted the "B" level folders moved up, and not go recursive on their contents, I would have just done it through the GUI, and be done by now.

Open up the "A" folder, showing all the "B" level contents. Select all. Drag and drop to where you like.

  PureLegend said:
/media/Backup drive/[whatever number]/[files]

/media/Backup drive/[files]

i just hacked up this script just for you... :p

#!/bin/bash
## explode.sh
## by Kardona on neowin.net/forum for PureLegend
## see http://neowin.net/forum/index.php?showtopic=598007

## specify the path you want to 'explode'
PATH_TO_EXPLODE=~/test

## PLEASE BACK UP YOUR FILES BEFORE YOU RUN THIS SCRIPT
#tar -cvvzf ~/backed_up_explosion.tar.gz $PATH_TO_EXPLODE/

## all the folders in exploding folder go into variable DIRS
export DIRS=`ls -AF $PATH_TO_EXPLODE|grep \/$|sed s:/::`

## returns ls of exploding folder -- not including folders
function getlist {
	ls -AF $PATH_TO_EXPLODE|grep -v \/$
} 

for n in $DIRS
do
	cd $PATH_TO_EXPLODE/$n
	for r in `ls -A $PATH_TO_EXPLODE/$n`
	do
	## move each file, r, up one directory
	## appending folder name instead of overwrite
		echo moving $r in $n to $PATH_TO_EXPLODE/$r
		getlist | grep $r > /dev/null
		if [ $? -eq 0 ]; then
			echo "ERROR: $r already exists in $PATH_TO_EXPLODE"
			echo "instead moving $r to $PATH_TO_EXPLODE/$r_$n"
			mv $r $PATH_TO_EXPLODE/$r.$n
			break;
		fi
		mv $r $PATH_TO_EXPLODE/
	done
	echo "removing directory $n"
	rm -rf $PATH_TO_EXPLODE/$n
done

put above in file explode.sh (or whatever you want) and chmod +x

make sure you modify the PATH_TO_EXPLODE var -- set it to the folder you want to 'explode' not including the trailing slash!

you could set PATH_TO_EXPLODE to $1 in the code and then run the script like this ./explode.sh /path/to/explode

PATH_TO_EXPLODE=$1

don't forget to backup your files BEFORE you run this script. if you uncomment the line

tar -cvvzf ~/backed_up_explosion.tar.gz $PATH_TO_EXPLODE/

the script handles duplicates by naming the file NAME.FOLDER instead of NAME when a duplicate filename is found. it will backup the files in the EXPLOED folder to ~/backed_up_explosion.tar.gz for you. I tested this file before i pasted it here. i just created a folder ~/test/, created folders inside of it then touch'd some files. It worked great. I hope it works for you.

This script only goes one deep (ie /A/B). if you have /A/B/C instead of /A/B all of C's contents will be deleted by the end of the script. -- use with caution and (i can't stress this enough) BACKUP before you run this script.

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.