Advertisement
Guest User

Untitled

a guest
Jul 23rd, 2019
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 2.33 KB | None | 0 0
  1. #/bin/bash
  2. ### For the Beginer Linux group
  3. ### Read the code ! It is purposely over commented for easy(er) understanding
  4. ### I'm not an expert in bash, but the script has been tested and works. Feel free to improve it and share back
  5.  
  6.  
  7.  
  8. bad="bob "   # Text you want to change
  9. location="."  # Where to look for. Put / to look everywhere; . (dot) for the current directory  You may have to run as root for some locations.
  10. bad_files=()  # Create an empty array
  11. RED="\e[5;91m"  # Define blinking (5) red (91)
  12. GREEN="\e[0;92m" # Define normal (0) green (92)
  13. NC='\e[0m' # Define normal (0) to reste the colors
  14.  
  15. for i in $( grep -ril $bad $location ) # Loop through content of files, with "i" for non-sensitive case. do NOT affect the substitution process later
  16. do echo -e  "\n--------------------------\nIn file $i" # Print the file containing the malicious text
  17. bad_files+=($i) # Store the file name into an array
  18. grep --color=always $bad $i # Shows the bad text found with color highlight
  19. done
  20.  
  21. echo -e "\n$GREEN List of modified files found: $NC\n ${bad_files}" # print the list of modified files
  22. echo -e "$RED Would you like to correct them ? $NC I'll do a backup first ;) (Yy/Nn)"
  23. read answer  # Expect user input and stores it in the variable "answer"
  24. case $answer in                                                                                                                                    
  25.     y|Y) echo -e "\n Type the full path of a backup directory (default /home/"$USER"/Backup)"
  26.     read backup_dir
  27.     if [ -n "$backup_dir" ]  # Did the user provided an answer
  28.         then mkdir $backup_dir 2>/dev/null # Create the directory provided by the user
  29.             for file in ${bad_files[@]} ; do # Loop through the found files
  30.                 cp $file $backup_dir 2>/dev/null # Copy then to the backup directory
  31.                 sed -i "s/$bad//g" $file # Substitute/$bad/by_nothing/globally.The "-i" applies the changes in the file  
  32.                 done
  33.         else backup_dir="/home/$USER/Backup" && mkdir $backup_dir 2>/dev/null # Set backup dir to default if aswer left blank
  34.             for file in ${bad_files[@]} ; do
  35.                 cp $file $backup_dir 2>/dev/null
  36.                 sed -i "s/$bad//g" $file
  37.                 done
  38.     fi
  39.     ;;                              
  40. n|N)
  41. echo OK, Bye !  ;
  42. ;;
  43. esac
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement