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

20th December 2006, 01:53 AM
|
|
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
|

20th December 2006, 01:58 AM
|
|
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.
|

20th December 2006, 02:03 AM
|
|
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.
|

20th December 2006, 02:05 AM
|
 |
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
|

20th December 2006, 02:09 AM
|
|
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
|

20th December 2006, 02:12 AM
|
|
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
|

20th December 2006, 03:53 AM
|
|
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.
|

20th December 2006, 04:31 AM
|
|
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
|

20th December 2006, 05:03 AM
|
|
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.
|

20th December 2006, 05:08 AM
|
|
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
|

20th December 2006, 06:08 AM
|
|
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.
|

20th December 2006, 06:32 AM
|
|
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.
|

20th December 2006, 06:54 PM
|
 |
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
|

20th December 2006, 11:02 PM
|
|
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
|

20th December 2006, 11:34 PM
|
 |
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?
|
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Linear Mode
|
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
|
|
|
Current GMT-time: 22:15 (Tuesday, 21-05-2013)
|
|
 |
 |
 |
 |
|
|