I am trying to run a Perl script that reads in filenames from stdin and calls du using a system command. When I run the du command on the commandline it works fine but when du is called in the perl script using system() I get "No such file or directory." The filename does have spaces and an apostrophe which I escape before passing it into the perl script. The perl script itself is VERY simple:
use strict; use warnings;
my $line1;
my @tmutil_args;
while($line1 = <>){
@tmutil_args = ("du", "-sh", $line1);
system(@tmutil_args);
}
I also use a shell script to escape the filename. The one line script is as follows:
s/[\' ]/\\&/g
a sample filename before and after it is run through sed is:
/Volumes/Mac Mini Backup 4TB/Backups.backupdb/John's Mac Mini/2015-06-04-132645
/Volumes/Mac\ Mini\ Backup\ 4TB/Backups.backupdb/Pete\'s\ Mac\ Mini/2015-06-04-132645
I have tried with and without escaping the spaces and apostrophe.
the output is:
du: /Volumes/Mac\ Mini\ Backup\ 4TB/Backups.backupdb/Pete\'s\ Mac\ Mini/2015-06-04-132645
: No such file or directory
one important thing, I am running this on a mac OS X 10.95 in a terminal window. Perl v5.16.2
you might ask what I am trying to do. I am trying to get the size of the individual Mac TimeMachine backups.
so can anyone tell me what I need?
The error message tells you what you need. It is trying to open a file whose name ends with
\n
. Trychomp
.But really you should be using find w/--exec or xargs, or traversing in perl with
File::Find
. This is an extremely fragile way to accomplish your goals.