#!/usr/bin/perl use warnings; use strict; use Cwd; use File::Find; my $cwd = getcwd(); open(my $FILES_FOLDERS, '+>', "files_folders.txt") or die $!; find(sub { print $FILES_FOLDERS "$File::Find::dir\n" ;#How to print unique directories and get only the directory name } , "$cwd" );