Fedora Linux Support Community & Resources Center

Go Back   FedoraForum.org > Fedora 17/18 > Using Fedora
FedoraForum Search

Forgot Password? Join Us!

Using Fedora General support for current versions. Ask questions about Fedora and it's software that do not belong in any other forum.

Reply
 
Thread Tools Search this Thread Display Modes
  #1  
Old 20th December 2006, 01:53 AM
Wayne
Guest
 
Posts: n/a
deleting files in sub-directories

I'm confused with the 'rm' command and I'm worried that I will remove directories along with the files I want to delete if I use it with the -r operator.

I have a directory, say: /home/username/stuff

under /stuff there are several more directories and sub-directories and they all contain different files, including ones with the suffix 'txt' (beer.txt, boobs.txt, etc) I want to delete all those 'txt' files but leave everything else as is.

Someone please clue me in before I do serious damage

Cheers

Wayne
Reply With Quote
  #2  
Old 20th December 2006, 01:58 AM
cdlaforc Offline
Registered User
 
Join Date: Jun 2005
Location: Wisconsin
Age: 33
Posts: 241
Hello,
If you do a man on rm you see that rm -r removes files and directories recursively. So if you need to remove many files you may just want to write a script to cd to each of these directories and then issue the "rm *.txt" command. Any questions let me know.
__________________
Thanks,

Chris.
Reply With Quote
  #3  
Old 20th December 2006, 02:03 AM
wneumann Offline
Registered User
 
Join Date: Dec 2004
Posts: 511
You could do "ls -R *txt" first just to check there is nothing ending in txt that you don't want to delete. If not, "rm -r *txt" is safe.
Reply With Quote
  #4  
Old 20th December 2006, 02:05 AM
heavyseas's Avatar
heavyseas Offline
Registered User
 
Join Date: Oct 2006
Location: Bawlmer Hon'
Posts: 6
Quote:
Originally Posted by rondonjin
I'm confused with the 'rm' command and I'm worried that I will remove directories along with the files I want to delete if I use it with the -r operator.

I have a directory, say: /home/username/stuff

under /stuff there are several more directories and sub-directories and they all contain different files, including ones with the suffix 'txt' (beer.txt, boobs.txt, etc) I want to delete all those 'txt' files but leave everything else as is.

Someone please clue me in before I do serious damage

Cheers

Wayne
You should be able to use a combination of find and remove to get the results you want. Try the following:

find /home/username/stuff -name "*.txt" -print

If it gives you the list of files you want deleted change it as so:

find /home/username/stuff -name "*.txt" -print | xargs rm
Reply With Quote
  #5  
Old 20th December 2006, 02:09 AM
Wayne
Guest
 
Posts: n/a
Quote:
Originally Posted by cdlaforc
Hello,
If you do a man on rm you see that rm -r removes files and directories recursively. So if you need to remove many files you may just want to write a script to cd to each of these directories and then issue the "rm *.txt" command. Any questions let me know.
Thanks, I already did a man on rm and it frightened me hence my question

Cheers

Wayne
Reply With Quote
  #6  
Old 20th December 2006, 02:12 AM
Wayne
Guest
 
Posts: n/a
Thanks all. I thought it would be much simpler than it seems. I found this script but haven't tried it yet. I'm waiting till the brain warms up a bit and it makes more sense to me:

Code:
#!/usr/bin/perl -w
use File::Find;

sub rm();

@directories = ("/home/dir1", "/home/dir2");

find(\&rm, @directories);

sub rm() {
my ($filename) = $_;
if ($filename =~ /\.txt$/) {
unlink($File::Find::name);
# $File::Find::dir contains the current directory
# $File::Find::name contains the complete path name (dir + filename)
# $_ or $filename contains the filename alone.
}
}
Wayne
Reply With Quote
  #7  
Old 20th December 2006, 03:53 AM
wneumann Offline
Registered User
 
Join Date: Dec 2004
Posts: 511
You can't have that many to delete that just doing "rm -r -i *.txt" and answering y or n for each wouldn't be faster than agonizing about scripts. But anyway, so long as you don't have a directory namedn something.txt -- and you would surely know if you do -- "rm -r *.txt" is perfectly safe. I use "rm -r" all the time.
Reply With Quote
  #8  
Old 20th December 2006, 04:31 AM
Wayne
Guest
 
Posts: n/a
Quote:
Originally Posted by wneumann
You can't have that many to delete that just doing "rm -r -i *.txt" and answering y or n for each wouldn't be faster than agonizing about scripts. But anyway, so long as you don't have a directory namedn something.txt -- and you would surely know if you do -- "rm -r *.txt" is perfectly safe. I use "rm -r" all the time.
Wanna bet?

There are 22 directories off '/home/username/stuff', the first subdirectory '/home/username/stuff/a' contains 47 *.txt files, under that directory are 7 subdirectories, the first one '/home/username/stuff/a/a1' contains 1 *.txt file, the second one, '/home/username/stuff/a/a2' contains 49 *.txt files. Do you want to work out how many that could come to?

Wayne
Reply With Quote
  #9  
Old 20th December 2006, 05:03 AM
wneumann Offline
Registered User
 
Join Date: Dec 2004
Posts: 511
About 500? One "y" or "n" every two seconds (one second to think, the other to hit the key) is less than 20 minutes. I bet you've spent more than that on this already.
Reply With Quote
  #10  
Old 20th December 2006, 05:08 AM
Wayne
Guest
 
Posts: n/a
Quote:
Originally Posted by wneumann
About 500? One "y" or "n" every two seconds (one second to think, the other to hit the key) is less than 20 minutes. I bet you've spent more than that on this already.
Actually, I'm more inclined to zip it up and move it to my old laptop that's still running OS/2 and do a 'del *.txt /s' on it

Wayne
Reply With Quote
  #11  
Old 20th December 2006, 06:08 AM
Dies Offline
Registered User
 
Join Date: Oct 2006
Posts: 4,752
You can always try it and find out, I mean if you issue the command rm -r /home/user/whatever as a regular user it will either do what you want or do nothing since only root can delete home, right?

Me, I would just open Nautilus, select them all and right click, move to trash.
Reply With Quote
  #12  
Old 20th December 2006, 06:32 AM
Wayne
Guest
 
Posts: n/a
Quote:
Originally Posted by Dies
You can always try it and find out, I mean if you issue the command rm -r /home/user/whatever as a regular user it will either do what you want or do nothing since only root can delete home, right?

Me, I would just open Nautilus, select them all and right click, move to trash.
If I could select them all I would but they are spread over multiple subdirectories so it's not that easy. Actually, I haven't spent much time on it, just posted a quick question here and did a quick Google but I've had other things to do that are more important, like doing the laundry, had lunch, had a sly drop of stout from my keg, fed the cats, cleaned up their tray, watched a bit of TV and generally been lazy today Next up is a shower and a beer-run

My family went skiing on Monday and they're due back today. It's been a very peaceful three days, very conducive to relaxing and drinking beer in front of the goggle-box with the cats to keep me company. They should have gone for the week

Wayne

Last edited by rondonjin; 20th December 2006 at 06:37 AM.
Reply With Quote
  #13  
Old 20th December 2006, 06:54 PM
heavyseas's Avatar
heavyseas Offline
Registered User
 
Join Date: Oct 2006
Location: Bawlmer Hon'
Posts: 6
Quote:
Originally Posted by rondonjin
Wanna bet?

There are 22 directories off '/home/username/stuff', the first subdirectory '/home/username/stuff/a' contains 47 *.txt files, under that directory are 7 subdirectories, the first one '/home/username/stuff/a/a1' contains 1 *.txt file, the second one, '/home/username/stuff/a/a2' contains 49 *.txt files. Do you want to work out how many that could come to?

Wayne
Did you try the combination of find and rm. It should do what you're looking for.

find /home/username/stuff -name "*.txt" -print

This will list all the files in /home/username/stuff and all subdirectories which end with
.txt and print out the names. To delete them just pipe the results through the
xargs command with rm.

find /home/username/stuff -name "*.txt" -print | xargs rm

% mkdir foo; touch foo/x.txt
% mkdir foo/bar; touch foo/bar/x2.txt
% mkdir foo/bar/baz; touch foo/bar/baz/x3.txt
% find /home/username -name "*.txt" -print
/home/username/foo/x.txt
/home/username/foo/bar/x2.txt
/home/username/foo/bar/baz/x3.txt

% find /home/username/foo -name "*.txt" -print | xargs rm
% ls -lR /home/username/foo
/home/username/foo:
total 4
drwxr-xr-x 3 dba7900 users 4096 Dec 20 14:08 bar

/home/username/foo/bar:
total 4
drwxr-xr-x 2 dba7900 users 4096 Dec 20 14:08 baz

/home/username/foo/bar/baz:
total 0
Reply With Quote
  #14  
Old 20th December 2006, 11:02 PM
Wayne
Guest
 
Posts: n/a
Actually, no, I haven't done anything about it yet. It suddenly became low priority. I'll get back to it later using this info, thanks.

Wayne
Reply With Quote
  #15  
Old 20th December 2006, 11:34 PM
heavyseas's Avatar
heavyseas Offline
Registered User
 
Join Date: Oct 2006
Location: Bawlmer Hon'
Posts: 6
Quote:
Originally Posted by rondonjin
Actually, no, I haven't done anything about it yet. It suddenly became low priority. I'll get back to it later using this info, thanks.

Wayne
If I had a keg of stout removing files would be a low priority for me as well. Dry, sweet or imperial?
Reply With Quote
Reply

Tags
deleting, files, subdirectories

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

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 On
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Deleting files in KDE wombat53 Using Fedora 5 8th March 2009 09:57 PM
Deleting files in mulitple directories using wildcards TTA89 Using Fedora 1 4th March 2008 02:05 AM
Deleting files of a particular extension tech291083 Using Fedora 1 9th September 2007 01:47 PM
Fedora 6 not deleting files???? zygomatic Using Fedora 3 8th August 2007 08:44 AM
Deleting tmp files satimis Using Fedora 7 1st November 2004 04:32 AM


Current GMT-time: 22:15 (Tuesday, 21-05-2013)

TopSubscribe to XML RSS for all Threads in all ForumsFedoraForumDotOrg Archive
logo

All trademarks, and forum posts in this site are property of their respective owner(s).
FedoraForum.org is privately owned and is not directly sponsored by the Fedora Project or Red Hat, Inc.

Privacy Policy | Term of Use | Posting Guidelines | Archive | Contact Us | Founding Members

Powered by vBulletin® Copyright ©2000 - 2012, vBulletin Solutions, Inc.

FedoraForum is Powered by RedHat