Does anyone know how to search through thousands of subdirectories for all directories that contain only 1 file and not more than 1 file?

Any suggestion on what tool to use or an easy code snippet?


In PowerShell, this is one way you could do it:

PS> Get-ChildItem -recurse | `
     Where {$_.PSIsContainer -and `
           @(Get-ChildItem $_.Fullname | Where {!$_.PSIsContainer}).Length -eq 1}

The $_.PSIsContainer returns true for dirs and false for files. The @() syntax ensures the result of the expression is an array. If its length is 1 then there's only one file in that dir. This example also makes use of a nested pipeline e.g. Get-ChildItem $_.Fullname | Where {...} within the first Where scriptblock.

  • I tried this and it works well. – Bratch Oct 14 '09 at 19:55
  • yep, that was easy. it works (minus the backtick marks) in my Powershell ISE IDE. thanks much! – djangofan Oct 15 '09 at 21:17
  • The backtick is the line continuation char and come in handy on the console host (PowerShell prompt). – user19468 Oct 16 '09 at 1:38

Here is a Perl solution (tested on Windows):


use strict;
use warnings;

use File::Find;
use File::Slurp;
use File::Spec::Functions qw(catfile canonpath rel2abs);

my ($top) = @ARGV;
die "Provide top directory\n" unless defined($top) and length $top;

find(\&wanted, $top);

sub wanted {
    my $name = $File::Find::name;
    return unless -d $name;
    return unless 1 == grep { -f catfile($name, $_) } read_dir $name;
    print canonpath(rel2abs $name), "\n";


C:\Temp> f .

If this was on Linux I would be tempted to use a command like this.

find . -type 'f' -printf '%h\n' | sort | uniq -c

The find command will print out the directory name of all the files. Which we then run through sort, and then use the -c option of uniq to give us the number of files per directory. Once you have the count per directory it should be easy enough to just grep out the directories with a value of 1.

If you would prefer to perform actions on the directories while keeping it on one line, you can pipe the results through awk to xargs. For example, to delete each folder:

find . -type 'f' -printf '%h\n' | sort | uniq -c | awk '{ if ($1 == "1") printf "%s%c",$2,0 }' | xargs -0 -I {} rm -rf {}

This prints out each directory with a value of 1 to a null terminated string, which can then be taken as arguments to xargs. You use a null terminated string so that spaces will be handled as expected. In xargs, the {} characters will be replaced by each passed argument.

  • I'm confused. Isn't the question tagged "powershell"? – Dennis Williamson Oct 14 '09 at 19:02
  • I always like seeing 'Well, under a real OS with real tools I would do xxx' answers. – MikeyB Oct 14 '09 at 19:35
  • 1
    @Dennis, yes it is, but it is also tagged perl, and it doesn't include a requirement for a specific OS. It seemed to me that the OP was looking for any available solution. I figured one that included installing GNU tools may be acceptable, or at least useful to someone else in the future. – Zoredache Oct 14 '09 at 21:37

I think you could craft something in a few lines using


Something like this.

use File::Find;
my $base_dir = '/';
  sub {
    # do stuff on each file here.
    $filename = $File::Find::name;
    $dir = $File::Find::dir;
  }, $base_dir );

EDIT: I really like Zoredache's find method better, but you did tag this as perl.

  • This answer isn't really complete because the OP wants to find only those directories which have exactly one file in them. – John Gardeniers Oct 15 '09 at 2:54

The solution with:

sub wanted {
    my $name = $File::Find::name;
    return unless -d $name;
    return unless 1 == grep { -f catfile($name, $_) } read_dir $name;
    print canonpath(rel2abs $name), "\n";

Needlessly reads each directory to count items within it, then reads it again when actually descending it (as part of the File::Find framework).

A simpler solution is just to descend, charging the presence of each file to the directory that contains it:

my %count = 0;
sub wanted {
  return unless -f;

my @one_file_dirs = sort grep { $count{$_} == 1 } keys %count;

Now if you want to do something with those folders.

$RootFolder = "c:\myfolder"
$FoldersWithOnlyOneFile = Get-ChildItem $RootFolder -Recurse | `
    Where {$_.PSIsContainer -and @( Get-ChildItem $_.Fullname | Where {!$_.PSIsContainer}).Length -eq 1 `
                            -and @( Get-ChildItem $_.Fullname | Where {$_.PSIsContainer}).Length -eq 0 }

Foreach($folder in $FoldersWithOnlyOneFile)
   Get-ChildItem $Folder.FullName

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.