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 |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
 |
GNU/Linux Basic Guide
This 255-page guide will provide you with the keys to understand the philosophy of free software, teach you how to use and handle it, and give you the tools required to move easily in the world of GNU/Linux. Many users and administrators will be taking their first steps with this GNU/Linux Basic guide and it will show you how to approach and solve the problems you encounter.
Click Here to receive this Complete Guide absolutely free. |
|
 |
12-28-2004, 03:23 PM
|
#1
|
|
LQ Newbie
Registered: Dec 2004
Location: Colorado
Posts: 9
Rep:
|
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?
|
|
|
|
12-28-2004, 04:00 PM
|
#2
|
|
LQ Newbie
Registered: Dec 2004
Distribution: Red Hat & Mandrake
Posts: 4
Rep:
|
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);
|
|
|
|
12-28-2004, 05:23 PM
|
#3
|
|
LQ Newbie
Registered: Dec 2004
Location: Colorado
Posts: 9
Original Poster
Rep:
|
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.
|
|
|
|
12-28-2004, 06:55 PM
|
#4
|
|
Member
Registered: May 2004
Distribution: Slackware 10
Posts: 63
Rep:
|
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.
|
|
|
|
12-31-2004, 05:18 AM
|
#5
|
|
LQ Newbie
Registered: Dec 2004
Distribution: Red Hat & Mandrake
Posts: 4
Rep:
|
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);
|
|
|
|
12-31-2004, 05:57 AM
|
#6
|
|
Moderator
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733
|
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.
|
|
|
|
12-31-2004, 10:03 AM
|
#7
|
|
LQ Newbie
Registered: Dec 2004
Location: Colorado
Posts: 9
Original Poster
Rep:
|
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.
|
|
|
|
| Thread Tools |
Search this Thread |
|
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
All times are GMT -5. The time now is 03:54 AM.
|
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|