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.
  • Posts

    • Apple iPad Mini is back to its lowest price, saving you $100 by Fiza Ali Amazon US is once again offering the Apple iPad mini at its lowest price, so you may want to check it out. The iPad mini offers an 8.3‑inch Liquid Retina display which delivers a 2266×1488 pixel resolution at 326 ppi. It further supports P3 wide colour, True Tone, an anti‑reflective coating, and achieves up to 500 nits of brightness. At its core sits the A17 Pro chip, which comprises a six‑core CPU (two performance cores and four efficiency cores), a five‑core GPU, and a 16‑core Neural Engine. Hardware‑accelerated ray tracing is supported for advanced graphics, and the Neural Engine accelerates machine learning tasks. When it comes to the device’s camera system, video calls and selfies are handled by the 12MP Ultra Wide front camera with Centre Stage, while the 12MP Wide rear camera with True Tone flash captures photos, scans documents, and records 4K video. Moreover, the iPad mini features dual microphones for calls, video capture, and audio recording, alongside landscape‑oriented stereo speakers that deliver clear, immersive sound. The iPad mini supports both Apple Pencil Pro and Apple Pencil (USB‑C). It also includes Apple Intelligence, a personal intelligence system that assists with writing, creative expression, and productivity. Finally, wireless connectivity comprises Wi‑Fi 6E (802.11ax) with simultaneous dual‑band, Bluetooth 5.3, and sub‑6 GHz 5G and Gigabit LTE. Touch ID is also integrated into the top button, enabling secure fingerprint authentication for unlocking, app sign‑in, and Apple Pay. Apple iPad mini (A17 Pro): $549 (Amazon US) - 15% off This Amazon deal is US-specific and not available in other regions unless specified. If you don't like it or want to look at more options, check out the Amazon US deals page here. Get Prime (SNAP), Prime Video, Audible Plus or Kindle / Music Unlimited. Free for 30 days. As an Amazon Associate, we earn from qualifying purchases.
    • Cool, this is certainly helpful for a stop watch function. To do similar on Windows, was thinking to use the Windows 11's widgets, but shocker, those inbox widgets are totally broken and non-functional for some stupid microsoft quality reason!
    • They spend a decent amount of time on Earth.
    • Read the book when it came out. Can't wait to watch it. 
    • Microsoft wants partners to promote Edge for Business by Usama Jawad Microsoft's partner program is an initiative that aims to provide a collaborative framework through which businesses can build solutions using Redmond technologies and sell them at scale to their own customers. It enables Microsoft to reach a wider audience while also offering a financial incentive to businesses looking to leverage cutting-edge technology. Now, the company has explicitly asked its partners to bundle Edge for Business in their security offerings. In a blog post titled "Better browser security starts with Microsoft Edge for Business—and you", Microsoft has urged solution vendors offering security implementations to include Edge for Business in their package. It is being pitched as a secure browser meant for enterprise use-cases, with the additional benefit being the AI features in tow. Microsoft has also boasted that the browser contains advanced management tools that can save IT admins time and reduce overall costs. In order to assist independent solution vendors (ISVs) in promoting Edge for Business to more customers, Microsoft has actually prepared some resources to facilitate the process. This includes a collection of videos designed to emphasize the importance of protection against browser-based attacks. It has also packaged some demos for Edge management service (EMS) and Microsoft 365 Lighthouse. In fact, there's even a pitch deck, à la Apple, that can help partners emphasize the benefits of Edge for Business, along with a comprehensive landing page for the browser. This landing page contains information about how organizations can improve their security posture to protect themselves against cyberattacks, along with a white paper on cybersecurity and a licensing matrix. Even though some may view the move as desperate, there really is no downside to it. ISVs aren't being forced to promote Edge for Business to their own customers; Microsoft is just nudging them in that direction while offering some in-house assistance. As long as Edge for Business reaches more customers, it's a win-win situation for both Microsoft and its partners.
  • Recent Achievements

    • Week One Done
      dennis Nebeker earned a badge
      Week One Done
    • One Year In
      timothytoots earned a badge
      One Year In
    • One Month Later
      CHUNWEI earned a badge
      One Month Later
    • Week One Done
      TIGOSS earned a badge
      Week One Done
    • First Post
      henryj earned a badge
      First Post
  • Popular Contributors

    1. 1
      +primortal
      466
    2. 2
      +FloatingFatMan
      194
    3. 3
      ATLien_0
      163
    4. 4
      Xenon
      78
    5. 5
      Som
      73
  • Tell a friend

    Love Neowin? Tell a friend!