PERL - Copying ONLY files from one dir to another


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting PERL - Copying ONLY files from one dir to another
# 1  
Old 02-19-2014
PERL - Copying ONLY files from one dir to another

I'm writing a Perl script which has its 1st step as to copy files from one directory to another directory. The Source directory has got files with extension, without extension, directories etc. But I want to copy ONLY files with no extension. The files with extensions and directories should not get copied to destination directory.

The piece of code i wrote is as below -

Code:
        opendir(DIR, $source_dir) or die;
            @allfiles = grep {$_ ne '.' and $_ ne '..'} readdir(DIR) or die;
            @list_files = grep { !-d } @allfiles;
            @dirs = grep {  -d } @allfiles ;
        closedir(DIR);

But the @list_files array still gets the directories into it. And @dirs array is empty. Here, i've not tried to filter the files with extensions which i wish to do in my code.

Need help.

Thank you.
# 2  
Old 02-19-2014
Do not filter the files with extensions
Code:
 #!/usr/bin/perl -w
use warnings;
my $source_dir="/home/EDISON/test";
 opendir(DIR, $source_dir) or die;
my @allfiles = grep {$_ ne '.' and $_ ne '..' } readdir(DIR) or die;
my $tmpfile;
my @list_files;
my @dirs;
for $tmpfile(@allfiles)
{
        if(!-d $source_dir."/".$tmpfile)
{
push @list_files,$tmpfile;
}else
{
push @dirs,$tmpfile;
}
}
print @list_files;
print "\n";
print @dirs;
closedir(DIR);

If filter the files with extensions
Code:
#!/usr/bin/perl -w
use warnings;
my $source_dir="/home/EDISON/test";
 opendir(DIR, $source_dir) or die;
my @allfiles = grep {$_ ne '.' and $_ ne '..' } readdir(DIR) or die;
my $tmpfile;
my @list_files;
my @dirs;
for $tmpfile(@allfiles)
{
        if(!-d $source_dir."/".$tmpfile  && $tmpfile !~ m/\.[^\.]+$/)
{
push @list_files,$tmpfile;
}else
{
push @dirs,$tmpfile;
}
}
print @list_files;
print "\n";
print @dirs;
closedir(DIR);

This User Gave Thanks to Lucas_0418 For This Post:
# 3  
Old 02-19-2014
Thank you Lucas_0418. It worked Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Finding Files with Perl on a Hidden Dir?

Greetings! Been a while since I futzed around with Perl, and came upon a minor headscratcher for the community ;) Here's the basic code which I'm trying to make tick over:#!/usr/bin/perl use strict; use warnings; use diagnostics; print " starting "; while (-e "~/.somedir/testFile")... (9 Replies)
Discussion started by: LinQ
9 Replies

2. Shell Programming and Scripting

Issue with copying files into dir inside for loop

Hi , I'm trying to move/copy the files inside the loop into a directory . I tried the below code and the issue is the data is not copying into the created directory but the files are copying into another file file_path="/home/etc" Last_Day=20130930 mkdir $file_path/ARC_${Last_Day} ... (3 Replies)
Discussion started by: smile689
3 Replies

3. Shell Programming and Scripting

Copying files to new dir structure.

I am trying to figure out a way to script copying specific files from one dir structure to another. I have a dir structure like this: dira/author 1/book 1/file a.epub /book 2/file b.epub /author 2/book 1/file c.epub /author 3/book 1/file d.epub /book 2/file... (2 Replies)
Discussion started by: arcanas
2 Replies

4. Shell Programming and Scripting

Perl-opening a file then copying files

Good morning guys!! Im still practicing with Perl and now Im trying to open a file, and copy its contents to another file. Them I want to remeove the information out of the orginal file after it is copied over. The flow should be messages-->messages1-->messages2. Kind of like a log... (1 Reply)
Discussion started by: bigben1220
1 Replies

5. Shell Programming and Scripting

Copying specific files from one dir to another

Hi Folks, I have one curious case. There are list of following files placed in one directory such as... And updated each month. files.JAN09.csv files.FEB09.csv files.MAR09.csv ..... Now, I need to move a specific files; i.e, For this month, I need to move only OCT09, NOV09, DEC09,... (1 Reply)
Discussion started by: Jerald Nathan
1 Replies

6. UNIX for Advanced & Expert Users

copying of files by userB, dir & files owned by userA

I am userB and have a dir /temp1 This dir is owned by me. How do I recursively copy files from another users's dir userA? I need to preserve the original user who created files, original group information, original create date, mod date etc. I tried cp -pr /home/userA/* . ... (2 Replies)
Discussion started by: Hangman2
2 Replies

7. Shell Programming and Scripting

PERL count files in a dir

Hi Guys, I need to count files in a dir which were updated yesterday. ls -lth | grep -i 'Jul 7' | wc -l The dir holds files of last 15 days and total count is as 2067476. Is it efficient to count the files using perl? I have developed the following perl script making use of system(). Can... (3 Replies)
Discussion started by: Asteroid
3 Replies

8. Shell Programming and Scripting

copying files to new dir

Hello, I'm new to shell scripting so I don't really understand what I'm doing wrong. The script I'm trying to do saves all the files (*.c) on the current dir to a list and, one by one, copies them to a new one called Backup. The thing is, if there are already other versions of the files I'm... (6 Replies)
Discussion started by: G3nn
6 Replies

9. Shell Programming and Scripting

copying a file from one dir to another dir

hi i have a script compareFiles() { find /tmp/Satya -type f | \ while read filename1 do echo "----------------------------------------$filename1" find /tmp/Satya -type f | \ while read filename2 do if diff $filename1 $filename2 then echo "Both files... (3 Replies)
Discussion started by: Satyak
3 Replies

10. Shell Programming and Scripting

perl - copying files

Folks, here is my code: I am a serious newbie, why doesn't this make copies of my files? I dont get any errors, the files are just not created. Your help is appreciated #!/usr/bin/perl use File::Copy; $PWD = `pwd`; $REPORTDIR = "/usr/local/tripwire/tfs/report"; chdir $REPORTDIR;... (1 Reply)
Discussion started by: BG_JrAdmin
1 Replies
Login or Register to Ask a Question