jayu_rao has asked for the wisdom of the Perl Monks concerning the following question:

Hi Monks,

I am trying to move files / folders in a particular directory to a new subfolder within it using perl script.

It works for Linux but not in AIX. The same command works on command line without any errors but gives error when run via perl script. Any suggestions on what could be wrong?

$ nl move_files.pl 1 #!/usr/bin/perl -w 2 use File::Find; 3 sub my_move_log_files { 4 # Get Directory names for moving files; 5 my @log_dirs_to_move = @_; 6 my $src_dir; foreach $src_dir (@log_dirs_to_move) { my $move_cur_logs_cmd="cd $src_dir; mkdir -p $ +src_dir/logs_dir\$(date +%Y%m%d%H%M%S); mv \`/bin/ls -A | grep -v log +s_dir\` \$_"; print "$move_cur_logs_cmd \n"; system("$move_cur_logs_cmd"); } 7 return 1; 8 } 9 my @found; 10 sub findstuff { 11 my $file = $_; 12 return unless -f $file; 13 my $fullpath = $File::Find::name; 14 push @found, $fullpath; 15 } 16 my $dir_to_search = "/home/user1/test"; 17 find(\&findstuff, $dir_to_search); 18 print "Before move: "; 19 print "@found\n"; 20 my $result=my_move_log_files($dir_to_search); 21 find(\&findstuff, $dir_to_search); 22 print "After move: "; 23 print "@found\n"; 24 print "Sample Run: \n"; 25 print "----------------\n" $ perl move_files.pl Before move: /home/user1/test/a.txt /home/user1/test/aa.txt /home/user +1/test/aaa.txt /home/user1/test/aaaa.txt cd /home/user1/test; mkdir -p /home/user1/test/logs_dir$(date +%Y%m%d% +H%M%S); mv `/bin/ls -A | grep -v logs_dir` $_ Usage: mv [-I] [ -d | -e] [-i | -f] [-E{force|ignore|warn}] [--] src t +arget or: mv [-I] [-d | -e] [-i | -f] [-E{force|ignore|warn}] [--] src1 . +.. srcN directory After move: /home/user1/test/a.txt /home/user1/test/aa.txt /home/user1 +/test/aaa.txt /home/user1/test/aaaa.txt /home/user1/test/a.txt /home/ +user1/test/aa.txt /home/user1/test/aaa.txt /home/user1/test/aaaa.txt $ $ $ ls -l ../test/ total 0 -rw-r--r-- 1 user1 user1 0 May 28 02:10 a.txt -rw-r--r-- 1 user1 user1 0 May 28 02:10 aa.txt -rw-r--r-- 1 user1 user1 0 May 28 02:10 aaa.txt -rw-r--r-- 1 user1 user1 0 May 28 02:10 aaaa.txt drwxr-xr-x 2 user1 user1 256 May 28 18:47 logs_dir20150528 +184713 $ $ cd /home/user1/test; mkdir -p /home/user1/test/logs_dir$(date +%Y%m% +d%H%M%S); mv `/bin/ls -A | grep -v logs_dir` $_ $pwd /home/user1/test $ $ ls -ltr total 0 drwxr-xr-x 2 user1 user1 256 May 28 18:47 logs_dir20150528 +184713 drwxr-xr-x 2 user1 user1 256 May 28 18:53 logs_dir20150528 +185347 $ ls -l logs_dir20150528185347 total 0 -rw-r--r-- 1 user1 user1 0 May 28 02:10 a.txt -rw-r--r-- 1 user1 user1 0 May 28 02:10 aa.txt -rw-r--r-- 1 user1 user1 0 May 28 02:10 aaa.txt -rw-r--r-- 1 user1 user1 0 May 28 02:10 aaaa.txt $

Replies are listed 'Best First'.
Re: Moving files recursively to new subfolder
by Anonymous Monk on May 28, 2015 at 23:15 UTC
    You're doing a lot of shell programming... the problem is with your system call, shells can be very different accross systems (their syntax can vary), as can ls/cp/mv... and their options

    If you were to use File::Find::Rule with Path::Tiny your program gets a little simpler and more portable , like this, using the other AMs renaming idea

    #!/usr/bin/perl -- use strict; use warnings; use POSIX(); use File::Find::Rule qw/ find rule /; use Path::Tiny qw/ path /; my $dir_to_search = 'goner'; print join ' ', "Before move: ", find( file => in => $dir_to_search ,) +, "\n"; my_move_log_files($dir_to_search); print join ' ', "After move: ", find( file => in => $dir_to_search ,) +, "\n"; exit( 0 ); sub my_move_log_files { my @log_dirs_to_move = @_; for my $src_dir (@log_dirs_to_move) { my $dated = "logs_dir".POSIX::strftime('%Y%m%d%H%M%S', localt +ime ); my $final = path( $src_dir, $dated); my $temp = path( $src_dir, '..', $dated); path( $src_dir )->move( $temp ); path( $src_dir )->mkpath; path( $temp )->move( $final ); } } __END__ $ perl file-find-rule-path-tiny-move-rename-goner-assubdir.pl Before move: goner/touchme.txt After move: goner/logs_dir20150528161733/touchme.txt
      Thanks much for your reply. Unfortunately I do not have control over the machine and hence I can not install the File::Find::Rule.pm on the machines where I need to run the perl script. Any other suggestions?
Re: Moving files recursively to new subfolder
by Anonymous Monk on May 28, 2015 at 19:25 UTC
    what's wrong with simply doing:
    cp -r dir1 dir2 rm -rf dir1 mkdir dir1
      Will not rm -rf dir1 delete the contents of dir2 because it is a subfolder within dir1?