File Manipulation: Scripting Question

后端 未结 4 1295
旧时难觅i
旧时难觅i 2020-12-21 08:48

I have a script which connects to database and gets all records which statisfy the query. These record results are files present on a server, so now I have a text file which

4条回答
  •  长情又很酷
    2020-12-21 09:02

    In perl, the -s filetest operator is probaby what you want.

    use strict;
    use warnings;
    use File::Copy;
    
    my $folderpath = 'the_path';
    my $destination = 'path/to/destination/directory';
    open my $IN, '<', 'path/to/infile';
    my $total;
    while (<$IN>) {
        chomp;
        my $size = -s "$folderpath/$_";
        print "$_ => $size\n";
        $total += $size;
        move("$folderpath/$_", "$destination/$_") or die "Error when moving: $!";
    }
    print "Total => $total\n";
    

    Note that -s gives size in bytes not blocks like du.

    On further investigation, perl's -s is equivalent to du -b. You should probably read the man pages on your specific du to make sure that you are actually measuring what you intend to measure.

    If you really want the du values, change the assignment to $size above to:

    my ($size) = split(' ', `du "$folderpath/$_"`);
    

提交回复
热议问题