- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - Linux
- >
- Perl Script filter & copy file
Operating System - Linux
1820389
Members
3544
Online
109623
Solutions
Forums
Categories
Company
Local Language
back
Forums
Discussions
Forums
- Data Protection and Retention
- Entry Storage Systems
- Legacy
- Midrange and Enterprise Storage
- Storage Networking
- HPE Nimble Storage
Discussions
Discussions
Discussions
Discussions
Forums
Discussions
back
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
- BladeSystem Infrastructure and Application Solutions
- Appliance Servers
- Alpha Servers
- BackOffice Products
- Internet Products
- HPE 9000 and HPE e3000 Servers
- Networking
- Netservers
- Secure OS Software for Linux
- Server Management (Insight Manager 7)
- Windows Server 2003
- Operating System - Tru64 Unix
- ProLiant Deployment and Provisioning
- Linux-Based Community / Regional
- Microsoft System Center Integration
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Blogs
Information
Community
Resources
Community Language
Language
Forums
Blogs
Go to solution
Topic Options
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-04-2006 06:53 PM
07-04-2006 06:53 PM
Hi,
I need to write a Perl script that takes a directory name as its argument, creates a new sub-directory called executables within it, then scans the directory and all its sub-directories looking for executables and moves them into the new sub-directory.
I tried the below so far it create folder but could not filter & copy...
use File::Copy;
print "Enter the directory name in which you need to check for files\n";
chomp(my $dir =);
chdir $dir or die "Can't chdir to '$dir': $!";
opendir $dir,"." or die "Can't opendir '$dir': $!";
foreach (readdir $dir)
{
next if $_ =~ /^\./;
if($_ != 'executable')
{
mkdir "executable", 0755 or warn "Cannot make executable directory: $!";
if (-x $_){
copy("$_", "executable") or die "Can't copy '$_' to 'executable': $!\n" and print "copied $_ into executable file\n";}
}
if($_ != 'graphics')
{
mkdir "graphics" or warn "Cannot make graphics directory: $!";
if($_ == "*.jpg")
{
copy("$_", "graphics") or die "Can't copy '$_' to 'graphics': $!\n" and print "copied $_ into graphics file\n";
}
}
}
closedir $dir;
I need to write a Perl script that takes a directory name as its argument, creates a new sub-directory called executables within it, then scans the directory and all its sub-directories looking for executables and moves them into the new sub-directory.
I tried the below so far it create folder but could not filter & copy...
use File::Copy;
print "Enter the directory name in which you need to check for files\n";
chomp(my $dir =
chdir $dir or die "Can't chdir to '$dir': $!";
opendir $dir,"." or die "Can't opendir '$dir': $!";
foreach (readdir $dir)
{
next if $_ =~ /^\./;
if($_ != 'executable')
{
mkdir "executable", 0755 or warn "Cannot make executable directory: $!";
if (-x $_){
copy("$_", "executable") or die "Can't copy '$_' to 'executable': $!\n" and print "copied $_ into executable file\n";}
}
if($_ != 'graphics')
{
mkdir "graphics" or warn "Cannot make graphics directory: $!";
if($_ == "*.jpg")
{
copy("$_", "graphics") or die "Can't copy '$_' to 'graphics': $!\n" and print "copied $_ into graphics file\n";
}
}
}
closedir $dir;
Solved! Go to Solution.
- Tags:
- Perl
1 REPLY 1
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-04-2006 07:06 PM
07-04-2006 07:06 PM
Solution
1. Use File::Find ?
2. Use ne/eq instead of ==/!= if comparing strings
3. Use patterm matches instead of !+ "*.jpg"
This recurses into subfolders with File::Find, and fixes all your errors. Take from it what you need
--8<---
use strict;
use warnings;
use File::Find;
use File::Copy;
print "Enter the directory name in which you need to check for files\n";
chomp (my $dir =);
chdir $dir or die "Can't chdir to '$dir': $!\n";
find (sub {
m/^\./ and return;
if ($_ ne 'executable') { # Here was your error. != is numeric comparison
mkdir "executable", 0755 or warn "Cannot make executable directory: $!";
if (-x $_) {
# Specify full target here, and do not double-quote $_: n ot needed
copy ($_, "executable/$_") or die "Can't copy '$_' to 'executable': $!\n";
print "copied $_ into executable file\n";
}
}
if ($_ ne "graphics") { # != vs ne again
mkdir "graphics" or warn "Cannot make graphics directory: $!";
if (m/\.jpg$/) { # $_ == "*.jpg" is unexplainable wrong. Not numeric, and no patter match
copy ($_, "graphics/$_") or die "Can't copy '$_' to 'graphics': $!\n";
print "copied $_ into graphics file\n";
}
}
}, $dir);
-->8---
Enjoy, Have FUN! H.Merijn
2. Use ne/eq instead of ==/!= if comparing strings
3. Use patterm matches instead of !+ "*.jpg"
This recurses into subfolders with File::Find, and fixes all your errors. Take from it what you need
--8<---
use strict;
use warnings;
use File::Find;
use File::Copy;
print "Enter the directory name in which you need to check for files\n";
chomp (my $dir =
chdir $dir or die "Can't chdir to '$dir': $!\n";
find (sub {
m/^\./ and return;
if ($_ ne 'executable') { # Here was your error. != is numeric comparison
mkdir "executable", 0755 or warn "Cannot make executable directory: $!";
if (-x $_) {
# Specify full target here, and do not double-quote $_: n ot needed
copy ($_, "executable/$_") or die "Can't copy '$_' to 'executable': $!\n";
print "copied $_ into executable file\n";
}
}
if ($_ ne "graphics") { # != vs ne again
mkdir "graphics" or warn "Cannot make graphics directory: $!";
if (m/\.jpg$/) { # $_ == "*.jpg" is unexplainable wrong. Not numeric, and no patter match
copy ($_, "graphics/$_") or die "Can't copy '$_' to 'graphics': $!\n";
print "copied $_ into graphics file\n";
}
}
}, $dir);
-->8---
Enjoy, Have FUN! H.Merijn
Enjoy, Have FUN! H.Merijn
The opinions expressed above are the personal opinions of the authors, not of Hewlett Packard Enterprise. By using this site, you accept the Terms of Use and Rules of Participation.
Company
Learn About
News and Events
Support
© Copyright 2025 Hewlett Packard Enterprise Development LP