Remove backup files

Every time you edit and save a file on your Linux box, a backup file is automatically created. These files have simply a ~ character added either to the front or the end of the filename, usually at the end. A basic example is filename.txt~.

Notice

This has nothing to do with the general backups for files or databases you create from time to time. These files are created by the operating system.

You might wonder why I am writing now a script for removing those files. If it's your personal computer it's ok to keep backups, but not on your server where your homepage is running and where you store sensitive data. Just imagine what can happen if someone finds your index.php~ file, located in the DocumentRoot. By loading it into a browser window, all code can be viewed because (in this example) the file is no longer recongnized as PHP script, but as simple text document. The best method to prevent this is to remove all backup copies and keep only your truly needed scripts and documents. I am using it at home too, while developing I edit a lot of files and this means a lot of backups. Even if the backups are hidden files, sometimes I have to pack archives and send them to customers or simply need to browse myself through hidden files and folders but these backups make an owerview quite hard, so I remove them always.

To get rid of the backup files, use a bash script that you point to a folder of your choice and it will handle the rest. Save following file as rm_backup.sh to your computer or server and you are ready to go. How? Right-click the download link and choose "Save File As" or "Save Target As".

Download: rm_backup.sh

To execute the little bash script, use following command in your shell console from the same folder where you have saved the file:

./rm_backup.sh /path/to/directory/

In case you do not pass the the correct path to a directory as parameter, or the parameter is not a directory, it will stop and show an error message. If everything works fine, and backup files are found, you'll see a message along with the removed filename. The script will find any file matching a ~ character on the front or end of any filename.

I hope you like this little handy tool, it's my favorite bash script and I use it at least once a day, sometimes I really think on setting a cron-job to run it every hour or so. Once again, thanks to Christian Huegel for a helpful tip on the find command.

Contribute your thoughts

Personal information
Comment details
Image verification

To prevent automated logins, please enter the secret phrase shown in the image below.

If you have trouble reading the code, click on the code itself to generate a new one.

krwetatnt.net responds: September 26 08:19
thanks so much

Affiliation

  • PHP Link Directory
  • Cluj Blog Roll
v-beta-1