Submit Hint Search The Forums LinksStatsPollsHeadlinesRSS
14,000 hints and counting!

Click here to return to the 'Flatten a directory structure ' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
Flatten a directory structure
Authored by: leichter on Mar 18, '13 03:51:36AM
While I've sometimes used one-liners such as those being described here, I've all too often regretted it: A minor typo and files end up in the wrong place or with almost but not quite the intended names. And then you're left with coming up with a new command to fix things up. (Even worse, your one-liner fails part way through, and you now have two different problems to solve: The original one for the remaining files, and a new one for the mis-directed files.)

Two approaches make this safer:
1. Don't get fancy with one-liners. Use find to generate a list of input files; then convert it into a list of shell commands to carry out the necessary actions. Finally, when you've looked everything over and are satisfied with it, feed the commands to a shell ($ sh <command-file).

You can build the list of commands using whatever tools are comfortable and appropriate: sed, cut/paste, awk, tr (handy for changing upper to lower case, or '/' to '_'), emacs, vi. Sometimes a mix of tools is best - e.g., sed to do most of the work, emacs to fix up a few special cases.

2. If you do use a one-liner, check it first. Easy approach: Put echo in front of the command that actually renames or moves files or in any other way changes stuff. That way, nothing is actually done but you can see the commands that would have been executed. All too often, you'll find yourself feeling relief for not having "let it rip" without testing.
-- Jerry

Edited on Mar 18, '13 03:55:08AM by leichter

[ Reply to This | # ]