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 $

In reply to Moving files recursively to new subfolder by jayu_rao

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.