LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 12-11-2011, 04:31 AM   #1
rng
Senior Member
 
Registered: Aug 2011
Posts: 1,198

Rep: Reputation: 47
removing some files


I have many downloaded complete web pages which I want to remove now. They are in many subfolders of a folder. The files will have extension 'html' or 'htm'. In addition there are many folders ending with '_files' to be removed. What command should I use?

I was thinking of using following 3 commands:
Code:
rm -rf *.html
rm -rf *.htm
rm -rf *_files
Thanks for your help.
 
Old 12-11-2011, 06:56 AM   #2
Snark1994
Senior Member
 
Registered: Sep 2010
Distribution: Debian
Posts: 1,632
Blog Entries: 3

Rep: Reputation: 346Reputation: 346Reputation: 346Reputation: 346
Code:
find . -name *.html -print0 | xargs -n1 -0 echo rm
find . -name *.htm -print0 | xargs -n1 -0 echo rm
find . -name *_files -print0 | xargs -n1 -0 echo rm -r
The '-f' flag might be a bad idea, as it won't prompt you to remove stuff. You can, of course, add it back in if you're sure. I've intentionally left the 'echo' in so you can see what you'll be deleting.

Last edited by Snark1994; 12-11-2011 at 06:57 AM.
 
Old 12-11-2011, 09:23 AM   #3
rng
Senior Member
 
Registered: Aug 2011
Posts: 1,198

Original Poster
Rep: Reputation: 47
Thanks. Why do we need to add '-print0' to find command? Also why -n1 and -0 options are required in xargs? Can we use simple command as:

Code:
find . -name *.html  | xargs -I {} rm {}
find . -name *.html -exec rm {} \;
Regarding -f command, there will be a lot of files and folders, so I do not want to confirm for each one of them.

Also what is wrong with the rm commands that I suggested?

I appreciate your help in bash scripting.

Last edited by rng; 12-11-2011 at 09:24 AM.
 
Old 12-12-2011, 03:22 PM   #4
Snark1994
Senior Member
 
Registered: Sep 2010
Distribution: Debian
Posts: 1,632
Blog Entries: 3

Rep: Reputation: 346Reputation: 346Reputation: 346Reputation: 346
I added the print0 option (and the -0 flag to xargs) so that it will treat a path with spaces in properly - otherwise you would get:

Code:
~ $ ls
My      File    My Document
~ $ find . -name *Document -print | xargs -n1 echo rm
rm My
rm Document
(I haven't tested the example, I'm on Windows atm, but the issue is that spaces in filenames can lead to unintented behaviour)

-n1 is a personal preference. It means pass them one argument at a time, so:

Code:
~ $ find . -name * -print0 | xargs -0 echo rm
rm file1 file2 file3 file4
~ $ find . -name * -print0 | xargs -n1 -0 echo rm
rm file1
rm file2
rm file3
rm file4
I just find the second more readable, especially when you've got a long filepath.

Certainly in zsh, 'rm' doesn't prompt you for every file without the '-f' flag, I think it's only the ones without write permission or something similar. If you know what you're doing, then your command is fine.

Your commands are both fine as far as I can see, though I would add back the -print0 and -0 flags on the first one.

The only reason I thought your 'rm' commands were 'wrong' was because you said

Quote:
They are in many subfolders of a folder.
which suggested to me that if you were executing this in, e.g. ~/folder, you might want to delete ~/folder/subfolder/file.html

Your rm commands won't find this, as they will only be looking for files in ~/folder, not ~/folder/subfolder. If this is wrong, then your commands are fine, I'd just remove the '-r' flag from the *.html and *.htm rms, as you don't need it 'cos you're deleting files not directories.

Hope this helps,
 
Old 12-13-2011, 07:01 AM   #5
rng
Senior Member
 
Registered: Aug 2011
Posts: 1,198

Original Poster
Rep: Reputation: 47
Thanks.
 
  


Reply



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



Similar Threads
Thread Thread Starter Forum Replies Last Post
manually removing the .vdi files left over after removing VirtualBox adityavpratap Linux - General 2 10-24-2010 12:04 PM
removing files bloodyscript Linux - Newbie 4 07-15-2008 05:20 PM
removing files kjs Linux - Software 1 02-17-2005 02:18 PM
Removing files wihtout removing containing Direcotry caps_phisto Linux - General 2 10-07-2004 08:16 AM
Removing files ascii2k Linux - General 2 11-26-2001 11:20 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 07:19 PM.

Main Menu
Advertisement
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
Open Source Consulting | Domain Registration