How do I flatten a directory structure in Unix?
Dave, how can I flatten a directory structure? I've got a directory "foo", with many more sub-directories and files within. I want to take all the files from all levels beneath foo and put them in foo itself.
I've tried this: find . *.mp3 -print0 | xargs -0 mv . but I get an error on the mv command that I can't figure out. Help!
You're definitely on the right path here. To extract all files in a subdirectory the find command is the correct program, and using the Mac-specific extension "-print0" coupled with the xargs "-0" lets you handle those annoying filenames with spaces in them.
There are two problems with what you have here, though: first off, the syntax of mv is essentially move a to b, but you're ending up supplying the arguments in the wrong order, because xargs appends the filenames to what you specify, essentially creating "mv . folder/file1 folder/file2", etc. You can see where that's going to confuse the command!
The second problem is a bit more subtle: as the program proceeds, I wouldn't bet that the '.' is going to be interpreted properly and I would strongly suggest that instead you specify the directory name of your target anyway.
Unfortunately, the xargs man page is pretty darn confusing and doesn't help much. What you want to do is use the -J flag, specify a pattern (or single character), then embed that in the subsequent command. Everwhere the pattern appears, it'll be replaced by the stream of filenames produced by the find command.
This'll make more sense with an example, so let's jump in!
$ pwd /Users/taylor/Desktop/foo $ find . -type f -name "*mp3" -print0 | \ xargs -0 -J% mv % /Users/taylor/Desktop/foo $
That will do what you seek. When you're ready to delete all the now-empty directories, don't forget you can use find again, just specify -type d to match directories but not files.
Related Mac OS X Help articles:
✔ Audacity can't find LAME library, I can't save Mp3?
Hey Dave. I read your article Audacity can't save mp3 audio files and am still puzzled because I downloaded the LAME Mp3 converter...✔ How to remove Dashboard as a "space" in Mac OS X Spaces?
I'm a big fan of the Spaces utility in Mac OS X that lets me have multiple virtual screens [see Set Up Mac...✔ Best place to buy a cheap MacBook laptop?
Hi Dave. I am looking for two gently used MacBook laptops for my teen daughters. Personal computers would greatly facilitate their studies as...✔ File too big error copying to USB flash drive on my Mac?
I'm baffled. I have a 16GB Kingston USB flash drive that I use on my Mac system and I'm trying to copy a...✔ Stealth image capture photo from webcam on my Mac?
Someone sneaks into my cubicle while I'm at lunch and takes candy out of my desk. Petty, but stupid too. I want to...
Let's stay in touch!
Sign up for my weekly AskDaveTaylor Newsletter and you'll receive even more tech and gadget help right to your inbox, along with exclusive news and industry updates. It's good stuff. I promise!
I do have a comment, now that you mention it!
Check This Out Too...
Look for Answers
All Our Categories
Apple iPad Help
Articles and Reviews
Auctions and Online Shopping
Blogs and Blogging
Building Web Site Traffic
Business and Management
Computer and Internet Basics
d) None of the Above
Google Gmail Help
Google Plus Help
Industry News and Trade Shows
iPhone and Cell Phone Help
iPod, Sony PSP and MP3 Player Help
Kindle Fire Help
Mac OS X Help
Pay Per Click (PPC) Advertising
Search Engine Optimization (SEO)
Shell Script Programming
Tech Support Video Help
The Writing Business
Twitter, LinkedIn and Social Network Help
Unix and Linux Help
Video Game Tips and Help
Windows PC Help
Find Me on Google+
ADT on G+