LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 08-12-2003, 11:48 AM   #1
sk8guitar
Member
 
Registered: Jul 2003
Location: DC
Distribution: mandrake 9.1
Posts: 415

Rep: Reputation: 30
perl unlink?


hey, i am trying to delete all the files in a folder and the folder itself, and when i run this:

Code:
#!/usr/bin/perl
#

chomp($day=`date +%w`);

sub delete_files
{
        my $directory=$_[0];
        #print "$directory":
        opendir DH,"$directory" or warn "can't open $directory";
        foreach $file (readdir DH)
        {
                print "deleting $file\n";
                unlink "$file" or warn "failed $file - $!\n";
        };
};


if($day==2)
{
        &delete_files(Sunday);
        rmdir "Sunday";
        mkdir "Tuesday",0755;
};
i get hte followng warning error

Code:
deleting .
failed . - Is a directory
deleting ..
failed .. - Is a directory
deleting test.txt
failed test.txt - No such file or directory
of course the first 2 are expected, but the last one? test.txt? why isn't it being deleted?
 
Old 08-12-2003, 12:36 PM   #2
david_ross
Moderator
 
Registered: Mar 2003
Location: Scotland
Distribution: Slackware, RedHat, Debian
Posts: 12,047

Rep: Reputation: 79
Try using the full path and check your permissions. You would be easier making a call to rm for this sor of job though - replace
Code:
#print "$directory":
        opendir DH,"$directory" or warn "can't open $directory";
        foreach $file (readdir DH)
        {
                print "deleting $file\n";
                unlink "$file" or warn "failed $file - $!\n";
        };
with
Code:
$out=`rm -rf $directory 2>&1`;
print $out;
 
Old 08-12-2003, 12:56 PM   #3
acid_kewpie
Moderator
 
Registered: Jun 2001
Location: UK
Distribution: Gentoo, RHEL, Fedora, Centos
Posts: 43,417

Rep: Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985
am i right in thinking you shouldn't be surrounding the unlink parameter with quotes... won't htat make the marks part of the filename or similar along those lines?
 
Old 08-12-2003, 01:25 PM   #4
sk8guitar
Member
 
Registered: Jul 2003
Location: DC
Distribution: mandrake 9.1
Posts: 415

Original Poster
Rep: Reputation: 30
dunno acid, i'm just following my oreilly book. i tried without quotes and got hte same error.

what does the

Code:
$directory 2>&1
2>&1 mean in that command?
 
Old 08-12-2003, 01:28 PM   #5
sk8guitar
Member
 
Registered: Jul 2003
Location: DC
Distribution: mandrake 9.1
Posts: 415

Original Poster
Rep: Reputation: 30
thanks david, works like a charm. i didn't realize rm had a recursive command or else i would've used that.
 
Old 08-12-2003, 01:29 PM   #6
david_ross
Moderator
 
Registered: Mar 2003
Location: Scotland
Distribution: Slackware, RedHat, Debian
Posts: 12,047

Rep: Reputation: 79
It will direct stderr to stdout so that any error messages that arise from the rm command will be shown too.
 
Old 08-12-2003, 01:29 PM   #7
david_ross
Moderator
 
Registered: Mar 2003
Location: Scotland
Distribution: Slackware, RedHat, Debian
Posts: 12,047

Rep: Reputation: 79
No problem
 
Old 08-12-2003, 06:41 PM   #8
sk8guitar
Member
 
Registered: Jul 2003
Location: DC
Distribution: mandrake 9.1
Posts: 415

Original Poster
Rep: Reputation: 30
how do you direct stdout to stderr?
 
Old 08-12-2003, 06:49 PM   #9
kev82
Senior Member
 
Registered: Apr 2003
Location: Lancaster, England
Distribution: Debian Etch, OS X 10.4
Posts: 1,263

Rep: Reputation: 51
1>&2
 
Old 08-12-2003, 06:54 PM   #10
sk8guitar
Member
 
Registered: Jul 2003
Location: DC
Distribution: mandrake 9.1
Posts: 415

Original Poster
Rep: Reputation: 30
so does 1 mean stderr and 2 mean stdout? what does the & mean?
 
Old 08-12-2003, 07:55 PM   #11
kev82
Senior Member
 
Registered: Apr 2003
Location: Lancaster, England
Distribution: Debian Etch, OS X 10.4
Posts: 1,263

Rep: Reputation: 51
0=stdin, 1=stdout, 2=stderr

i think the & tells bash you mean the file descriptor, 2>1 would redirect stderr to a file called 1. check out the redirection section of the bash man page.
 
Old 08-13-2003, 10:44 AM   #12
sk8guitar
Member
 
Registered: Jul 2003
Location: DC
Distribution: mandrake 9.1
Posts: 415

Original Poster
Rep: Reputation: 30
cool, thanks
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
when to use unlink port moonmoth Linux - Networking 1 07-20-2005 05:32 AM
RM: Cannot Unlink margolik Linux - Software 0 04-17-2004 11:03 AM
cannot unlink messages TruckStuff Linux - General 5 02-13-2003 02:04 PM
cannot unlink simon Linux - General 1 07-31-2001 07:25 PM
How to unlink a directory? _TK_ Linux - General 4 04-25-2001 08:32 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 06:32 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration