Download your favorite Linux distribution at LQ ISO.
Go Back > Forums > Linux Forums > Linux - Newbie
User Name
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!


Search this Thread
Old 03-05-2013, 03:17 PM   #1
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:

Here's my code in perl:

@chr=(1 .. 22);
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);

there's an error in the opendir line. and maybe more, can somebody help me out please?
Old 03-05-2013, 06:43 PM   #2
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.6, Centos 5.10
Posts: 16,344

Rep: Reputation: 2049Reputation: 2049Reputation: 2049Reputation: 2049Reputation: 2049Reputation: 2049Reputation: 2049Reputation: 2049Reputation: 2049Reputation: 2049Reputation: 2049
Best practice is to start code with warnings & strict turned on
#!/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.
Old 03-05-2013, 08:35 PM   #3
Senior Member
Registered: Feb 2011
Location: Massachusetts, USA
Distribution: CentOS 6 (pre-systemd)
Posts: 1,815

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

I think you are confusing opendir with chdir:

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, 11:03 AM   #4
LQ Newbie
Registered: Feb 2013
Posts: 2

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


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 01:47 PM
Print output of a script to screen using Perl/Multiple installation of Perl Modules metallica1973 Linux - General 1 02-17-2011 06:59 PM
Multiple groups sharing directory access OnlyNewb Linux - General 2 11-20-2009 12:39 AM
Perl seems to think I have two INC variables. versaulis Linux - Software 3 11-18-2008 08:00 PM
Perl variables nbp Programming 2 11-02-2003 10:33 AM

All times are GMT -5. The time now is 07:30 PM.

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