Awk|Bash – automatically remove unnecessary backup copies of files.
Home page Ubuntu C++ Hardware HTML i javascript Java Software SQL

Awk|Bash – automatically remove unnecessary backup copies of files.

Added: 2012-11-23

By creating a copy of the C++ classes, the number of files on the disk can grow very quickly.

The script removes files the following format:

[nameFile].[extension].[backupInformation]

Used commands:

  • ls | grep -c "textToSearch " – counting line where text is
  • ls | grep "^textToSearch\." - search for the line that begins with the text, and after text is dot

Below script:

#!/bin/bash

echo "The script remove backup file in format: ";
echo "[nameFile].[extension].[backupInformation]";

if [ -f $FILE_TEMP ]; then
	echo "Remove all backup file? [yes]";
	read option;
	if [ "$option" != "yes" ]; then
		exit 0;
	fi

fi


ls | awk 'BEGIN{
}{
	if (($0 != ".") || ($0 != "..")) {
		if (system("[ `ls | grep -c \"^"$0"\.\"` -gt 0 ]") == 0){
			system("echo \"File do delete: \"`ls | grep -c \"^"$0"\.\"`");
			system("ls | grep \"^"$0"\.\"");
			print "rm `ls | grep \"^"$0"\.\"`";
			print "...";
			system ("rm `ls | grep \"^"$0"\.\"`");
		}
	}
}'

echo "The end ;)";