#!/usr/bin/perl -w # # # # 2009-10-06: Written by Steven J. DeRose, sderose@acm.org. # # To do: # Option to remove all the dirs? # Option to only inlclude a certain number of ancestor dirs. # Handle collisions for real. # use strict; use Getopt::Long; my $version = "2010-09-12"; my $quiet = 0; my $verbose = 0; ############################################################################### # Getopt::Long::Configure ("ignore_case"); my $result = GetOptions( "h|help" => sub { system "perldoc flattenDirs"; exit; }, "q!" => \$quiet, "v+" => \$verbose, "version" => sub { die "Version of $version, by Steven J. DeRose.\n"; } ); ($result) || die "Bad options.\n"; ############################################################################### # Set implied options, validate option values... ############################################################################### # my $currentDir = "."; my @files = `ls -R ./*`; my $lnum = 0; my $moved = 0; for my $f (@files) { $lnum++; chomp $f; if ($f =~ m/^\s*$/) { } elsif ($f =~ m/:$/) { ($currentDir = $f) =~ s/:$//; ($verbose) && print "DIR: $currentDir\n"; } else { my $oldName = "$currentDir/$f"; if (-d "$oldName") { next; } ($verbose) && print "FILE: $currentDir/$f\n"; (my $newName = "$currentDir/$f") =~ s/\//_/g; $newName =~ s/^\._//; ($verbose) && print "NEW: $newName\n"; if (-e '$newName') { warn "File already exists: '$newName'\n"; next; } my $cmd = "mv '$currentDir/$f' '$newName'"; system "$cmd" || warn "Move failed: $cmd\n"; $moved++; } } ($quiet) || warn "Done, $lnum dirs and files processed, $moved moved.\n"; exit; ############################################################################### # sub showUsage { warn " =head1 Usage flatten Moves all descendants of the current directory, up into the current dir. Preserves their former path by joining the directory names with '_' and prefixing that to the file name. =head1 Options (prefix 'no' to negate where applicable) =over =item * B<-q> Suppress most messages. =item * B<-v> Add more messages (repeatable). =item * B<-version> Show version/license info and exit. =back =head1 Known bugs and limitations =head1 Related commands =head1 Ownership This work by Steven J. DeRose is licensed under a Creative Commons Attribution-Share Alike 3.0 Unported License. For further information on this license, see http://creativecommons.org/licenses/by-sa/3.0/. The author's present email is sderose at acm.org. For the most recent version, see http://www.derose.net/steve/utilities/. =cut "; }