- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- Perl script to delete older files
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
тАО07-16-2008 08:26 AM
тАО07-16-2008 08:26 AM
I am new to scripting. I am using the command
find /tmp/histdwn/h0* -type f -mtime +30 -exec rm {} \; to delete files older than 30 days. The command works well on command line on HPUX.
But when I use the same code in a perl script.
#!/opt/OV/contrib/perl/bin/perl
find /tmp/histdwn/h0* -type f -mtime +28 -exec rm {} \;
The error I get is:
Backslash found where operator expected at ./hist.pl line 2, near "} \"
(Missing operator before \?)
syntax error at ./hist.pl line 9, near "} \"
Execution of ./hist.pl aborted due to compilation errors.
Please correct this. Thanks!
Solved! Go to Solution.
- Tags:
- Perl
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО07-16-2008 08:44 AM
тАО07-16-2008 08:44 AM
SolutionThe easiest way is not to use PERL at all. Create your script as a regular shell script.
# cat my-script
#!/usr/bin/sh
find /tmp/histdwn/h0* -type f -mtime +28 -exec rm {} \;
Now give yourself permissions to run it and you should be fine.
- Tags:
- find
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО07-16-2008 08:44 AM
тАО07-16-2008 08:44 AM
Re: Perl script to delete older files
You can call shell commands from a perl script but not in the direct fashion as you have.
Not sure why you need to use perl to do this but Perl could provide another methods as well. unfortunately I am not a perl guru..
Need to wait until the perl gurus chime in.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО07-16-2008 08:48 AM
тАО07-16-2008 08:48 AM
Re: Perl script to delete older files
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО07-16-2008 08:57 AM
тАО07-16-2008 08:57 AM
Re: Perl script to delete older files
Your syntax is fine for a shell but for a Perl script, you should do:
# perl -MFile::Find -le 'find(sub{unlink $File::Find::name if -f $_ && -M _ >= 28},@ARGV)' /path
To match filenames containing "h0", do something like:
perl -MFile::Find -le 'find(sub{print $File::Find::name if -f $_ && -M _ >= 6000 && m{h0}},@ARGV)' /path
Regards!
...JRF...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО07-16-2008 10:41 AM
тАО07-16-2008 10:41 AM
Re: Perl script to delete older files
If you want better performance in find, replace that ";" by a +:
find ... -exec rm {} +
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО07-17-2008 04:24 AM
тАО07-17-2008 04:24 AM
Re: Perl script to delete older files
My second example, adding matching for specific filenames, neglected to change the 'print' to 'unlink' and set the number of days too high. I meant to write:
# To match filenames containing "h0", do something like:
perl -MFile::Find -le 'find(sub{unlink $File::Find::name if -f $_ && -M _ >= 28 && m{h0}},@ARGV)' /path
As you can see, the code can be tested by changing 'unlink' (the system call for 'rm') to 'print'.
Note too, that you can pass multiple '/path' arguments to the script if you want to examine multiple directories.
Regards!
...JRF...