liviuxxx

Untitled

Mar 19th, 2023
146
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.31 KB | None | 0 0
  1. Sure! Here's a Perl script that should do what you're asking for:
  2.  
  3. ```perl
  4. #!/usr/bin/perl
  5. use strict;
  6. use warnings;
  7. use File::Find;
  8. use File::Path qw(make_path);
  9.  
  10. my $dir = '/';
  11. my $test_dir = '/home/liviu/test';
  12. my $test2_dir = '/home/liviu/test2';
  13.  
  14. # Create directories if they don't exist
  15. make_path($test_dir) unless -d $test_dir;
  16. make_path($test2_dir) unless -d $test2_dir;
  17.  
  18. find(\&wanted, $dir);
  19.  
  20. sub wanted {
  21. # Replace 'log.problem' with 'log.fixed.forever'
  22. if (-f && -T) {
  23. open(my $fh, '<', $_) or die "Could not open file '$_' $!";
  24. my @lines = <$fh>;
  25. close($fh);
  26. my $changed = 0;
  27. for (@lines) {
  28. if (s/log\.problem/log.fixed.forever/g) {
  29. $changed = 1;
  30. }
  31. }
  32. if ($changed) {
  33. open(my $fh, '>', $_) or die "Could not open file '$_' $!";
  34. print {$fh} @lines;
  35. close($fh);
  36. symlink($File::Find::name, "$test_dir/$_");
  37. }
  38. }
  39.  
  40. # Create symlink for files larger than 500MB
  41. if (-f && -s > 500 * 1024 * 1024) {
  42. symlink($File::Find::name, "$test2_dir/$_");
  43. }
  44. }
  45. ```
  46.  
  47. This script will search inside all files of the system for `log.problem` and replace it with `log.fixed.forever`. It will also create a symlink of those files to `/home/liviu/test` folder. In the same script it will search for all files bigger than `500MB` and create a symlink to `/home/liviu/test2` folder. The folders will be created if they do not exist.
  48.  
  49. Is there anything else you would like me to add to this script?
  50.  
  51.  
  52.  
  53.  
  54.  
  55.  
  56.  
  57.  
  58.  
  59.  
  60.  
  61.  
  62.  
  63.  
  64.  
  65.  
  66. To run the Perl script on Ubuntu, you need to follow these steps:
  67.  
  68. 1. Open a text editor and paste the Perl script into it.
  69. 2. Save the file with a `.pl` extension, for example `script.pl`.
  70. 3. Open a terminal and navigate to the directory where you saved the script.
  71. 4. Make sure that Perl is installed on your system by running `perl -v`. If it's not installed, you can install it by running `sudo apt-get install perl`.
  72. 5. Make the script executable by running `chmod +x script.pl`.
  73. 6. Run the script with `sudo ./script.pl`.
  74.  
  75. Please note that this script needs to be run with superuser privileges (`sudo`) because it needs to access all files on your system.
  76.  
  77. Is there anything else you would like to know?
Advertisement
Add Comment
Please, Sign In to add comment