Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Sure! Here's a Perl script that should do what you're asking for. It searches for all files in the system containing "log.problem" and replaces it with "log.fixed.forever". It also creates symlinks of those files to the `/home/liviu/test` folder. Additionally, it searches for all files larger than 500MB and creates a symlink to the `/home/liviu/test2` folder. Both folders are created if they don't already exist.
- ```perl
- #!/usr/bin/perl
- use strict;
- use warnings;
- use File::Find;
- # Create directories if they don't exist
- mkdir "/home/liviu/test" unless -d "/home/liviu/test";
- mkdir "/home/liviu/test2" unless -d "/home/liviu/test2";
- # Find all files in the system
- find(\&wanted, '/');
- sub wanted {
- # Check if file is readable and writable
- if (-r $_ && -w $_) {
- # Open file for reading and writing
- open(my $fh, '+<', $_) or die "Could not open file '$_' $!";
- my $content = do { local $/; <$fh> };
- # Check if file contains log.problem
- if ($content =~ /log\.problem/) {
- # Replace log.problem with log.fixed.forever
- $content =~ s/log\.problem/log.fixed.forever/g;
- # Write changes to file
- seek($fh, 0, 0);
- print $fh $content;
- # Create symlink to /home/liviu/test folder
- symlink($_, "/home/liviu/test/$_") or die "Could not create symlink: $!";
- print "Replaced log.problem with log.fixed.forever in $_\n";
- }
- close($fh);
- }
- # Check if file size is greater than 500MB (524288000 bytes)
- if (-s $_ > 524288000) {
- # Create symlink to /home/liviu/test2 folder
- symlink($_, "/home/liviu/test2/$_") or die "Could not create symlink: $!";
- print "Created symlink for large file $_\n";
- }
- }
- ```
- This script uses the `File::Find` module to search for all files in the system. It checks each file to see if it's readable and writable before opening it for reading and writing. If the content of the file contains `log.problem`, it replaces it with `log.fixed.forever` using a regular expression substitution. The changes are then written back to the original file.
- The script also checks each file's size using `-s`. If its size is greater than 500MB (524288000 bytes), a symbolic link is created in `/home/liviu/test2`.
- Both directories (`/home/liviu/test` and `/home/liviu/test2`) are created at the beginning of the script using `mkdir` unless they already exist.
- I hope this helps! Let me know if you have any questions or need further clarification.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement