LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Software (https://www.linuxquestions.org/questions/linux-software-2/)
-   -   Deleting backup files in my system. (https://www.linuxquestions.org/questions/linux-software-2/deleting-backup-files-in-my-system-379156/)

hgb 11-01-2005 11:16 PM

Deleting backup files in my system.
 
Hey, I dont know how to do it... I really dont like that my system start populating of:

filex.sss~
cuacua.sxcv~


I have tryied to do a simple thing

Code:

ls -a -p --recursive| grep "~"
with that I will get all the archives that have a ~... bu by the way, also if they start or have a intermediate ~ in the name (I guess).. wich is the correct way for find the backup files???

And wich is the correct way of do some like:

Code:

ls -a -p --recursive| grep "~" | rm

zhangmaike 11-02-2005 12:06 AM

You could do something like...

Code:

find -name "*~" | xargs rm
that will remove all files matching the pattern "*~" (all files ending with a ~) recursively, from the current directory onward... so... excercise caution. ;)

hgb 11-02-2005 12:20 AM

Thx for the tip, I have finded a problem, it delete the files that dosent contain spaces, so for example:

Code:


rea@linux:/pro_extern/txl10.4a.linux/ChallengeFiles> ls
cal.Txl        expression.cal  Input3.tu  questions 1.txt  t1.2.Txl  t1.Txl    t2.3.Txl  t3.Txl
cha1.Txl      Input1.tu      Input4.tu  questions 1.txt~  t1.3.Txl  t2.1.Txl  t2.Txl    Turing.Grm
Challenge.txt  Input2.tu      Prueba.Txl  t1.1.Txl          t1.4.Txl  t2.2.Txl  t3.1.Txl  Turing.Txl
rea@linux:/pro_extern/txl10.4a.linux/ChallengeFiles> find -name "*~" | xargs rm
rm: no se puede borrar «./questions»: No existe el fichero o el directorio
rm: no se puede borrar «1.txt~»: No existe el fichero o el directorio

I see that is taking the space like a separator, is is "questions 1.txt" not "questions" "1.txt"... :), thx by the way :).



EDIT:


Like I see now, xargs pass arguments... I guess that is passing questions 1.txt... I have tryied enter the command manually, and I see that for pass the space, there should be an backslash "\" before the space char, some like instead of pass questions 1.txt, pass questions\ 1.txt, now is that possible?????

zhangmaike 11-02-2005 12:44 AM

Hmmm...

Code:

find -name "*~"  -printf "\"%p\"\n" | xargs rm
will do the same thing, but because of the -printf option, it will print each filename within quotes... which may do the trick.

EDIT: Ah... okay... make sure the -printf option is LAST! (... if it isn't, it will delete all files recursively)

Tinkster 11-02-2005 12:59 AM

How about:

Code:

find -type f -name "*~" -exec rm "{}"  \;

Cheers,
Tink

hgb 11-06-2005 04:46 PM

Hi there, thx for the suguestionts, they work like a charm.

I have now tryied delete a specific dirs and its contents recursively, for example CVS folders in a project.


Tought I havent finded a good way of do it, also dont know if will work if the files or the directoires contain spaces....

And is a thing of 2 +1 steps....

Step 1) Write this (for delete CVS folders and contents):


Code:

find -type d -name "CVS"  -printf "\"%p\"\n" | xargs ls | xargs
After that, there will be a output like

Code:

./CVS: Entries Repository Root ./etc/CVS: Entries Repository Root ./etc/interface/CVS: Entries Repository Root

Step 2) copy the output (dont a nice way.. but work... :S)



Step 3) run deleter [paste here the output] OR in this case #deleter ./CVS: Entries Repository Root ./etc/CVS: Entries Repository Root ./etc/interface/CVS: Entries Repository Root

Where deleter is the name of the program output of the following code....

Code:

#include <stdio.h>
#include <string.h>

#define MAX_BUFFER 1024
int main(int argCount, char *argStringList[]){
        int i=0, lenOfCurDir=0;
        char dirString[MAX_BUFFER*2];
        char dirString2[MAX_BUFFER*2];
        char fString[MAX_BUFFER*2];
        dirString[0] = '\0';
        while(i < argCount -1){
                if( (char) argStringList[i+1][0] == '.'){
                        if( strlen(dirString) != 0){
                                sprintf(dirString2, "rmdir %s", dirString);
                                system(dirString2);
                        }
                        lenOfCurDir = strlen(argStringList[i+1]);
                        argStringList[i+1][lenOfCurDir-1] = '/';
                        sprintf(dirString, "%s", argStringList[i+1]);
                }else{
                        sprintf(fString, "rm %s%s", dirString, argStringList[i+1]);
                        system(fString);
                }
                i++;
        }
        return 0;
}

That will delete the files and dirs using system thing... also dont know if this will work if the dirs or files have a space(s) in the name.


But at less have worked OK here...

Tinkster 11-07-2005 12:15 AM

find -type d -name "CVS" -exec rm -rf "{}" \;


Cheers,
Tink


All times are GMT -5. The time now is 10:31 AM.