- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - Linux
- >
- Re: check to see if multiple files exist
Categories
Company
Local Language
Forums
Discussions
Forums
- Data Protection and Retention
- Entry Storage Systems
- Legacy
- Midrange and Enterprise Storage
- Storage Networking
- HPE Nimble Storage
Discussions
Discussions
Discussions
Forums
Forums
Discussions
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
Discussion Boards
Discussion Boards
Discussion Boards
Community
Resources
Forums
Blogs
- 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
тАО08-11-2006 07:16 AM
тАО08-11-2006 07:16 AM
check to see if multiple files exist
I am doing the following:
my @arr = `ls \directory\file*`;
my $arr_cnt = $#arr +1;
if ($arr_cnt > 0)
{
foreach (@arr)
{
unlink($_);
}
}
This works fine if there are files in the directory but retuns an error message that no file exist in that directory if that directory does not have files of the specified pattern.
I know that I cannot check if multiple files exist with the file test.
Can anyone help.
Thanks,
Rahul
- Tags:
- Perl
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО08-11-2006 07:29 AM
тАО08-11-2006 07:29 AM
Re: check to see if multiple files exist
if (scalar(@arr) > 1) { do something }
that should tell you if you have multiple items in your array.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО08-11-2006 07:30 AM
тАО08-11-2006 07:30 AM
Re: check to see if multiple files exist
my @arr = `ls \directory\file*`;
becomes:
my @arr = `ls \directory\file* 2>/dev/null`;
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО08-11-2006 07:31 AM
тАО08-11-2006 07:31 AM
Re: check to see if multiple files exist
#!/usr/bin/perl
my @arr = glob "/path/file*";
unlink for (@arr);
Regards!
...JRF...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО08-11-2006 07:38 AM
тАО08-11-2006 07:38 AM
Re: check to see if multiple files exist
The 'glob' is a Perl built-in, so it's faster than invoking an external 'ls'.
That aside, there is no need to test for the number of array elements --- the 'foreach' (or shorter 'for') iterates over the array (or list) until there are no more elements. If there are none to begin with, the loop terminates immediately.
Regards!
...JRF...
- Tags:
- glob
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО08-11-2006 09:54 AM
тАО08-11-2006 09:54 AM
Re: check to see if multiple files exist
Actually, we can eliminate iterating over the list; pass it in it's entirely to 'unlink'; count the number of files removed; and print that summary in one short piece of code. For example:
# perl -e '$n=unlink glob("/tmp/*.log");printf "%d file%s unlinked\n", $n, $n==1 ? "":"s"'
Regards!
...JRF...
- Tags:
- unlink