Advertisement
liviuxxx

Untitled

Mar 19th, 2023
32
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.61 KB | None | 0 0
  1. 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.
  2.  
  3. ```perl
  4. #!/usr/bin/perl
  5. use strict;
  6. use warnings;
  7. use File::Find;
  8.  
  9. # Create directories if they don't exist
  10. mkdir "/home/liviu/test" unless -d "/home/liviu/test";
  11. mkdir "/home/liviu/test2" unless -d "/home/liviu/test2";
  12.  
  13. # Find all files in the system
  14. find(\&wanted, '/');
  15.  
  16. sub wanted {
  17. # Check if file is readable and writable
  18. if (-r $_ && -w $_) {
  19. # Open file for reading and writing
  20. open(my $fh, '+<', $_) or die "Could not open file '$_' $!";
  21. my $content = do { local $/; <$fh> };
  22.  
  23. # Check if file contains log.problem
  24. if ($content =~ /log\.problem/) {
  25. # Replace log.problem with log.fixed.forever
  26. $content =~ s/log\.problem/log.fixed.forever/g;
  27.  
  28. # Write changes to file
  29. seek($fh, 0, 0);
  30. print $fh $content;
  31.  
  32. # Create symlink to /home/liviu/test folder
  33. symlink($_, "/home/liviu/test/$_") or die "Could not create symlink: $!";
  34.  
  35. print "Replaced log.problem with log.fixed.forever in $_\n";
  36. }
  37.  
  38. close($fh);
  39. }
  40.  
  41. # Check if file size is greater than 500MB (524288000 bytes)
  42. if (-s $_ > 524288000) {
  43. # Create symlink to /home/liviu/test2 folder
  44. symlink($_, "/home/liviu/test2/$_") or die "Could not create symlink: $!";
  45.  
  46. print "Created symlink for large file $_\n";
  47. }
  48. }
  49. ```
  50.  
  51. 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.
  52.  
  53. 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`.
  54.  
  55. Both directories (`/home/liviu/test` and `/home/liviu/test2`) are created at the beginning of the script using `mkdir` unless they already exist.
  56.  
  57. 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