LinuxQuestions.org
Register a domain and help support LQ
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices

Reply
 
Search this Thread
Old 12-28-2004, 03:23 PM   #1
wazoo
LQ Newbie
 
Registered: Dec 2004
Location: Colorado
Posts: 9

Rep: Reputation: 1
how to rm backup files recursively


I think as a result of my rsync attempts, I have tons of backup files -- files with names like "foobar~" or "foobar~~" etc. They are all in my home directory and subdirectories.

I tried typing "rm -Rf *~" but it only seems to work one directory down, then go no further.

Is there some command -- and I recognize that I really need to be careful about adding that tilde or I can wipe out ALL my files -- that will delete all those unnecessary backups?
 
Old 12-28-2004, 04:00 PM   #2
mearley
LQ Newbie
 
Registered: Dec 2004
Distribution: Red Hat & Mandrake
Posts: 4

Rep: Reputation: 0
I think the problem that you are running into is that you are specifying the filename that you want to remove *~, but that will only cause it to remove files and directories in the top directory that have the name 'something~'.

Not sure how you can do it from one command. In the past I wrote a short script to clean out subdirectories of unwanted files.

The following script works, if it is in the top level directory where you want to start removing files.

#!/usr/bin/perl
$execline = "find . -name '*~'";

open (INPUT, "$execline |");
while (<INPUT>)
{
chomp $_;
`rm $_`;
}
close (INPUT);
 
Old 12-28-2004, 05:23 PM   #3
wazoo
LQ Newbie
 
Registered: Dec 2004
Location: Colorado
Posts: 9

Original Poster
Rep: Reputation: 1
Well, that was interesting. It didn't eliminate a good many files -- but that's because I have followed the bad habit of accepting files from others with spaces in them. So your script reports several errors in a file like "do dah day.~" -- it will tell me that it can't delete do, dah, or day.~ -- because of course those files don't exist. But it got rid of a lot of them.

Thanks for the time, and the programming expertise.
 
Old 12-28-2004, 06:55 PM   #4
Ebel
Member
 
Registered: May 2004
Distribution: Slackware 10
Posts: 63

Rep: Reputation: 15
Perhaps you could use find:
find . -iname '*~' -exec rm -f {}

That might work. I haven't tested it, so you should test it on some test files first.
 
Old 12-31-2004, 05:18 AM   #5
mearley
LQ Newbie
 
Registered: Dec 2004
Distribution: Red Hat & Mandrake
Posts: 4

Rep: Reputation: 0
Minor adjustment to script

The following will do the trick... If the previous command line does not work.


#!/usr/bin/perl
$execline = "find . -name '*~'";

open (INPUT, "$execline |");
while (<INPUT> )
{
chomp $_;
`rm "$_"`;
}
close (INPUT);
 
Old 12-31-2004, 05:57 AM   #6
jschiwal
Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 654Reputation: 654Reputation: 654Reputation: 654Reputation: 654Reputation: 654
Try escaping the '~' character with a backslash. The ~ character is used to represent your home directory so that could cause problems.

This oneliner has been tested:
find ./ -name "*\~" -exec rm -v {} \;

I take it back. The '~' character doesn't expand to your home directory inside of single or double quotes, but the '\~' didn't change the result.
Single or double quotes are both ok around the *\~ filename wildcard expression. If the expression involved a variable such as
find ./ -name "$1\~" -exec rm -v {} \;
then you would need to use double quotes.

Last edited by jschiwal; 12-31-2004 at 06:00 AM.
 
Old 12-31-2004, 10:03 AM   #7
wazoo
LQ Newbie
 
Registered: Dec 2004
Location: Colorado
Posts: 9

Original Poster
Rep: Reputation: 1
jschiwal, that worked like a charm. One of the other suggestions I got from somebody did indeed wipe out a lot of home "." files, so I did test this one first, and it worked great.

I'm saving your command line in a file I call "clean." Then I "chmod 777 clean" to make it an routine executable. Hmm. Guess I could move it over to cron, too.

Thanks, everybody, that's a genuinely useful discovery that will help me not to clutter up my computer and backup files.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Delete files for a particular changed date recursively? qwertyme Linux - Newbie 5 01-23-2009 10:41 AM
Recursively move files michaelsanford Linux - General 4 10-02-2005 01:50 PM
Copying hidden files recursively? mrd Linux - General 1 09-30-2005 07:51 AM
How to recursively rename files using their directory name pattern ceg4048 Linux - General 2 09-28-2005 01:16 PM
Recursively listing files over FTP? overbored Linux - Software 4 10-14-2004 12:12 PM


All times are GMT -5. The time now is 01:44 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration