in Education by
in the source directory, i have some binary files named 1.bin, 2.bin, 3.bin, etc. In the dest directory, i have the same named binary files, but with different content. I need to append the contents of the dest directory to the files with the same name, in the source directory, but also be sure that the content of the destination directory will be added below the content of the source directory. This is where i have come: #!/usr/bin/perl $source_dir = "/data/source_dir"; $dest_dir = "/data/dest_dir"; opendir ($source, $source_dir); @source_files = readdir $source; foreach $each_file (@source_files){ if($each !~ /^(\.|\.\.)$/) { open $file_h , "< $source_dir/$each_file"; @contents = <$file_h>; open $dest_file, ">>$dest_dir/$each_file"; print $dest_file @contents; @contents =(); } } How can i be sure that 1.bin from dest directory is appended - merged under the content of the source directory? How then the code should look like? JavaScript questions and answers, JavaScript questions pdf, JavaScript question bank, JavaScript questions and answers pdf, mcq on JavaScript pdf, JavaScript questions and solutions, JavaScript mcq Test , Interview JavaScript questions, JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)

1 Answer

0 votes
by
One possible solution use autodie to automatically handle open/close errors File::Spec for path name handling read the source file as binary using File::Slurper open the destination file for appending as binary file using the modern 3-argument version of open() print the content to the destination file handle #!/usr/bin/perl use warnings; use strict; use autodie; use File::Spec; use File::Slurper qw(read_binary); my $source_dir = "tmp/source_dir"; my $dest_dir = "tmp/dest_dir"; opendir(my $source, $source_dir); foreach my $file (readdir $source) { unless ($file =~ /^\.\.?$/) { my $content = read_binary(File::Spec->catfile($source_dir, $file)); open(my $ofh, '>> :raw :bytes', File::Spec->catfile($dest_dir, $file)); print $ofh $content; close($ofh); } } closedir($source); exit 0; Test run: $ ls -lhtR tmp/ ... tmp/dest_dir: -rw-rw-r--. 1 stefanb stefanb 33 22. 3. 20:32 1.bin -rw-rw-r--. 1 stefanb stefanb 27 22. 3. 20:32 2.bin -rw-rw-r--. 1 stefanb stefanb 15 22. 3. 20:32 3.bin tmp/source_dir: -rw-rw-r--. 1 stefanb stefanb 11 22. 3. 20:32 1.bin -rw-rw-r--. 1 stefanb stefanb 9 22. 3. 20:32 2.bin -rw-rw-r--. 1 stefanb stefanb 5 22. 3. 20:31 3.bin $ perl dummy.pl $ ls -lhtR tmp/ ... tmp/dest_dir: -rw-rw-r--. 1 stefanb stefanb 44 22. 3. 20:34 1.bin -rw-rw-r--. 1 stefanb stefanb 36 22. 3. 20:34 2.bin -rw-rw-r--. 1 stefanb stefanb 20 22. 3. 20:34 3.bin tmp/source_dir: -rw-rw-r--. 1 stefanb stefanb 11 22. 3. 20:32 1.bin -rw-rw-r--. 1 stefanb stefanb 9 22. 3. 20:32 2.bin -rw-rw-r--. 1 stefanb stefanb 5 22. 3. 20:31 3.bin UPDATE: OP changed the requirements to only append if the destination file exists. The unless block would then look like: my $dest_file = File::Spec->catfile($dest_dir, $file); # only append if destination file exists if (-f $dest_file ) { my $source_file = File::Spec->catfile($source_dir, $file); my $content = read_binary($source_file); open(my $ofh, '>> :raw :bytes', $dest_file); print "Appending contents of ${source_file} to ${dest_file}\n"; print $ofh $content; close($ofh); }

Related questions

0 votes
    How can I find where my httpd.conf file is located? I am running an Ubuntu Linux server from the Amazon Web ... find my Apache config. Select the correct answer from above options...
asked Jan 30, 2022 in Education by JackTerrance
0 votes
    What is the best way to open a file as reading/write if it exists, or if it does not, then create it and ... to do the opening part. Select the correct answer from above options...
asked Jan 30, 2022 in Education by JackTerrance
0 votes
    This question already has answers here: Why doesnt "tail" work to truncate log files? (6 answers) Closed ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 20, 2022 in Education by JackTerrance
0 votes
    This question already has answers here: Why doesnt "tail" work to truncate log files? (6 answers) Closed ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 19, 2022 in Education by JackTerrance
0 votes
    This question already has answers here: Why doesnt "tail" work to truncate log files? (6 answers) Closed ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 19, 2022 in Education by JackTerrance
0 votes
    I use nonblocking socket to receive new connection. But the code repeatedly fails to accept(). int sockfd = ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jul 6, 2022 in Education by JackTerrance
0 votes
    For instance, given ( 1, 2, 5, 6, 7), i'd like to determine that 3 and 4 are missing? ... questions, JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jun 14, 2022 in Education by JackTerrance
0 votes
    Installed Postgres-XC $>sudo apt-get install postgres-xc then $ postgres -V postgres (PostgreSQL) 9.2. ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jun 14, 2022 in Education by JackTerrance
0 votes
    For instance, given ( 1, 2, 5, 6, 7), i'd like to determine that 3 and 4 are missing? ... questions, JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jun 5, 2022 in Education by JackTerrance
0 votes
    Closed. This question needs to be more focused. It is not currently accepting answers. Want to improve this ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 26, 2022 in Education by JackTerrance
0 votes
    fairly new to using linux on shell. I want to reduce the amount of pipes I used to extract the ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 22, 2022 in Education by JackTerrance
0 votes
    This is obviously a stupid question. I am coding in Eclipse both on Mac and Linux, but I mixed up ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 22, 2022 in Education by JackTerrance
0 votes
    Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers. Want ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 19, 2022 in Education by JackTerrance
0 votes
    Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers. Want ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 19, 2022 in Education by JackTerrance
0 votes
    Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers. Want ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 17, 2022 in Education by JackTerrance
...