Hellow zarath,
Something like this should do what you want.
#!/usr/bin/perl use strict; use warnings; use File::Copy; use Data::Dumper; use File::Path qw(make_path); my $dir = shift || "."; opendir(my $dh, $dir) or die "cannot open directory $dir $!"; my @files = grep { /\.txt$/ && -f "$dir/$_" } readdir($dh); closedir $dh; # 0 1 2 3 4 5 6 7 8 my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(t +ime); # $mon the month in the range 0..11 , with 0 indicating January and 11 $mon += 1; # $year contains the number of years since 1900. To get a 4-digit year + write: $year += 1900; # To get a 2-digit year write: my $short_year = substr $year, 2; my $new_dir = "$dir/$year/$mon/$mday"; eval { make_path($new_dir); 1 } or die "Can't create home directory: $@\n"; if (@files) { foreach my $file (@files) { # the perl file move function move("$dir/$file", $new_dir) or die "The move operation failed: $!"; } print Dumper \@files; } else { print "No files found to move\n"; } __DATA__ $ perl test.pl $VAR1 = [ 'counts.txt', 'file.txt', 'test.txt' ]; ~/Monks$ ll 2017/5/23/ total 16 drwxrwxr-x 2 tinyos tinyos 4096 May 23 13:29 ./ drwxrwxr-x 3 tinyos tinyos 4096 May 23 13:23 ../ -rw-rw-r-- 1 tinyos tinyos 0 May 9 15:19 counts.txt -rw-rw-r-- 1 tinyos tinyos 24 May 9 11:53 file.txt -rw-rw-r-- 1 tinyos tinyos 465 May 9 15:55 test.txt $ perl test.pl No files found to move
If you want you can set permissions, read (File::Path). I also used (readdir and File::Copy).
There are multiple ways of resolving your task, this is just one.
Update:
Adding if and else condition on greped files.Hope this helps.
In reply to Re: Wildcards for a wide array of strings?
by thanos1983
in thread Wildcards for a wide array of strings?
by zarath
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |