- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - Linux
- >
- Re: extract similar kind of files through perl scr...
Categories
Company
Local Language
Forums
Discussions
Forums
- Data Protection and Retention
- Entry Storage Systems
- Legacy
- Midrange and Enterprise Storage
- Storage Networking
- HPE Nimble Storage
Discussions
Discussions
Discussions
Forums
Forums
Discussions
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
- BladeSystem Infrastructure and Application Solutions
- Appliance Servers
- Alpha Servers
- BackOffice Products
- Internet Products
- HPE 9000 and HPE e3000 Servers
- Networking
- Netservers
- Secure OS Software for Linux
- Server Management (Insight Manager 7)
- Windows Server 2003
- Operating System - Tru64 Unix
- ProLiant Deployment and Provisioning
- Linux-Based Community / Regional
- Microsoft System Center Integration
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Community
Resources
Forums
Blogs
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО07-04-2006 11:48 PM
тАО07-04-2006 11:48 PM
extract similar kind of files through perl script
I need extract files and put them in respective folders depending on a config file.
That is...the config file is as below
grap=*.jpg,*.gif,*.bmp
backup=*.~*,*.tmp,*.bak
Source=*.pl,*.php,*.tcl
depending on this the script should scan all the files & subdirectories in an user given directory and create a directory in the name specified in the config file i.e created dir name is as before(leftside) of = in config file and the files scaned and that should be put in that directory is those files with the extension after = .
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО07-05-2006 12:01 AM
тАО07-05-2006 12:01 AM
Re: extract similar kind of files through perl script
#!/opt/perl/bin/perl
use strict;
use warnings;
use Cwd;
use File::Find;
use File::Copy;
my $config_file = shift;
-f $config_file && -s _ && -r _ or die "$config_file: not a (valid/readable) config file\n";
my $pwd = getcwd;
my %config;
{ @ARGV = ($config_file);
while (<>) {
m/^[;#]/ and next; # Comment
my ($dir, $pat) = m/^\s*(\S+)\s*=\s*(.*)/ or next;
$config{$dir} = [ split m/\s*[,;]\s*/, $pat ];
}
}
foreach my $dir (keys %config) {
-d $dir or mkdir $dir, 0777;
}
find {sub {
(my $tld = $File::Find::dir) =~ s{^(?:\./)?([^/]+)(?:/.*}{};
exists $config{$tld} and return;
foreach my $dir (keys %config) {
foreach my $file (map { glob $_ } @{$config{$dir}}) {
copy $file, "$pwd/$dir";
}
}
}, ".");
-->8---
Enjoy, Have FUN! H.Merijn
- Tags:
- Perl
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО07-05-2006 12:01 AM
тАО07-05-2006 12:01 AM
Re: extract similar kind of files through perl script
find . -depth \( -name "*.jpg" -o -name "*.gif" -o -name "*.bmp" \) -exec ll -d {} \; > grap.txt
[ -f grap.txt ] && mkdir grap
cat grap.txt | while read line
do
mv ${line} grap/.
done
Same for other types of files.
- Tags:
- find
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО07-05-2006 12:04 AM
тАО07-05-2006 12:04 AM
Re: extract similar kind of files through perl script
(if you wait a little you probably get a rather thorough one from procura),
just a few hints.
To recursively search directory trees import the Find::File module.
It merely reuires the definition of a small callback sub (mostly an anonymous one even suffices).
On the other hand you can use C-shell like filename globbing from Perl which the glob() function or the <> brackets provide
(though its use is generally deprecated).
Then there are opendir(), readdir(), and closedir().
To create new directories you could use the mkdir() function, and unlink() deletes files.
All are nicely documented in your installation's POD.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО07-05-2006 12:06 AM
тАО07-05-2006 12:06 AM
Re: extract similar kind of files through perl script
procura already submitted while I was still searching the keys on my keyboard.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО07-05-2006 06:22 PM
тАО07-05-2006 06:22 PM
Re: extract similar kind of files through perl script
The code is not working I am getting the following error
"Use of uninitialized value in -f at testfti line 14.
Use of uninitialized value in concatenation (.) or string at testfti line 14.
: not a (valid/readable) config file"
& the folder "graph" is not created.once that
folder is created all the *.jpg,*.gif,*.bmp,etc files should get copied into that folder.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО07-05-2006 06:44 PM
тАО07-05-2006 06:44 PM
Re: extract similar kind of files through perl script
--8<--- untested braindump follows ...
So it was just a reflection of my thoughts.
It's up to you to complete it.
I cannot test if I don't have a config file.
My bet is that the config file looks different that your specs
I was quite defensive in my parsing, but if you do not have separators between your patterns, I cannot tell what will happen.
You could start with attaching the config file, so I could suggest some changes.
I was more or less hoping that my framework (in fact almost complete working code) would get you on the rails.
Enjoy, Have FUN! H.Merijn
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО07-05-2006 07:21 PM
тАО07-05-2006 07:21 PM
Re: extract similar kind of files through perl script
2.It should scan all the file's extensions in that User directory.
3.It should match those extensions with the extensions specified in the config file.
4.If match found it should create a directory in the User given directory path,with name specified for that extension in the config file and put that file in the corresponding newly created directory.
The config file contains like below
.jpg,*.gif,*.bmp,etc.. -> graphical type files
*.~*,*.tmp,*.bak,etc.. -> backup type files
*.pl,*.php,*.tcl,etc.. -> source type files
in the user entered directory.
like this ...the extensions might be added up in future for each grap,backup and source.