#!/usr/bin/perl -w use strict; use warnings; use File::Basename; sub remove_files_in_empty_directories($) { my ($directory) = @_; # do nothing if the directory is not empty # This is the problematic part return if glob("$directory/*"); # directory is empty, remove the .Ecurep-Lenovo* files foreach my $file ( glob("$directory/.EcuRep-Lenovo_*") ) { print $file,"\n"; } } my $list_file = shift @ARGV; die "list_file is missing - Usage: $0 list_file\n" unless $list_file; open(F,"<",$list_file) || die "can not open $list_file: $!\n"; while (my $line = ) { chomp $line; my $dir = dirname $line; remove_files_in_empty_directories($dir); } #### /ecurep/hw/7/7042/21/EC/21EC3EC/.EcuRep-Lenovo_outofscope /ecurep/hw/7/7042/21/EC/21EC41C/.EcuRep-Lenovo_outofscope /ecurep/hw/7/7042/21/EC/21EC4DC/.EcuRep-Lenovo_outofscope #### /ecurep/hw/7/7042/21/EC/21EC4DC/.EcuRep-Lenovo_outofscope #### /ecurep/hw/7/7042/21/EC/21EC3EC/.EcuRep-Lenovo_outofscope /ecurep/hw/7/7042/21/EC/21EC41C/.EcuRep-Lenovo_outofscope #### drwxr-s--- 4 root swsupt 512 Apr 08 04:53 21EC3EC drwxr-s--- 4 root swsupt 512 Apr 08 05:24 21EC41C drwxr-s--- 5 root swsupt 512 Apr 11 12:12 21EC4DC [1:root@itcaix23:]/ecurep/tmp/perl_test # ls -l /ecurep/hw/7/7042/21/EC/21EC41C total 0 -rw-r--r-- 1 root swsupt 25 Jun 24 2016 .EcuRep-Lenovo_outofscope drwxr-s--- 2 root swsupt 512 Mar 31 09:06 2017-03-31 drwxr-s--- 2 root swsupt 512 Apr 07 09:05 2017-04-07 [1:root@itcaix23:]/ecurep/tmp/perl_test # ls -l /ecurep/hw/7/7042/21/EC/21EC4DC total 0 -rw------- 1 root swsupt 29 Apr 11 12:12 .EcuRep-Lenovo_outofscope drwxr-s--- 2 root swsupt 512 Mar 28 09:48 2017-03-28 drwxr-s--- 2 root swsupt 512 Apr 04 09:49 2017-04-04 drwxr-s--- 2 root swsupt 512 Apr 11 09:48 2017-04-11 [1:root@itcaix23:]/ecurep/tmp/perl_test # ls -l /ecurep/hw/7/7042/21/EC/21EC3EC total 0 -rw-r--r-- 1 root swsupt 25 Nov 04 05:26 .EcuRep-Lenovo_outofscope drwxr-s--- 2 root swsupt 512 Mar 31 06:26 2017-03-31 drwxr-s--- 2 root swsupt 512 Apr 07 06:26 2017-04-07 #### # makes no difference return if glob "'${directory}/*'"; # with this construct it works! my @files = glob("$directory/*"); return if scalar @files; # doesn't work either - prints "hi hi if ( glob("$directory/*") ) { print "hi\n"; return; }