LinuxQuestions.org
Help answer threads with 0 replies.
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 02-16-2006, 07:40 PM   #1
heislyc
LQ Newbie
 
Registered: Jul 2005
Location: Kuala Lumpur
Distribution: Red Hat
Posts: 13

Rep: Reputation: 0
Question How to remove certain types of file?


Hi all,

I've been surfing around but none of them helps.

I need to find out how to remove certain files base on file extension in Linux.

I've tried this from another website:

Quote:
find /scratch/yourdir -name 'core.*' > find.out then edit this new file. For example, remove the files you want to keep from 'find.out,' then put the remove command and a space ('rm ') at the beginning of each remaining line. To do this, while in the vi editor for example, enter:%s/^/rm /Write and quite the file, change its mode (chmod u+rwx find.out) and execute the file (./find.out). All files that remain in find.out will be deleted from your disk.
The result is: (let say i want to remove all .pif00 files listed in [find.out])

Quote:
rm: cannot lstat 'myfolder/file.doc': No such file or directory
Quote:
rm: cannot lstat '.pif': No such file or directory
OK, for above, i realize that because those files are with names like:

Quote:
filename.doc[ long spaces in between ].pif
That's why the method i used not really working. (How do i fix this?)

Guys, what's wrong? Or could anyone suggest me other ways to remove multiple files in multiple sub-directories base on file extensions?

Last edited by heislyc; 02-16-2006 at 07:52 PM.
 
Old 02-16-2006, 08:01 PM   #2
kegwell
Member
 
Registered: Feb 2004
Distribution: Gentoo Linux
Posts: 46

Rep: Reputation: 15
Unless I don't understand your question, why can't you simply use a wildcard recursively? For example, if you wanted to remove all .txt files within the directories report, project, work, and documents in the directory structure, /home/heislyc/documents/work/project/report just run the following command: rm -rR /home/heislyc/*.txt

I believe that is what you are looking for?
 
Old 02-16-2006, 08:21 PM   #3
gilead
Senior Member
 
Registered: Dec 2005
Location: Brisbane, Australia
Distribution: Slackware64 14.0
Posts: 4,141

Rep: Reputation: 168Reputation: 168
Have you tried something like the following:
Code:
find /tmp -name '*log' -exec rm -f {} \;
This will delete all of the files whose name ends in log that are in or below the /tmp directory.
 
Old 02-17-2006, 01:07 AM   #4
heislyc
LQ Newbie
 
Registered: Jul 2005
Location: Kuala Lumpur
Distribution: Red Hat
Posts: 13

Original Poster
Rep: Reputation: 0
I managed to solve the problem by using mv command.

Initially, the rm -R /home/*.txt doesn't work, it gives me no such files error.

After that, i've tried using mv -R /home/*/*/*.txt dummy, then only i managed to move out all those files to dummy directory, then delete all files that moved into [dummy].

That basically means, you need to insert each wildcard for each level of directory tree. :S

Thanks guys for reply, as for find command, i'll try it out, but i think same rules applied.
 
Old 02-17-2006, 05:22 AM   #5
lurker79
Member
 
Registered: Jan 2005
Location: UK
Posts: 55

Rep: Reputation: 16
I would use -ok instead of -exec when removing files as it will prompt you whether or not you want to run that command on that file, saves you from accidently deleting files when you typo the find command.

e.g.

find /tmp -name '*log' -ok rm -f {} \;
 
Old 02-17-2006, 12:21 PM   #6
gilead
Senior Member
 
Registered: Dec 2005
Location: Brisbane, Australia
Distribution: Slackware64 14.0
Posts: 4,141

Rep: Reputation: 168Reputation: 168
If there are only a few files, -ok would be practical. If there a lot of files, I use the following process:
Code:
find /tmp -name '*log' -exec ls -l {} \; # Sometimes I pipe through grep, if everything is OK I run
find /tmp -name '*log' -exec rm -f {} \;
Since I regularly backup my system this way is more practical.
 
  


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
determining file types supersucker Linux - Newbie 2 05-03-2005 12:29 PM
Getting text from different file types jedson Mandriva 2 12-14-2004 01:36 PM
changing file types buffed317 Linux - General 1 07-21-2004 09:26 PM
file types jclark00001 Linux - Newbie 2 02-17-2003 03:07 PM
File types in KDE ChimpFace9000 Linux - Newbie 1 04-23-2002 09:05 AM

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

All times are GMT -5. The time now is 07:22 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