How to recursively move all folders one directory up back one directory

15

2

I have folders in scenery/terrain that contain folders called terrain and objects. These folders have folders with data files. How do I move all of the contents scenery/terrain/[name]/terrain to scenery/terrain, and the contents of scenery/terrain/[name]/objects to scenery/objects?

Andrew D.

Posted 2012-10-02T21:04:14.747

Reputation: 153

Answers

19

There is no recursive option for the mv command, so if there are directories inside the directories you want to move, you'll have to use cp. How about this one:

cp -R scenery/terrain/*/terrain/* scenery/terrain/
cp -R scenery/terrain/*/objects/* scenery/objects/

Then when you're sure that worked:

rm -rf scenery/terrain/*/terrain
rm -rf scenery/terrain/*/objects

Ryan

Posted 2012-10-02T21:04:14.747

Reputation: 3 179

Good point, but I think under Mac OS it will be cp -R (upper case R). – QSQ – 2012-10-02T21:51:28.540

@QSQ Thanks for the correction. Fixed it now. – Ryan – 2012-10-02T21:55:58.747

-r worked fine. – Andrew D. – 2012-10-02T23:17:13.423

1

mv scenery/terrain/*/terrain/* scenery/terrain/
mv scenery/terrain/*/objects/* scenery/objects/

QSQ

Posted 2012-10-02T21:04:14.747

Reputation: 450

Try it on a copy of the folder in case I misunderstood something :-) – QSQ – 2012-10-02T21:14:55.460

Too late now.. Fail on my part. I can always just re-torrent the 13 GB of scenery. – Andrew D. – 2012-10-02T21:15:48.273

Didn't work. Says that "directory is not empty" – Andrew D. – 2012-10-02T21:16:57.460

Hmm odd. I had tried it here with building a tree like that and putting files into scenery/terrain/a/terrain/ scenery/terrain/b/terrain/ and scenery/terrain/a/objects/ and scenery/terrain/b/objects/ – QSQ – 2012-10-02T21:24:21.380

Maybe I can try to move the current /terrain dir somewhere else to prevent file conflicts? – Andrew D. – 2012-10-02T21:28:32.047

Are there directories inside scenery/terrain/*/objects/ or terrain/? It would complain if the directory is not empty if it contained a directory. Try replacing mv with cp -r. You can delete the originals later. – Ryan – 2012-10-02T21:37:13.163

Ok. Will try... It does contain dirs. – Andrew D. – 2012-10-02T22:06:59.753

A bit more explanation of the answer would be useful here. Explaining why it should work might help @AndrewD. work out why it didn't in his case. – ChrisF – 2012-10-02T22:14:45.853

What did work was cp -r, because the directories contained directories, meaning mv didn't like it. cp -r did. – Andrew D. – 2012-10-02T23:10:07.050