4

In my code :

$status = `ls -l error*`;

It shows output : ls *error No such file or directory. How can I suppress this message. I am interested in determining that the error files are generated or not. If yes, I need the list of files else ignore (without printing the message)

iDev
  • 2,163
  • 10
  • 39
  • 64

1 Answers1

9

By running it like

$status = `ls -l error* 2> /dev/null`;

and suppressing the external command's output to standard error.

If you just need the file names (and not all the other info that ls's -l switch gives you), this can be accomplished in pure Perl with a statement like

@files = glob("error*");
if (@files == 0) { 
    ... there were no files ...
} else { 
    ... do something with files ...
}

and if you do need all the other info you get from ls -l ..., applying the builtin stat function to each file name can give you the same information.

mob
  • 117,087
  • 18
  • 149
  • 283
  • Thanks @mob! It worked when I used `ls -l error* 2>/dev/null` . I had tried it with `ls -l error* 2>&1` but that didnt work. A bit confused here – iDev Aug 09 '13 at 20:11
  • 2
    `2>&1` means put the standard error stream (file descriptor 2) to standard output (file descriptor 1). `2>/dev/null` means throw the standard error stream away. – mob Aug 09 '13 at 20:13
  • Thanks! Finally understood the difference :) – iDev Aug 09 '13 at 20:24