Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #/bin/bash
- ### For the Beginer Linux group
- ### Read the code ! It is purposely over commented for easy(er) understanding
- ### I'm not an expert in bash, but the script has been tested and works. Feel free to improve it and share back
- bad="bob " # Text you want to change
- location="." # Where to look for. Put / to look everywhere; . (dot) for the current directory You may have to run as root for some locations.
- bad_files=() # Create an empty array
- RED="\e[5;91m" # Define blinking (5) red (91)
- GREEN="\e[0;92m" # Define normal (0) green (92)
- NC='\e[0m' # Define normal (0) to reste the colors
- 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
- do echo -e "\n--------------------------\nIn file $i" # Print the file containing the malicious text
- bad_files+=($i) # Store the file name into an array
- grep --color=always $bad $i # Shows the bad text found with color highlight
- done
- echo -e "\n$GREEN List of modified files found: $NC\n ${bad_files}" # print the list of modified files
- echo -e "$RED Would you like to correct them ? $NC I'll do a backup first ;) (Yy/Nn)"
- read answer # Expect user input and stores it in the variable "answer"
- case $answer in
- y|Y) echo -e "\n Type the full path of a backup directory (default /home/"$USER"/Backup)"
- read backup_dir
- if [ -n "$backup_dir" ] # Did the user provided an answer
- then mkdir $backup_dir 2>/dev/null # Create the directory provided by the user
- for file in ${bad_files[@]} ; do # Loop through the found files
- cp $file $backup_dir 2>/dev/null # Copy then to the backup directory
- sed -i "s/$bad//g" $file # Substitute/$bad/by_nothing/globally.The "-i" applies the changes in the file
- done
- else backup_dir="/home/$USER/Backup" && mkdir $backup_dir 2>/dev/null # Set backup dir to default if aswer left blank
- for file in ${bad_files[@]} ; do
- cp $file $backup_dir 2>/dev/null
- sed -i "s/$bad//g" $file
- done
- fi
- ;;
- n|N)
- echo OK, Bye ! ;
- ;;
- esac
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement