How to copy a directory except all hidden files in Perl?

I have a directory hierarchy with a bunch of files. Some of the directories start with . . I want to copy the hierarchy somewhere else, leaving all the files and directories starting with .

How can I do that?

+4
source share
5 answers

I think you want File :: Copy :: Recursive rcopy_glob() :

rcopy_glob ()

This function allows you to specify a pattern suitable for perl glob () as the first argument. Subsequently, each path returned by perl glob () receives an RCOPY () IED.

It also returns an array whose elements are array refs that contain the return value of each rcopy () call.

It forces the behavior as if $ File :: Copy :: Recursive :: CPRFComp is true.

+3
source

If you solve this problem without Perl, you should check rsync . It is available on Unix-like systems, on Windows via cygwin, and possibly as a standalone tool for Windows. He will do what you need and more.

 rsync -a -v --exclude='.*' foo/ bar/ 

If you do not own all the files, use -rOlt instead of -a .

+3
source

Glob ignores point files by default.

perl -lwe'rename($_, "foo/$_") or warn "failure renaming $_: $!" for glob("*")'

0
source

The code below does the job in a simple way, but does not handle symbolic links, for example.

 #! /usr/bin/perl use warnings; use strict; use File::Basename; use File::Copy; use File::Find; use File::Spec::Functions qw/ abs2rel catfile file_name_is_absolute rel2abs /; die "Usage: $0 src dst\n" unless @ARGV == 2; my($src,$dst) = @ARGV; $dst = rel2abs $dst unless file_name_is_absolute $dst; $dst = catfile $dst, basename $src if -d $dst; sub copy_nodots { if (/^\.\z|^[^.]/) { my $image = catfile $dst, abs2rel($File::Find::name, $src); if (-d $_) { mkdir $image or die "$0: mkdir $image: $!"; } else { copy $_ => $image or die "$0: copy $File::Find::name => $image: $!\n"; } } } find \&copy_nodots => $src; 
0
source
 cp -r .??* 

almost perfect because it skips files starting at. and followed by one sign. e.g. -d or .e

 echo .[!.] .??* 

it's even better

or

 shopt -s dotglob ; cp -a * destination; shopt -u dotglob 
0
source

Source: https://habr.com/ru/post/1299198/


All Articles