LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 03-05-2013, 02:17 PM   #1
kwoweiho
LQ Newbie
 
Registered: Feb 2013
Posts: 2

Rep: Reputation: Disabled
Perl: how to access multiple directory with variables


I want to access multiple directories with the following structure and concatenate all the files from each director into one file:
/a/b/c/1
/a/b/c/2
....
/a/b/c/22

Here's my code in perl:

@chr=(1 .. 22);
$n=0;
while ($chr[$n]) {
$path = "/a/b/c/$chr[$n]";
opendir(DIR, $path) or die “Cannot open $path : $!”;
system (cat * >/a/b/c/$chr[$n].txt);
closedir(DIR);
$n++;
}

there's an error in the opendir line. and maybe more, can somebody help me out please?
Thanks!
David
 
Old 03-05-2013, 05:43 PM   #2
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,359

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
Best practice is to start code with warnings & strict turned on
Code:
#!/usr/bin/perl -w
use strict;
It would also help if you showed the error msgs you're getting.
Incidentally, you should add error handling to closedir as well.

I'd guess at least one problem is you are opening a DIR to read, but you're not changing to that dir ( chdir() ), so the cat is working in the the wrong dir.
http://perldoc.perl.org/
 
Old 03-05-2013, 07:35 PM   #3
smallpond
Senior Member
 
Registered: Feb 2011
Location: Massachusetts, USA
Distribution: Fedora
Posts: 4,140

Rep: Reputation: 1263Reputation: 1263Reputation: 1263Reputation: 1263Reputation: 1263Reputation: 1263Reputation: 1263Reputation: 1263Reputation: 1263
First time with Perl?

I think you are confusing opendir with chdir:

Code:
use warnings;
use strict;

for my $dir (1 .. 22) {
   chdir "/a/b/c/$dir";
   system("cat * >/a/b/c/$dir.txt");
}
 
Old 03-06-2013, 10:03 AM   #4
kwoweiho
LQ Newbie
 
Registered: Feb 2013
Posts: 2

Original Poster
Rep: Reputation: Disabled
Thanks guys, it works perfectly!
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
convert multiple file in directory: ascii to hex in perl fad216 Programming 3 04-10-2011 12:47 PM
Print output of a script to screen using Perl/Multiple installation of Perl Modules metallica1973 Linux - General 1 02-17-2011 05:59 PM
Multiple groups sharing directory access OnlyNewb Linux - General 2 11-19-2009 11:39 PM
Perl seems to think I have two INC variables. versaulis Linux - Software 3 11-18-2008 07:00 PM
Perl variables nbp Programming 2 11-02-2003 09:33 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 03:16 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration